Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / liblustre / lutil.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2004 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <assert.h>
25 #include <signal.h>
26 #include <sys/types.h>
27
28 #include <fcntl.h>
29 #ifdef HAVE_NETDB_H
30 #include <netdb.h>
31 #endif
32 #ifdef _AIX
33 #include "syscall_AIX.h"
34 #else
35 #include <syscall.h>
36 #endif
37 #include <sys/utsname.h>
38 #ifdef HAVE_NETINET_IN_H
39 #include <netinet/in.h>
40 #endif
41 #include <sys/socket.h>
42 #ifdef HAVE_ARPA_INET_H
43 #include <arpa/inet.h>
44 #endif
45 #ifdef HAVE_CATAMOUNT_DATA_H
46 #include <catamount/data.h>
47 #endif
48
49 #include "lutil.h"
50
51
52 unsigned int libcfs_subsystem_debug = ~0 - (S_LNET | S_LND);
53 unsigned int libcfs_debug = 0;
54
55 struct task_struct     *current;
56
57 void *inter_module_get(char *arg)
58 {
59         if (!strcmp(arg, "ldlm_cli_cancel_unused"))
60                 return ldlm_cli_cancel_unused;
61         else if (!strcmp(arg, "ldlm_namespace_cleanup"))
62                 return ldlm_namespace_cleanup;
63         else if (!strcmp(arg, "ldlm_replay_locks"))
64                 return ldlm_replay_locks;
65 #ifdef HAVE_QUOTA_SUPPORT
66         else if (!strcmp(arg, "osc_quota_interface"))
67                 return &osc_quota_interface;
68         else if (!strcmp(arg, "mdc_quota_interface"))
69                 return &mdc_quota_interface;
70         else if (!strcmp(arg, "lov_quota_interface"))
71                 return &lov_quota_interface;
72 #endif
73         else
74                 return NULL;
75 }
76
77 /*
78  * random number generator stuff
79  */
80
81 #ifdef HAVE_GETHOSTBYNAME
82 static int get_ipv4_addr()
83 {
84         struct utsname myname;
85         struct hostent *hptr;
86         int ip;
87
88         if (uname(&myname) < 0)
89                 return 0;
90
91         hptr = gethostbyname(myname.nodename);
92         if (hptr == NULL ||
93             hptr->h_addrtype != AF_INET ||
94             *hptr->h_addr_list == NULL) {
95                 CWARN("Warning: fail to get local IPv4 address\n");
96                 return 0;
97         }
98
99         ip = ntohl(*((int *) *hptr->h_addr_list));
100
101         return ip;
102 }
103 #endif
104
105 void liblustre_init_random()
106 {
107         int seed[2];
108         struct timeval tv;
109
110 #ifdef LIBLUSTRE_USE_URANDOM
111         int _rand_dev_fd;
112         _rand_dev_fd = syscall(SYS_open, "/dev/urandom", O_RDONLY);
113         if (_rand_dev_fd >= 0) {
114                 if (syscall(SYS_read, _rand_dev_fd,
115                             &seed, sizeof(seed)) == sizeof(seed)) {
116                         ll_srand(seed[0], seed[1]);
117                         return;
118                 }
119                 syscall(SYS_close, _rand_dev_fd);
120         }
121 #endif /* LIBLUSTRE_USE_URANDOM */
122
123 #ifdef HAVE_GETHOSTBYNAME
124         seed[0] = get_ipv4_addr();
125 #else
126         seed[0] = _my_pnid;
127 #endif
128         gettimeofday(&tv, NULL);
129         ll_srand(tv.tv_sec ^ __swab32(seed[0]), tv.tv_usec ^__swab32(getpid()));
130 }
131
132 void get_random_bytes(void *buf, int size)
133 {
134         int *p = buf;
135         int rem;
136         LASSERT(size >= 0);
137
138         rem = min((unsigned long)buf & (sizeof(int) - 1), size);
139         if (rem) {
140                 int val = ll_rand();
141                 memcpy(buf, &val, rem);
142                 p = buf + rem;
143                 size -= rem;
144         }
145
146         while (size >= sizeof(int)) {
147                 *p = ll_rand();
148                 size -= sizeof(int);
149                 p++;
150         }
151         buf = p;
152         if (size) {
153                 int val = ll_rand();
154                 memcpy(buf, &val, size);
155         }
156 }
157  
158 static void init_capability(int *res)
159 {
160 #ifdef HAVE_LIBCAP
161         cap_t syscap;
162         cap_flag_value_t capval;
163         int i;
164
165         *res = 0;
166
167         syscap = cap_get_proc();
168         if (!syscap) {
169                 CWARN("Warning: failed to get system capability, "
170                       "set to minimal\n");
171                 return;
172         }
173
174         for (i = 0; i < sizeof(cap_value_t) * 8; i++) {
175                 if (!cap_get_flag(syscap, i, CAP_EFFECTIVE, &capval)) {
176                         if (capval == CAP_SET) {
177                                 *res |= 1 << i;
178                         }
179                 }
180         }
181 #else
182         /*
183          * set fake cap flags to ship to linux server
184          * from client platforms that have none (eg. catamount)
185          *  full capability for root
186          *  no capability for anybody else
187          */
188 #define FAKE_ROOT_CAP 0x1ffffeff
189 #define FAKE_USER_CAP 0
190
191         *res = (current->fsuid == 0) ? FAKE_ROOT_CAP: FAKE_USER_CAP;
192 #endif
193 }
194
195 int in_group_p(gid_t gid)
196 {
197         int i;
198
199         if (gid == current->fsgid)
200                 return 1;
201
202         for (i = 0; i < current->ngroups; i++) {
203                 if (gid == current->groups[i])
204                         return 1;
205         }
206
207         return 0;
208 }
209
210 int liblustre_init_current(char *comm)
211 {
212         current = malloc(sizeof(*current));
213         if (!current) {
214                 CERROR("Not enough memory\n");
215                 return -ENOMEM;
216         }
217
218         strncpy(current->comm, comm, sizeof(current->comm));
219         current->pid = getpid();
220         current->fsuid = geteuid();
221         current->fsgid = getegid();
222         memset(&current->pending, 0, sizeof(current->pending));
223
224         current->max_groups = sysconf(_SC_NGROUPS_MAX);
225         current->groups = malloc(sizeof(gid_t) * current->max_groups);
226         if (!current->groups) {
227                 CERROR("Not enough memory\n");
228                 return -ENOMEM;
229         }
230         current->ngroups = getgroups(current->max_groups, current->groups);
231         if (current->ngroups < 0) {
232                 perror("Error getgroups");
233                 return -EINVAL;
234         }
235
236         init_capability(&current->cap_effective);
237
238         return 0;
239 }
240
241 void generate_random_uuid(unsigned char uuid_out[16])
242 {
243         get_random_bytes(uuid_out, sizeof(uuid_out));
244 }
245
246 int init_lib_portals()
247 {
248         int rc;
249         ENTRY;
250
251         rc = libcfs_debug_init(5 * 1024 * 1024);
252         if (rc != 0) {
253                 CERROR("libcfs_debug_init() failed: %d\n", rc);
254                 RETURN (-ENXIO);
255         }
256
257         rc = LNetInit();
258         if (rc != 0) {
259                 CERROR("LNetInit() failed: %d\n", rc);
260                 RETURN (-ENXIO);
261         }
262         RETURN(0);
263 }
264
265 extern void ptlrpc_exit_portals(void);
266 void cleanup_lib_portals()
267 {
268         libcfs_debug_cleanup();
269         ptlrpc_exit_portals();
270 }