Skip to content
This repository was archived by the owner on Mar 17, 2021. It is now read-only.

Commit 988c084

Browse files
committed
Allow fence_virtd to run as non-root
1 parent 6d79ea1 commit 988c084

File tree

2 files changed

+26
-36
lines changed

2 files changed

+26
-36
lines changed

server/daemon_init.c

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@
2929
/*
3030
* This should ultimately go in a header file.
3131
*/
32-
void daemon_init(const char *prog, int nofork);
32+
void daemon_init(const char *prog, const char *pid_file, int nofork);
3333
void daemon_cleanup(void);
34-
int check_process_running(const char *prog, pid_t * pid);
34+
int check_process_running(const char *cmd, const char *pid_file, pid_t * pid);
3535

3636
/*
3737
* Local prototypes.
3838
*/
39-
static void update_pidfile(const char *prog);
39+
static void update_pidfile(const char *filename);
4040
static int setup_sigmask(void);
4141
static char pid_filename[PATH_MAX];
4242

@@ -99,12 +99,10 @@ check_pid_valid(pid_t pid, const char *prog)
9999

100100

101101
int
102-
check_process_running(const char *prog, pid_t * pid)
102+
check_process_running(const char *cmd, const char *filename, pid_t * pid)
103103
{
104104
pid_t oldpid;
105105
FILE *fp;
106-
char filename[PATH_MAX];
107-
char *cmd;
108106
int ret;
109107
struct stat st;
110108

@@ -114,11 +112,6 @@ check_process_running(const char *prog, pid_t * pid)
114112
* Now see if there is a pidfile associated with this cmd in /var/run
115113
*/
116114
fp = NULL;
117-
memset(filename, 0, PATH_MAX);
118-
119-
cmd = basename((char *)prog);
120-
snprintf(filename, sizeof (filename), "/var/run/%s.pid", cmd);
121-
122115
ret = stat(filename, &st);
123116
if ((ret < 0) || (!st.st_size))
124117
return 0;
@@ -146,15 +139,11 @@ check_process_running(const char *prog, pid_t * pid)
146139

147140

148141
static void
149-
update_pidfile(const char *prog)
142+
update_pidfile(const char *filename)
150143
{
151144
FILE *fp = NULL;
152-
char *cmd;
153-
154-
memset(pid_filename, 0, PATH_MAX);
155145

156-
cmd = basename((char *)prog);
157-
snprintf(pid_filename, sizeof (pid_filename), "/var/run/%s.pid", cmd);
146+
strncpy(pid_filename, filename, PATH_MAX);
158147

159148
fp = fopen(pid_filename, "w");
160149
if (fp == NULL) {
@@ -197,28 +186,16 @@ setup_sigmask(void)
197186

198187

199188
void
200-
daemon_init(const char *prog, int nofork)
189+
daemon_init(const char *prog, const char *pid_file, int nofork)
201190
{
202-
uid_t uid;
203191
pid_t pid;
204192

205-
uid = getuid();
206-
if (uid) {
207-
syslog(LOG_ERR,
208-
"daemon_init: Sorry, only root wants to run this.\n");
209-
exit(1);
210-
}
211-
212-
/* Prevents multiple instances of the daemon from running.
213-
needs to be fixed by the devs.
214-
*/
215-
/* if (check_process_running(prog, &pid) && (pid != getpid())) {
193+
if (check_process_running(prog, pid_file, &pid) && (pid != getpid())) {
216194
syslog(LOG_ERR,
217195
"daemon_init: Process \"%s\" already running.\n",
218196
prog);
219197
exit(1);
220198
}
221-
*/
222199

223200
if (setup_sigmask() < 0) {
224201
syslog(LOG_ERR, "daemon_init: Unable to set signal mask.\n");
@@ -230,8 +207,7 @@ daemon_init(const char *prog, int nofork)
230207
exit(1);
231208
}
232209

233-
/* update_pidfile(prog); */
234-
210+
update_pidfile(pid_file);
235211
}
236212

237213

server/main.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <unistd.h>
55
#include <signal.h>
66
#include <sys/types.h>
7+
#include <sys/param.h>
78

89
/* Local includes */
910
#include <stdint.h>
@@ -16,7 +17,7 @@
1617

1718
/* configure.c */
1819
int do_configure(config_object_t *config, const char *filename);
19-
int daemon_init(const char *prog, int nofork);
20+
int daemon_init(const char *prog, const char *pid_file, int nofork);
2021
int daemon_cleanup(void);
2122

2223

@@ -29,6 +30,8 @@ usage(void)
2930
printf(" -d <level> Set debugging level to <level>.\n");
3031
printf(" -c Configuration mode.\n");
3132
printf(" -l List plugins.\n");
33+
printf(" -w Wait for initialization.\n");
34+
printf(" -p <file> Use <file> to record the active process id.\n");
3235
}
3336

3437

@@ -47,6 +50,7 @@ main(int argc, char **argv)
4750
char listener_name[80];
4851
char backend_name[80];
4952
const char *config_file = DEFAULT_CONFIG_FILE;
53+
char *pid_file = NULL;
5054
config_object_t *config = NULL;
5155
map_object_t *map = NULL;
5256
const listener_plugin_t *lp;
@@ -64,7 +68,7 @@ main(int argc, char **argv)
6468
return -1;
6569
}
6670

67-
while ((opt = getopt(argc, argv, "Ff:d:cwlh")) != EOF) {
71+
while ((opt = getopt(argc, argv, "Ff:d:cwlhp:")) != EOF) {
6872
switch(opt) {
6973
case 'F':
7074
printf("Background mode disabled\n");
@@ -74,6 +78,10 @@ main(int argc, char **argv)
7478
printf("Using %s\n", optarg);
7579
config_file = optarg;
7680
break;
81+
case 'p':
82+
printf("Using %s\n", optarg);
83+
pid_file = optarg;
84+
break;
7785
case 'd':
7886
debug_set = atoi(optarg);
7987
break;
@@ -190,7 +198,13 @@ main(int argc, char **argv)
190198
return 1;
191199
}
192200

193-
daemon_init(basename(argv[0]), foreground);
201+
if(pid_file == NULL) {
202+
pid_file = malloc(PATH_MAX);
203+
memset(pid_file, 0, PATH_MAX);
204+
snprintf(pid_file, PATH_MAX, "/var/run/%s.pid", basename(argv[0]));
205+
}
206+
207+
daemon_init(basename(argv[0]), pid_file, foreground);
194208
signal(SIGINT, exit_handler);
195209
signal(SIGTERM, exit_handler);
196210
signal(SIGQUIT, exit_handler);

0 commit comments

Comments
 (0)