Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
main: move -u and -g processing outside fork, fixes 2331
  • Loading branch information
perexg committed Sep 28, 2014
1 parent 60c5bcc commit f918323
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/main.c
Expand Up @@ -696,14 +696,13 @@ main(int argc, char **argv)
http_server_init(opt_bindaddr); // bind to ports only
htsp_init(opt_bindaddr); // bind to ports only

/* Daemonise */
if(opt_fork) {
/* Set priviledges */
if(opt_fork || opt_group || opt_user) {
const char *homedir;
gid_t gid;
uid_t uid;
struct group *grp = getgrnam(opt_group ?: "video");
struct passwd *pw = opt_user ? getpwnam(opt_user) : NULL;
FILE *pidfile = fopen(opt_pidpath, "w+");

if(grp != NULL) {
gid = grp->gr_gid;
Expand All @@ -715,7 +714,7 @@ main(int argc, char **argv)
if (getuid() != pw->pw_uid) {
gid_t glist[10];
int gnum;
gnum = get_user_groups(pw, glist, 10);
gnum = get_user_groups(pw, glist, ARRAY_SIZE(glist));
if (setgroups(gnum, glist)) {
tvhlog(LOG_ALERT, "START",
"setgroups() failed, do you have permission?");
Expand All @@ -738,6 +737,11 @@ main(int argc, char **argv)
"setuid() failed, do you have permission?");
return 1;
}
}

/* Daemonise */
if(opt_fork) {
FILE *pidfile = fopen(opt_pidpath, "w+");

if(daemon(0, 0)) {
exit(2);
Expand Down

0 comments on commit f918323

Please sign in to comment.