int i = 0;
int res = 0;
char **env = NULL;
- gid_t groups[] = {EXT_STORAGE_GROUP,
- EXT_STORAGE_APPDATA_GROUP,
- MEDIA_STORAGE_GROUP};
+ int n;
+ gid_t *groups;
if (!ctx)
- return -1;;
- if (setgid(ctx->gid)) {
- ERR("setgid failed: %d", errno);
+ return -1;
+
+ n = getgroups(0, NULL);
+ if (n < 0) {
+ ERR("Failed to get the number of supplementary group IDs");
return -1;
}
- if (setgroups(ARRAY_SIZE(groups), groups) < 0) {
- ERR("setgroups failed: %d", errno);
+
+ groups = (gid_t *)calloc(1, sizeof(gid_t) * (n + 3));
+ if (groups == NULL) {
+ ERR("out of memory");
+ return -1;
+ }
+
+ n = getgroups(n, groups);
+ if (n < 0) {
+ ERR("Failed to get list of supplementary group IDs");
+ free(groups);
return -1;
}
- if (setuid(ctx->uid)) {
- ERR("setuid failed: %d", errno);
+
+ groups[n++] = EXT_STORAGE_GROUP;
+ groups[n++] = EXT_STORAGE_APPDATA_GROUP;
+ groups[n++] = MEDIA_STORAGE_GROUP;
+
+ if (setgroups(n, groups) < 0) {
+ ERR("setgroups failed: %d", errno);
+ free(groups);
return -1;
}
+ free(groups);
+
env = ctx->env;
/* env variable ends by NULL element */
while (env[i]) {