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 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
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>
60 #ifdef HAVE_CATAMOUNT_DATA_H
61 #include <catamount/data.h>
67 unsigned int libcfs_subsystem_debug = ~0 - (S_LNET | S_LND);
68 unsigned int libcfs_debug = 0;
70 struct task_struct *current;
72 void *inter_module_get(char *arg)
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 if (!strcmp(arg, "mdc_quota_interface"))
81 return &mdc_quota_interface;
82 else if (!strcmp(arg, "lmv_quota_interface"))
83 return &lmv_quota_interface;
84 else if (!strcmp(arg, "osc_quota_interface"))
85 return &osc_quota_interface;
86 else if (!strcmp(arg, "lov_quota_interface"))
87 return &lov_quota_interface;
93 * random number generator stuff
96 #ifdef HAVE_GETHOSTBYNAME
97 static int get_ipv4_addr()
99 struct utsname myname;
100 struct hostent *hptr;
103 if (uname(&myname) < 0)
106 hptr = gethostbyname(myname.nodename);
108 hptr->h_addrtype != AF_INET ||
109 *hptr->h_addr_list == NULL) {
110 CWARN("Warning: fail to get local IPv4 address\n");
114 ip = ntohl(*((int *) *hptr->h_addr_list));
120 void liblustre_init_random()
125 #ifdef LIBLUSTRE_USE_URANDOM
127 _rand_dev_fd = syscall(SYS_open, "/dev/urandom", O_RDONLY);
128 if (_rand_dev_fd >= 0) {
129 if (syscall(SYS_read, _rand_dev_fd,
130 &seed, sizeof(seed)) == sizeof(seed)) {
131 ll_srand(seed[0], seed[1]);
132 syscall(SYS_close, _rand_dev_fd);
135 syscall(SYS_close, _rand_dev_fd);
137 #endif /* LIBLUSTRE_USE_URANDOM */
139 #ifdef HAVE_GETHOSTBYNAME
140 seed[0] = get_ipv4_addr();
144 gettimeofday(&tv, NULL);
145 ll_srand(tv.tv_sec ^ __swab32(seed[0]), tv.tv_usec ^__swab32(getpid()));
148 static void init_capability(__u32 *res)
152 cap_flag_value_t capval;
157 syscap = cap_get_proc();
159 CWARN("Warning: failed to get system capability, "
164 for (i = 0; i < sizeof(cap_value_t) * 8; i++) {
165 if (!cap_get_flag(syscap, i, CAP_EFFECTIVE, &capval)) {
166 if (capval == CAP_SET) {
173 * set fake cap flags to ship to linux server
174 * from client platforms that have none (eg. catamount)
175 * full capability for root
176 * no capability for anybody else
178 #define FAKE_ROOT_CAP 0x1ffffeff
179 #define FAKE_USER_CAP 0
181 *res = (current->fsuid == 0) ? FAKE_ROOT_CAP: FAKE_USER_CAP;
185 int cfs_curproc_is_in_groups(gid_t gid)
189 if (gid == current->fsgid)
192 for (i = 0; i < current->ngroups; i++) {
193 if (gid == current->groups[i])
200 int liblustre_init_current(char *comm)
202 current = malloc(sizeof(*current));
204 CERROR("Not enough memory\n");
208 strncpy(current->comm, comm, sizeof(current->comm));
209 current->pid = getpid();
210 current->gid = getgid();
211 current->fsuid = geteuid();
212 current->fsgid = getegid();
213 memset(¤t->pending, 0, sizeof(current->pending));
215 current->max_groups = sysconf(_SC_NGROUPS_MAX);
216 current->groups = malloc(sizeof(gid_t) * current->max_groups);
217 if (!current->groups) {
218 CERROR("Not enough memory\n");
221 current->ngroups = getgroups(current->max_groups, current->groups);
222 if (current->ngroups < 0) {
223 perror("Error getgroups");
227 init_capability(¤t->cap_effective);
232 void cfs_cap_raise(cfs_cap_t cap)
234 current->cap_effective |= (1 << cap);
237 void cfs_cap_lower(cfs_cap_t cap)
239 current->cap_effective &= ~(1 << cap);
242 int cfs_cap_raised(cfs_cap_t cap)
244 return current->cap_effective & (1 << cap);
247 cfs_cap_t cfs_curproc_cap_pack(void) {
248 return cfs_current()->cap_effective;
251 void cfs_curproc_cap_unpack(cfs_cap_t cap) {
252 cfs_current()->cap_effective = cap;
255 int cfs_capable(cfs_cap_t cap)
257 return cfs_cap_raised(cap);
260 int init_lib_portals()
265 rc = libcfs_debug_init(5 * 1024 * 1024);
267 CERROR("libcfs_debug_init() failed: %d\n", rc);
273 CERROR("LNetInit() failed: %d\n", rc);
279 extern void ptlrpc_exit_portals(void);
280 void cleanup_lib_portals()
282 libcfs_debug_cleanup();
283 ptlrpc_exit_portals();