Whamcloud - gitweb
Branch b1_4_newconfig2
[fs/lustre-release.git] / lustre / utils / l_getgroups.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2004 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
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.
11  *
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.
16  *
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.
20  *
21  */
22
23 #include <stdlib.h>
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <fcntl.h>
30 #include <pwd.h>
31 #include <grp.h>
32 #include <lustre/lustre_user.h>
33
34 int get_groups_local(struct mds_grp_downcall_data **grp)
35 {
36         struct mds_grp_downcall_data *param;
37         int i, maxgroups, size;
38         struct passwd *pw;
39         struct group  *gr;
40
41         pw = getpwuid((*grp)->mgd_uid);
42         if (!pw) {
43                 (*grp)->mgd_err = -errno;
44                 return sizeof(*param);
45         }
46
47         maxgroups = sysconf(_SC_NGROUPS_MAX);
48         size = offsetof(struct mds_grp_downcall_data, mgd_groups[maxgroups]);
49         param = malloc(size);
50         if (param == NULL) {
51                 (*grp)->mgd_err = -ENOMEM;
52                 return sizeof(*param);
53         }
54
55         memcpy(param, *grp, sizeof(*param));
56         *grp = param;
57         while ((gr = getgrent())) {
58                 if (!gr->gr_mem)
59                         continue;
60                 for (i = 0; gr->gr_mem[i]; i++) {
61                         if (strcmp(gr->gr_mem[i], pw->pw_name) == 0) {
62                                 param->mgd_groups[param->mgd_ngroups++] =
63                                         gr->gr_gid;
64                                 break;
65                         }
66                 }
67                 if (param->mgd_ngroups == maxgroups)
68                         break;
69         }
70         endgrent();
71
72         return size;
73 }
74
75 /* Note that we need to make the downcall regardless of error, so that the
76  * MDS doesn't continue to wait on the upcall. */
77 int main(int argc, char **argv)
78 {
79         int fd, rc, size;
80         struct mds_grp_downcall_data sparam = { MDS_GRP_DOWNCALL_MAGIC };
81         struct mds_grp_downcall_data *param = &sparam;
82         char pathname[1024];
83
84         if (argc != 3) {
85                 printf("bad parameter\n");
86                 return -1;
87         }
88
89         snprintf(pathname, 1024, "/proc/fs/lustre/mds/%s/group_info", argv[1]);
90         param->mgd_uid = atoi(argv[2]);
91
92         fd = open(pathname, O_WRONLY);
93         if (fd < 0) {
94                 printf("can't open device %s\n", pathname);
95                 return -1;
96         }
97
98         size = get_groups_local(&param);
99
100         rc = write(fd, param, size);
101
102         close(fd);
103         return rc;
104 }