groups=(p1)
public
Show source
static VALUE
proc_setgroups(VALUE obj, VALUE ary)
{
int ngroups, i;
rb_gid_t *groups;
long getgr_buf_len = sysconf(_SC_GETGR_R_SIZE_MAX);
char* getgr_buf;
if (getgr_buf_len < 0)
getgr_buf_len = 4096;
getgr_buf = ALLOCA_N(char, getgr_buf_len);
Check_Type(ary, T_ARRAY);
ngroups = RARRAY_LENINT(ary);
if (ngroups > maxgroups())
rb_raise(rb_eArgError, "too many groups, %d max", maxgroups());
groups = ALLOCA_N(rb_gid_t, ngroups);
for (i = 0; i < ngroups; i++) {
VALUE g = RARRAY_PTR(ary)[i];
if (FIXNUM_P(g)) {
groups[i] = NUM2GIDT(g);
}
else {
VALUE tmp = rb_check_string_type(g);
struct group grp;
struct group *p;
int ret;
if (NIL_P(tmp)) {
groups[i] = NUM2GIDT(g);
}
else {
const char *grpname = StringValueCStr(tmp);
ret = getgrnam_r(grpname, &grp, getgr_buf, getgr_buf_len, &p);
if (ret)
rb_sys_fail("getgrnam_r");
p = getgrnam(grpname);
if (p == NULL) {
rb_raise(rb_eArgError,
"can't find group for %s", RSTRING_PTR(tmp));
}
groups[i] = p->gr_gid;
}
}
}
if (setgroups(ngroups, groups) == -1) /* ngroups <= maxgroups */
rb_sys_fail(0);
return proc_getgroups(obj);
}