Whamcloud - gitweb
LU-1548 tests: add test 63 in conf-sanity
[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, Whamcloud, Inc.
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 <stdlib.h>
38 #include <string.h>
39 #include <assert.h>
40 #include <signal.h>
41 #include <sys/types.h>
42
43 #include <fcntl.h>
44 #ifdef HAVE_NETDB_H
45 #include <netdb.h>
46 #endif
47 #ifdef _AIX
48 #include "syscall_AIX.h"
49 #else
50 #include <sys/syscall.h>
51 #endif
52 #include <sys/utsname.h>
53 #ifdef HAVE_NETINET_IN_H
54 #include <netinet/in.h>
55 #endif
56 #include <sys/socket.h>
57 #ifdef HAVE_ARPA_INET_H
58 #include <arpa/inet.h>
59 #endif
60
61 #include "lutil.h"
62
63
64 unsigned int libcfs_subsystem_debug = ~0 - (S_LNET | S_LND);
65 unsigned int libcfs_debug = 0;
66
67 struct task_struct     *current;
68
69 void *inter_module_get(char *arg)
70 {
71         if (!strcmp(arg, "ldlm_cli_cancel_unused"))
72                 return ldlm_cli_cancel_unused;
73         else if (!strcmp(arg, "ldlm_namespace_cleanup"))
74                 return ldlm_namespace_cleanup;
75         else if (!strcmp(arg, "ldlm_replay_locks"))
76                 return ldlm_replay_locks;
77         else
78                 return NULL;
79 }
80
81 /*
82  * random number generator stuff
83  */
84
85 #ifdef HAVE_GETHOSTBYNAME
86 static int get_ipv4_addr()
87 {
88         struct utsname myname;
89         struct hostent *hptr;
90         int ip;
91
92         if (uname(&myname) < 0)
93                 return 0;
94
95         hptr = gethostbyname(myname.nodename);
96         if (hptr == NULL ||
97             hptr->h_addrtype != AF_INET ||
98             *hptr->h_addr_list == NULL) {
99                 CWARN("Warning: fail to get local IPv4 address\n");
100                 return 0;
101         }
102
103         ip = ntohl(*((int *) *hptr->h_addr_list));
104
105         return ip;
106 }
107 #endif
108
109 void liblustre_init_random()
110 {
111         int seed[2];
112         struct timeval tv;
113
114 #ifdef LIBLUSTRE_USE_URANDOM
115         int _rand_dev_fd;
116         _rand_dev_fd = syscall(SYS_open, "/dev/urandom", O_RDONLY);
117         if (_rand_dev_fd >= 0) {
118                 if (syscall(SYS_read, _rand_dev_fd,
119                             &seed, sizeof(seed)) == sizeof(seed)) {
120                         cfs_srand(seed[0], seed[1]);
121                         syscall(SYS_close, _rand_dev_fd);
122                         return;
123                 }
124                 syscall(SYS_close, _rand_dev_fd);
125         }
126 #endif /* LIBLUSTRE_USE_URANDOM */
127
128 #ifdef HAVE_GETHOSTBYNAME
129         seed[0] = get_ipv4_addr();
130 #else
131         seed[0] = _my_pnid;
132 #endif
133         gettimeofday(&tv, NULL);
134         cfs_srand(tv.tv_sec ^ __swab32(seed[0]), tv.tv_usec ^__swab32(getpid()));
135 }
136
137 static void init_capability(__u32 *res)
138 {
139 #ifdef HAVE_LIBCAP
140         cap_t syscap;
141         cap_flag_value_t capval;
142         int i;
143
144         *res = 0;
145
146         syscap = cap_get_proc();
147         if (!syscap) {
148                 CWARN("Warning: failed to get system capability, "
149                       "set to minimal\n");
150                 return;
151         }
152
153         for (i = 0; i < sizeof(cap_value_t) * 8; i++) {
154                 if (!cap_get_flag(syscap, i, CAP_EFFECTIVE, &capval)) {
155                         if (capval == CAP_SET) {
156                                 *res |= 1 << i;
157                         }
158                 }
159         }
160 #else
161         /*
162          * set fake cap flags to ship to linux server
163          * from client platforms that have none (eg. catamount)
164          *  full capability for root
165          *  no capability for anybody else
166          */
167 #define FAKE_ROOT_CAP 0x1ffffeff
168 #define FAKE_USER_CAP 0
169
170         *res = (current->fsuid == 0) ? FAKE_ROOT_CAP: FAKE_USER_CAP;
171 #endif
172 }
173
174 int cfs_curproc_is_in_groups(gid_t gid)
175 {
176         int i;
177
178         if (gid == current->fsgid)
179                 return 1;
180
181         for (i = 0; i < current->ngroups; i++) {
182                 if (gid == current->groups[i])
183                         return 1;
184         }
185
186         return 0;
187 }
188
189 int liblustre_init_current(char *comm)
190 {
191         current = malloc(sizeof(*current));
192         if (!current) {
193                 CERROR("Not enough memory\n");
194                 return -ENOMEM;
195         }
196
197         strncpy(current->comm, comm, sizeof(current->comm));
198         current->pid = getpid();
199         current->gid = getgid();
200         current->fsuid = geteuid();
201         current->fsgid = getegid();
202         memset(&current->pending, 0, sizeof(current->pending));
203
204         current->max_groups = sysconf(_SC_NGROUPS_MAX);
205         current->groups = malloc(sizeof(gid_t) * current->max_groups);
206         if (!current->groups) {
207                 CERROR("Not enough memory\n");
208                 return -ENOMEM;
209         }
210         current->ngroups = getgroups(current->max_groups, current->groups);
211         if (current->ngroups < 0) {
212                 perror("Error getgroups");
213                 return -EINVAL;
214         }
215
216         init_capability(&current->cap_effective);
217
218         return 0;
219 }
220
221 void cfs_cap_raise(cfs_cap_t cap)
222 {
223         current->cap_effective |= (1 << cap);
224 }
225
226 void cfs_cap_lower(cfs_cap_t cap)
227 {
228         current->cap_effective &= ~(1 << cap);
229 }
230
231 int cfs_cap_raised(cfs_cap_t cap)
232 {
233         return current->cap_effective & (1 << cap);
234 }
235
236 cfs_cap_t cfs_curproc_cap_pack(void) {
237         return cfs_current()->cap_effective;
238 }
239
240 void cfs_curproc_cap_unpack(cfs_cap_t cap) {
241         cfs_current()->cap_effective = cap;
242 }
243
244 int cfs_capable(cfs_cap_t cap)
245 {
246         return cfs_cap_raised(cap);
247 }
248
249 int init_lib_portals()
250 {
251         int rc;
252         ENTRY;
253
254         rc = libcfs_debug_init(5 * 1024 * 1024);
255         if (rc != 0) {
256                 CERROR("libcfs_debug_init() failed: %d\n", rc);
257                 RETURN (-ENXIO);
258         }
259
260         rc = LNetInit();
261         if (rc != 0) {
262                 CERROR("LNetInit() failed: %d\n", rc);
263                 RETURN (-ENXIO);
264         }
265         RETURN(0);
266 }
267
268 extern void ptlrpc_exit_portals(void);
269 void cleanup_lib_portals()
270 {
271         libcfs_debug_cleanup();
272         ptlrpc_exit_portals();
273 }