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