Whamcloud - gitweb
71a14d912e7d8b8bd6478d14955f0cefce910585
[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 <sys/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 static void init_capability(__u32 *res)
133 {
134 #ifdef HAVE_LIBCAP
135         cap_t syscap;
136         cap_flag_value_t capval;
137         int i;
138
139         *res = 0;
140
141         syscap = cap_get_proc();
142         if (!syscap) {
143                 CWARN("Warning: failed to get system capability, "
144                       "set to minimal\n");
145                 return;
146         }
147
148         for (i = 0; i < sizeof(cap_value_t) * 8; i++) {
149                 if (!cap_get_flag(syscap, i, CAP_EFFECTIVE, &capval)) {
150                         if (capval == CAP_SET) {
151                                 *res |= 1 << i;
152                         }
153                 }
154         }
155 #else
156         /*
157          * set fake cap flags to ship to linux server
158          * from client platforms that have none (eg. catamount)
159          *  full capability for root
160          *  no capability for anybody else
161          */
162 #define FAKE_ROOT_CAP 0x1ffffeff
163 #define FAKE_USER_CAP 0
164
165         *res = (current->fsuid == 0) ? FAKE_ROOT_CAP: FAKE_USER_CAP;
166 #endif
167 }
168
169 int in_group_p(gid_t gid)
170 {
171         int i;
172
173         if (gid == current->fsgid)
174                 return 1;
175
176         for (i = 0; i < current->ngroups; i++) {
177                 if (gid == current->groups[i])
178                         return 1;
179         }
180
181         return 0;
182 }
183
184 int liblustre_init_current(char *comm)
185 {
186         current = malloc(sizeof(*current));
187         if (!current) {
188                 CERROR("Not enough memory\n");
189                 return -ENOMEM;
190         }
191
192         strncpy(current->comm, comm, sizeof(current->comm));
193         current->pid = getpid();
194         current->gid = getgid();
195         current->fsuid = geteuid();
196         current->fsgid = getegid();
197         memset(&current->pending, 0, sizeof(current->pending));
198
199         current->max_groups = sysconf(_SC_NGROUPS_MAX);
200         current->groups = malloc(sizeof(gid_t) * current->max_groups);
201         if (!current->groups) {
202                 CERROR("Not enough memory\n");
203                 return -ENOMEM;
204         }
205         current->ngroups = getgroups(current->max_groups, current->groups);
206         if (current->ngroups < 0) {
207                 perror("Error getgroups");
208                 return -EINVAL;
209         }
210
211         init_capability(&current->cap_effective);
212
213         return 0;
214 }
215
216 int init_lib_portals()
217 {
218         int rc;
219         ENTRY;
220
221         rc = libcfs_debug_init(5 * 1024 * 1024);
222         if (rc != 0) {
223                 CERROR("libcfs_debug_init() failed: %d\n", rc);
224                 RETURN (-ENXIO);
225         }
226
227         rc = LNetInit();
228         if (rc != 0) {
229                 CERROR("LNetInit() failed: %d\n", rc);
230                 RETURN (-ENXIO);
231         }
232         RETURN(0);
233 }
234
235 extern void ptlrpc_exit_portals(void);
236 void cleanup_lib_portals()
237 {
238         libcfs_debug_cleanup();
239         ptlrpc_exit_portals();
240 }