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