4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
41 #include <sys/types.h>
48 #include "syscall_AIX.h"
50 #include <sys/syscall.h>
52 #include <sys/utsname.h>
53 #ifdef HAVE_NETINET_IN_H
54 #include <netinet/in.h>
56 #include <sys/socket.h>
57 #ifdef HAVE_ARPA_INET_H
58 #include <arpa/inet.h>
64 unsigned int libcfs_subsystem_debug = ~0 - (S_LNET | S_LND);
65 unsigned int libcfs_debug = 0;
67 struct task_struct *current;
69 void *inter_module_get(char *arg)
71 if (!strcmp(arg, "ldlm_cli_cancel_unused"))
72 return ldlm_cli_cancel_unused;
73 else if (!strcmp(arg, "ldlm_namespace_cleanup"))
74 return ldlm_namespace_cleanup;
75 else if (!strcmp(arg, "ldlm_replay_locks"))
76 return ldlm_replay_locks;
82 * random number generator stuff
85 #ifdef HAVE_GETHOSTBYNAME
86 static int get_ipv4_addr()
88 struct utsname myname;
92 if (uname(&myname) < 0)
95 hptr = gethostbyname(myname.nodename);
97 hptr->h_addrtype != AF_INET ||
98 *hptr->h_addr_list == NULL) {
99 CWARN("Warning: fail to get local IPv4 address\n");
103 ip = ntohl(*((int *) *hptr->h_addr_list));
109 void liblustre_init_random()
114 #ifdef LIBLUSTRE_USE_URANDOM
116 _rand_dev_fd = syscall(SYS_open, "/dev/urandom", O_RDONLY);
117 if (_rand_dev_fd >= 0) {
118 if (syscall(SYS_read, _rand_dev_fd,
119 &seed, sizeof(seed)) == sizeof(seed)) {
120 cfs_srand(seed[0], seed[1]);
121 syscall(SYS_close, _rand_dev_fd);
124 syscall(SYS_close, _rand_dev_fd);
126 #endif /* LIBLUSTRE_USE_URANDOM */
128 #ifdef HAVE_GETHOSTBYNAME
129 seed[0] = get_ipv4_addr();
133 gettimeofday(&tv, NULL);
134 cfs_srand(tv.tv_sec ^ __swab32(seed[0]), tv.tv_usec ^__swab32(getpid()));
137 static void init_capability(__u32 *res)
141 cap_flag_value_t capval;
146 syscap = cap_get_proc();
148 CWARN("Warning: failed to get system capability, "
153 for (i = 0; i < sizeof(cap_value_t) * 8; i++) {
154 if (!cap_get_flag(syscap, i, CAP_EFFECTIVE, &capval)) {
155 if (capval == CAP_SET) {
162 * set fake cap flags to ship to linux server
163 * from client platforms that have none (eg. catamount)
164 * full capability for root
165 * no capability for anybody else
167 #define FAKE_ROOT_CAP 0x1ffffeff
168 #define FAKE_USER_CAP 0
170 *res = (current->fsuid == 0) ? FAKE_ROOT_CAP: FAKE_USER_CAP;
174 int cfs_curproc_is_in_groups(gid_t gid)
178 if (gid == current->fsgid)
181 for (i = 0; i < current->ngroups; i++) {
182 if (gid == current->groups[i])
189 int liblustre_init_current(char *comm)
191 current = malloc(sizeof(*current));
193 CERROR("Not enough memory\n");
197 strncpy(current->comm, comm, sizeof(current->comm));
198 current->pid = getpid();
199 current->gid = getgid();
200 current->fsuid = geteuid();
201 current->fsgid = getegid();
202 memset(¤t->pending, 0, sizeof(current->pending));
204 current->max_groups = sysconf(_SC_NGROUPS_MAX);
205 current->groups = malloc(sizeof(gid_t) * current->max_groups);
206 if (!current->groups) {
207 CERROR("Not enough memory\n");
210 current->ngroups = getgroups(current->max_groups, current->groups);
211 if (current->ngroups < 0) {
212 perror("Error getgroups");
216 init_capability(¤t->cap_effective);
221 void cfs_cap_raise(cfs_cap_t cap)
223 current->cap_effective |= (1 << cap);
226 void cfs_cap_lower(cfs_cap_t cap)
228 current->cap_effective &= ~(1 << cap);
231 int cfs_cap_raised(cfs_cap_t cap)
233 return current->cap_effective & (1 << cap);
236 cfs_cap_t cfs_curproc_cap_pack(void) {
237 return cfs_current()->cap_effective;
240 void cfs_curproc_cap_unpack(cfs_cap_t cap) {
241 cfs_current()->cap_effective = cap;
244 int cfs_capable(cfs_cap_t cap)
246 return cfs_cap_raised(cap);
249 int init_lib_portals()
254 rc = libcfs_debug_init(5 * 1024 * 1024);
256 CERROR("libcfs_debug_init() failed: %d\n", rc);
262 CERROR("LNetInit() failed: %d\n", rc);
268 extern void ptlrpc_exit_portals(void);
269 void cleanup_lib_portals()
271 libcfs_debug_cleanup();
272 ptlrpc_exit_portals();