Whamcloud - gitweb
land b_groups onto HEAD:
[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
33 /*
34  * return:
35  *  0:      fail to insert (found identical)
36  *  1:      inserted
37  */
38 int insert_sort(gid_t *groups, int size, gid_t grp)
39 {
40         int i;
41         gid_t save;
42
43         for (i = 0; i < size; i++) {
44                 if (groups[i] == grp)
45                         return 0;
46                 if (groups[i] > grp)
47                         break;
48         }
49
50         for (; i <= size; i++) {
51                 save = groups[i];
52                 groups[i] = grp;
53                 grp = save;
54         }
55         return 1;
56 }
57
58 int get_groups_local(uid_t uid, int *ngroups, gid_t **groups)
59 {
60         int     maxgroups;
61         int     i, size = 0;
62         struct passwd *pw;
63         struct group  *gr;
64
65         *ngroups = 0;
66         *groups = NULL;
67         maxgroups = sysconf(_SC_NGROUPS_MAX);
68         *groups = malloc(maxgroups * sizeof(gid_t));
69         if (!*groups)
70                 return -ENOMEM;
71
72         pw = getpwuid(uid);
73         if (!pw)
74                 return -errno;
75
76         while ((gr = getgrent())) {
77                 if (!gr->gr_mem)
78                         continue;
79                 for (i = 0; gr->gr_mem[i]; i++) {
80                         if (strcmp(gr->gr_mem[i], pw->pw_name))
81                                 continue;
82                         size += insert_sort(*groups, size, gr->gr_gid);
83                         break;
84                 }
85                 if (size == maxgroups)
86                         break;
87         }
88         endgrent();
89         *ngroups = size;
90         return 0;
91 }
92
93 int main (int argc, char **argv)
94 {
95         int     fd, rc;
96         struct {
97                 uint32_t err;
98                 uint32_t uid;
99                 uint32_t ngroups;
100                 gid_t   *groups;
101         } ioc_data;
102         char    *pathname = "/proc/fs/lustre/mds/group_info";
103
104         if (argc != 2) {
105                 printf("bad parameter\n");
106                 return -EINVAL;
107         }
108
109         ioc_data.uid = atoi(argv[1]);
110
111         fd = open(pathname, O_WRONLY);
112         if (fd < 0) {
113                 rc = -errno;
114                 printf("can't open device %s\n", pathname);
115                 return rc;
116         }
117
118         ioc_data.err = get_groups_local(ioc_data.uid, &ioc_data.ngroups, &ioc_data.groups);
119
120         rc = write(fd, &ioc_data, sizeof(ioc_data));
121         return (rc != sizeof(ioc_data));
122 }