Whamcloud - gitweb
LU-3382 build: clean unused link dependencies
[fs/lustre-release.git] / lustre / liblustre / lutil.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #include <byteswap.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <malloc.h>
41 #include <netdb.h>
42 #include <stdio.h>
43 #include <unistd.h>
44 #include <arpa/inet.h>
45 #include <sys/socket.h>
46 #include <sys/syscall.h>
47 #include <sys/time.h>
48 #include <sys/types.h>
49 #include <sys/utsname.h>
50 #include <libcfs/libcfs.h>
51 #include <lustre/lustre_idl.h>
52 #include <liblustre.h>
53 #include <lnet/lnet.h>
54 #include <lustre_dlm.h>
55 #include "lutil.h"
56
57 struct task_struct *current;
58
59 /*
60  * random number generator stuff
61  */
62
63 #ifdef HAVE_GETHOSTBYNAME
64 static int get_ipv4_addr()
65 {
66         struct utsname myname;
67         struct hostent *hptr;
68         int ip;
69
70         if (uname(&myname) < 0)
71                 return 0;
72
73         hptr = gethostbyname(myname.nodename);
74         if (hptr == NULL ||
75             hptr->h_addrtype != AF_INET ||
76             *hptr->h_addr_list == NULL) {
77                 CWARN("Warning: fail to get local IPv4 address\n");
78                 return 0;
79         }
80
81         ip = ntohl(*((int *) *hptr->h_addr_list));
82
83         return ip;
84 }
85 #endif
86
87 void liblustre_init_random()
88 {
89         int seed[2];
90         struct timeval tv;
91
92 #ifdef LIBLUSTRE_USE_URANDOM
93         int _rand_dev_fd;
94         _rand_dev_fd = syscall(SYS_open, "/dev/urandom", O_RDONLY);
95         if (_rand_dev_fd >= 0) {
96                 if (syscall(SYS_read, _rand_dev_fd,
97                             &seed, sizeof(seed)) == sizeof(seed)) {
98                         cfs_srand(seed[0], seed[1]);
99                         syscall(SYS_close, _rand_dev_fd);
100                         return;
101                 }
102                 syscall(SYS_close, _rand_dev_fd);
103         }
104 #endif /* LIBLUSTRE_USE_URANDOM */
105
106 #ifdef HAVE_GETHOSTBYNAME
107         seed[0] = get_ipv4_addr();
108 #else
109         seed[0] = _my_pnid;
110 #endif
111         gettimeofday(&tv, NULL);
112         cfs_srand(tv.tv_sec ^ __swab32(seed[0]), tv.tv_usec ^__swab32(getpid()));
113 }
114
115 static void init_capability(__u32 *res)
116 {
117 #ifdef HAVE_LIBCAP
118         cap_t syscap;
119         cap_flag_value_t capval;
120         int i;
121
122         *res = 0;
123
124         syscap = cap_get_proc();
125         if (!syscap) {
126                 CWARN("Warning: failed to get system capability, "
127                       "set to minimal\n");
128                 return;
129         }
130
131         for (i = 0; i < sizeof(cap_value_t) * 8; i++) {
132                 if (!cap_get_flag(syscap, i, CAP_EFFECTIVE, &capval)) {
133                         if (capval == CAP_SET) {
134                                 *res |= 1 << i;
135                         }
136                 }
137         }
138 #else
139         /*
140          * set fake cap flags to ship to linux server
141          * from client platforms that have none (eg. catamount)
142          *  full capability for root
143          *  no capability for anybody else
144          */
145 #define FAKE_ROOT_CAP 0x1ffffeff
146 #define FAKE_USER_CAP 0
147
148         *res = (current->fsuid == 0) ? FAKE_ROOT_CAP: FAKE_USER_CAP;
149 #endif
150 }
151
152 int in_group_p(gid_t gid)
153 {
154         int i;
155
156         if (gid == current->fsgid)
157                 return 1;
158
159         for (i = 0; i < current->ngroups; i++) {
160                 if (gid == current->groups[i])
161                         return 1;
162         }
163
164         return 0;
165 }
166
167 int liblustre_init_current(char *comm)
168 {
169         current = malloc(sizeof(*current));
170         if (!current) {
171                 CERROR("Not enough memory\n");
172                 return -ENOMEM;
173         }
174
175         strncpy(current->comm, comm, sizeof(current->comm));
176         current->pid = getpid();
177         current->gid = getgid();
178         current->fsuid = geteuid();
179         current->fsgid = getegid();
180
181         current->max_groups = sysconf(_SC_NGROUPS_MAX);
182         current->groups = malloc(sizeof(gid_t) * current->max_groups);
183         if (!current->groups) {
184                 CERROR("Not enough memory\n");
185                 return -ENOMEM;
186         }
187         current->ngroups = getgroups(current->max_groups, current->groups);
188         if (current->ngroups < 0) {
189                 perror("Error getgroups");
190                 return -EINVAL;
191         }
192
193         init_capability(&current->cap_effective);
194
195         return 0;
196 }
197
198 void cfs_cap_raise(cfs_cap_t cap)
199 {
200         current->cap_effective |= (1 << cap);
201 }
202
203 void cfs_cap_lower(cfs_cap_t cap)
204 {
205         current->cap_effective &= ~(1 << cap);
206 }
207
208 int cfs_cap_raised(cfs_cap_t cap)
209 {
210         return current->cap_effective & (1 << cap);
211 }
212
213 cfs_cap_t cfs_curproc_cap_pack(void) {
214         return current->cap_effective;
215 }
216
217 void cfs_curproc_cap_unpack(cfs_cap_t cap) {
218         current->cap_effective = cap;
219 }
220
221 int cfs_capable(cfs_cap_t cap)
222 {
223         return cfs_cap_raised(cap);
224 }
225
226 int init_lib_portals()
227 {
228         int rc;
229         ENTRY;
230
231         rc = libcfs_debug_init(5 * 1024 * 1024);
232         if (rc != 0) {
233                 CERROR("libcfs_debug_init() failed: %d\n", rc);
234                 RETURN (-ENXIO);
235         }
236
237         rc = LNetInit();
238         if (rc != 0) {
239                 CERROR("LNetInit() failed: %d\n", rc);
240                 RETURN (-ENXIO);
241         }
242         RETURN(0);
243 }
244
245 extern void ptlrpc_exit_portals(void);
246 void cleanup_lib_portals()
247 {
248         libcfs_debug_cleanup();
249         ptlrpc_exit_portals();
250 }