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