1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2004 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <sys/types.h>
33 #include "syscall_AIX.h"
37 #include <sys/utsname.h>
38 #ifdef HAVE_NETINET_IN_H
39 #include <netinet/in.h>
41 #include <sys/socket.h>
42 #ifdef HAVE_ARPA_INET_H
43 #include <arpa/inet.h>
45 #ifdef HAVE_CATAMOUNT_DATA_H
46 #include <catamount/data.h>
52 unsigned int libcfs_subsystem_debug = ~0 - (S_LNET | S_LND);
53 unsigned int libcfs_debug = 0;
55 struct task_struct *current;
57 void *inter_module_get(char *arg)
59 if (!strcmp(arg, "ldlm_cli_cancel_unused"))
60 return ldlm_cli_cancel_unused;
61 else if (!strcmp(arg, "ldlm_namespace_cleanup"))
62 return ldlm_namespace_cleanup;
63 else if (!strcmp(arg, "ldlm_replay_locks"))
64 return ldlm_replay_locks;
70 * random number generator stuff
72 #ifdef LIBLUSTRE_USE_URANDOM
73 static int _rand_dev_fd = -1;
76 #ifdef HAVE_GETHOSTBYNAME
77 static int get_ipv4_addr()
79 struct utsname myname;
83 if (uname(&myname) < 0)
86 hptr = gethostbyname(myname.nodename);
88 hptr->h_addrtype != AF_INET ||
89 *hptr->h_addr_list == NULL) {
90 CWARN("Warning: fail to get local IPv4 address\n");
94 ip = ntohl(*((int *) *hptr->h_addr_list));
100 void liblustre_init_random()
105 #ifdef LIBLUSTRE_USE_URANDOM
106 _rand_dev_fd = syscall(SYS_open, "/dev/urandom", O_RDONLY);
107 if (_rand_dev_fd >= 0) {
108 if (syscall(SYS_read, _rand_dev_fd,
109 &seed, sizeof(int)) == sizeof(int)) {
113 syscall(SYS_close, _rand_dev_fd);
116 #endif /* LIBLUSTRE_USE_URANDOM */
118 #ifdef HAVE_GETHOSTBYNAME
119 seed = get_ipv4_addr();
123 gettimeofday(&tv, NULL);
124 srand(tv.tv_sec + tv.tv_usec + getpid() + __swab32(seed));
127 void get_random_bytes(void *buf, int size)
132 #ifdef LIBLUSTRE_USE_URANDOM
133 if (_rand_dev_fd >= 0) {
134 if (syscall(SYS_read, _rand_dev_fd, buf, size) == size)
136 syscall(SYS_close, _rand_dev_fd);
145 static void init_capability(int *res)
149 cap_flag_value_t capval;
154 syscap = cap_get_proc();
156 CWARN("Warning: failed to get system capability, "
161 for (i = 0; i < sizeof(cap_value_t) * 8; i++) {
162 if (!cap_get_flag(syscap, i, CAP_EFFECTIVE, &capval)) {
163 if (capval == CAP_SET) {
170 * set fake cap flags to ship to linux server
171 * from client platforms that have none (eg. catamount)
172 * full capability for root
173 * no capability for anybody else
175 #define FAKE_ROOT_CAP 0x1ffffeff
176 #define FAKE_USER_CAP 0
178 *res = (current->fsuid == 0) ? FAKE_ROOT_CAP: FAKE_USER_CAP;
182 int in_group_p(gid_t gid)
186 if (gid == current->fsgid)
189 for (i = 0; i < current->ngroups; i++) {
190 if (gid == current->groups[i])
197 int liblustre_init_current(char *comm)
199 current = malloc(sizeof(*current));
201 CERROR("Not enough memory\n");
205 strncpy(current->comm, comm, sizeof(current->comm));
206 current->pid = getpid();
207 current->fsuid = geteuid();
208 current->fsgid = getegid();
209 memset(¤t->pending, 0, sizeof(current->pending));
211 current->max_groups = sysconf(_SC_NGROUPS_MAX);
212 current->groups = malloc(sizeof(gid_t) * current->max_groups);
213 if (!current->groups) {
214 CERROR("Not enough memory\n");
217 current->ngroups = getgroups(current->max_groups, current->groups);
218 if (current->ngroups < 0) {
219 perror("Error getgroups");
223 init_capability(¤t->cap_effective);
228 void generate_random_uuid(unsigned char uuid_out[16])
230 get_random_bytes(uuid_out, sizeof(uuid_out));
233 int init_lib_portals()
240 CERROR("LNetInit failed: %d\n", rc);
246 extern void ptlrpc_exit_portals(void);
247 void cleanup_lib_portals()
249 ptlrpc_exit_portals();