getgrouplist.xs

filter: Statementstmt 100% 13 / 13 Branchbran 66.6% 8 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.0% 21 / 25 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/User/getgrouplist.pm »
1 /*
2 * Copyright (C) 2007-2014 Collax GmbH
3 * (Bastian Friedrich <bastian.friedrich@collax.com>)
4 */
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8 #include <pwd.h>
9 #include <unistd.h>
10  
11 MODULE = User::getgrouplist PACKAGE = User::getgrouplist
12  
13 PROTOTYPES: ENABLE
14  
15 AV *
16 getgrouplist(username)
17 const char *username
18 PREINIT:
19 2 int count = 0;
20 int max; /* OpenBSD/darwin overwrite input value */
21 int i;
22 2 gid_t *groups = NULL;
23 struct passwd *pw;
24 PPCODE:
25 2 pw = getpwnam(username);
26 2 RETVAL = NULL;
27  
28 2 ▶ if (pw != NULL) { /* Only execute when user exists */
Branch: 1 branch
BranchTrueFalseTotal
if (pw != NULL) { /* Only execute when user exists */ 1 1 2
29 #if defined __OpenBSD__ || defined __MACH__
30 /*
31 * The getgrouplist implementation in OpenBSD and MacOS X/
32 * darwin/MACH will not return the number of groups if it
33 * is larger than the input count argument.
34 * Thus, we need to find a list size. Start with an
35 * arbitrary number (32), and duplicate it on each iteration.
36 *
37 * 32 is a random number. NGROUPS_MAX defaults to 16 on my
38 * OpenBSD.
39 */
40 max = 32;
41 count = max;
42 groups = (gid_t *) malloc(count * sizeof (gid_t));
43 while (groups && (getgrouplist(username, pw->pw_gid, groups, &count) < 0) && (count < sysconf(_SC_NGROUPS_MAX))) {
44 max *= 2;
45 count = max; /* Re-set. Was overwritten by last getgrouplist call */
46 groups = (gid_t *) realloc(groups, count * sizeof (gid_t));
47 if (!groups) {
48 count = 0;
49 continue;
50 }
51 }
52 #else
53 #if defined __CYGWIN__
54 /* Cygwin behaves differently than Linux -- will return count even if groups is NULL */
55 if (getgrouplist(username, pw->pw_gid, NULL, &count) > 0) {
56 #else
57 1 ▶ if (getgrouplist(username, pw->pw_gid, NULL, &count) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (getgrouplist(username, pw->pw_gid, NULL, &count) < 0) { 1 0 1
58 #endif
59 1 groups = (gid_t *) malloc(count * sizeof (gid_t));
60 1 ▶ if (groups) {
Branch: 1 branch
BranchTrueFalseTotal
if (groups) { 1 0 1
61 1 getgrouplist(username, pw->pw_gid, groups, &count);
62 }
63 }
64 #endif
65 1 ▶ if (groups) {
Branch: 1 branch
BranchTrueFalseTotal
if (groups) { 1 0 1
66 2 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 1 1 2
67 1 ▶ XPUSHs(sv_2mortal(newSViv(groups[i])));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(groups[i]))); 0 1 1
68 }
69 1 free(groups);
70 }
71 }
↑ Summary blib/lib/User/getgrouplist.pm »