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