1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
43 #include <sys/types.h>
50 #include "syscall_AIX.h"
52 #include <sys/syscall.h>
54 #include <sys/utsname.h>
55 #ifdef HAVE_NETINET_IN_H
56 #include <netinet/in.h>
58 #include <sys/socket.h>
59 #ifdef HAVE_ARPA_INET_H
60 #include <arpa/inet.h>
62 #ifdef HAVE_CATAMOUNT_DATA_H
63 #include <catamount/data.h>
69 unsigned int libcfs_subsystem_debug = ~0 - (S_LNET | S_LND);
70 unsigned int libcfs_debug = 0;
72 struct task_struct *current;
74 void *inter_module_get(char *arg)
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;
87 * random number generator stuff
90 #ifdef HAVE_GETHOSTBYNAME
91 static int get_ipv4_addr()
93 struct utsname myname;
97 if (uname(&myname) < 0)
100 hptr = gethostbyname(myname.nodename);
102 hptr->h_addrtype != AF_INET ||
103 *hptr->h_addr_list == NULL) {
104 CWARN("Warning: fail to get local IPv4 address\n");
108 ip = ntohl(*((int *) *hptr->h_addr_list));
114 void liblustre_init_random()
119 #ifdef LIBLUSTRE_USE_URANDOM
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);
129 syscall(SYS_close, _rand_dev_fd);
131 #endif /* LIBLUSTRE_USE_URANDOM */
133 #ifdef HAVE_GETHOSTBYNAME
134 seed[0] = get_ipv4_addr();
138 gettimeofday(&tv, NULL);
139 cfs_srand(tv.tv_sec ^ __swab32(seed[0]), tv.tv_usec ^__swab32(getpid()));
142 static void init_capability(__u32 *res)
146 cap_flag_value_t capval;
151 syscap = cap_get_proc();
153 CWARN("Warning: failed to get system capability, "
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) {
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
172 #define FAKE_ROOT_CAP 0x1ffffeff
173 #define FAKE_USER_CAP 0
175 *res = (current->fsuid == 0) ? FAKE_ROOT_CAP: FAKE_USER_CAP;
179 int cfs_curproc_is_in_groups(gid_t gid)
183 if (gid == current->fsgid)
186 for (i = 0; i < current->ngroups; i++) {
187 if (gid == current->groups[i])
194 int liblustre_init_current(char *comm)
196 current = malloc(sizeof(*current));
198 CERROR("Not enough memory\n");
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(¤t->pending, 0, sizeof(current->pending));
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");
215 current->ngroups = getgroups(current->max_groups, current->groups);
216 if (current->ngroups < 0) {
217 perror("Error getgroups");
221 init_capability(¤t->cap_effective);
226 void cfs_cap_raise(cfs_cap_t cap)
228 current->cap_effective |= (1 << cap);
231 void cfs_cap_lower(cfs_cap_t cap)
233 current->cap_effective &= ~(1 << cap);
236 int cfs_cap_raised(cfs_cap_t cap)
238 return current->cap_effective & (1 << cap);
241 cfs_cap_t cfs_curproc_cap_pack(void) {
242 return cfs_current()->cap_effective;
245 void cfs_curproc_cap_unpack(cfs_cap_t cap) {
246 cfs_current()->cap_effective = cap;
249 int cfs_capable(cfs_cap_t cap)
251 return cfs_cap_raised(cap);
254 int init_lib_portals()
259 rc = libcfs_debug_init(5 * 1024 * 1024);
261 CERROR("libcfs_debug_init() failed: %d\n", rc);
267 CERROR("LNetInit() failed: %d\n", rc);
273 extern void ptlrpc_exit_portals(void);
274 void cleanup_lib_portals()
276 libcfs_debug_cleanup();
277 ptlrpc_exit_portals();