daemon: added daemonize_init(), daemonize_finish()

The constructor/destructor functions parse and free the configuration
properly.  This way, we don't have to load the pid file path more than
once.
This commit is contained in:
Max Kellermann 2009-01-18 18:29:27 +01:00
parent 0dd2dfff9d
commit bfcaecabbd
3 changed files with 73 additions and 46 deletions

View File

@ -17,8 +17,8 @@
*/ */
#include "daemon.h" #include "daemon.h"
#include "conf.h"
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
@ -34,26 +34,40 @@
#include <grp.h> #include <grp.h>
#endif #endif
#ifndef WIN32
/** the Unix user name which MPD runs as */
static char *user_name;
/** the Unix user id which MPD runs as */
static uid_t user_uid;
/** the Unix group id which MPD runs as */
static gid_t user_gid;
/** the absolute path of the pidfile */
static char *pidfile;
#endif
void void
daemonize_kill(void) daemonize_kill(void)
{ {
#ifndef WIN32 #ifndef WIN32
FILE *fp; FILE *fp;
struct config_param *param =
parseConfigFilePath(CONF_PID_FILE, 0);
int pid, ret; int pid, ret;
if (param == NULL) if (pidfile == NULL)
g_error("no pid_file specified in the config file"); g_error("no pid_file specified in the config file");
fp = fopen(param->value, "r"); fp = fopen(pidfile, "r");
if (fp == NULL) if (fp == NULL)
g_error("unable to open %s \"%s\": %s", g_error("unable to open pid file \"%s\": %s",
CONF_PID_FILE, param->value, g_strerror(errno)); pidfile, g_strerror(errno));
if (fscanf(fp, "%i", &pid) != 1) { if (fscanf(fp, "%i", &pid) != 1) {
g_error("unable to read the pid from file \"%s\"", g_error("unable to read the pid from file \"%s\"",
param->value); pidfile);
} }
fclose(fp); fclose(fp);
@ -85,36 +99,29 @@ void
daemonize_set_user(void) daemonize_set_user(void)
{ {
#ifndef WIN32 #ifndef WIN32
struct config_param *param = config_get_param(CONF_USER); if (user_name != NULL) {
if (param && strlen(param->value)) {
/* get uid */ /* get uid */
struct passwd *userpwd; struct passwd *userpwd;
if ((userpwd = getpwnam(param->value)) == NULL) {
g_error("no such user \"%s\" at line %i",
param->value, param->line);
}
if (setgid(userpwd->pw_gid) == -1) { if (setgid(user_gid) == -1) {
g_error("cannot setgid for user \"%s\" at line %i: %s", g_error("cannot setgid for user \"%s\": %s",
param->value, param->line, strerror(errno)); user_name, g_strerror(errno));
} }
#ifdef _BSD_SOURCE #ifdef _BSD_SOURCE
/* init suplementary groups /* init suplementary groups
* (must be done before we change our uid) * (must be done before we change our uid)
*/ */
if (initgroups(param->value, userpwd->pw_gid) == -1) { if (initgroups(user_name, user_gid) == -1) {
g_warning("cannot init supplementary groups " g_warning("cannot init supplementary groups "
"of user \"%s\" at line %i: %s", "of user \"%s\": %s",
param->value, param->line, strerror(errno)); user_name, g_strerror(errno));
} }
#endif #endif
/* set uid */ /* set uid */
if (setuid(userpwd->pw_uid) == -1) { if (setuid(userpwd->pw_uid) == -1) {
g_error("cannot change to uid of user " g_error("cannot change to uid of user \"%s\": %s",
"\"%s\" at line %i: %s", user_name, g_strerror(errno));
param->value, param->line, strerror(errno));
} }
/* this is needed by libs such as arts */ /* this is needed by libs such as arts */
@ -130,18 +137,15 @@ daemonize(Options *options)
{ {
#ifndef WIN32 #ifndef WIN32
FILE *fp = NULL; FILE *fp = NULL;
struct config_param *pidFileParam =
parseConfigFilePath(CONF_PID_FILE, 0);
if (pidFileParam) { if (pidfile != NULL) {
/* do this before daemon'izing so we can fail gracefully if we can't /* do this before daemon'izing so we can fail gracefully if we can't
* write to the pid file */ * write to the pid file */
g_debug("opening pid file"); g_debug("opening pid file");
fp = fopen(pidFileParam->value, "w+"); fp = fopen(pidfile, "w+");
if (!fp) { if (!fp) {
g_error("could not open %s \"%s\" (at line %i) for writing: %s", g_error("could not create pid file \"%s\": %s",
CONF_PID_FILE, pidFileParam->value, pidfile, g_strerror(errno));
pidFileParam->line, strerror(errno));
} }
} }
@ -165,7 +169,7 @@ daemonize(Options *options)
g_debug("daemonized!"); g_debug("daemonized!");
} }
if (pidFileParam) { if (pidfile != NULL) {
g_debug("writing pid file"); g_debug("writing pid file");
fprintf(fp, "%lu\n", (unsigned long)getpid()); fprintf(fp, "%lu\n", (unsigned long)getpid());
fclose(fp); fclose(fp);
@ -177,14 +181,34 @@ daemonize(Options *options)
} }
void void
daemonize_delete_pidfile(void) daemonize_init(const char *user, const char *_pidfile)
{ {
struct config_param *param = parseConfigFilePath(CONF_PID_FILE, 0); #ifndef WIN32
user_name = g_strdup(user);
if (user_name != NULL) {
struct passwd *pwd = getpwnam(user_name);
if (pwd == NULL)
g_error("no such user \"%s\"", user_name);
if (param == NULL) user_uid = pwd->pw_uid;
return; user_gid = pwd->pw_gid;
}
g_debug("cleaning up pid file");
pidfile = g_strdup(_pidfile);
unlink(param->value); #else
(void)user;
(void)_pidfile;
#endif
}
void
daemonize_finish(void)
{
#ifndef WIN32
if (pidfile != NULL)
unlink(pidfile);
g_free(user_name);
g_free(pidfile);
#endif
} }

View File

@ -21,6 +21,12 @@
#include "cmdline.h" #include "cmdline.h"
void
daemonize_init(const char *user, const char *pidfile);
void
daemonize_finish(void);
/** /**
* Kill the MPD which is currently running, pid determined from the * Kill the MPD which is currently running, pid determined from the
* pid file. * pid file.
@ -43,10 +49,4 @@ daemonize_set_user(void);
void void
daemonize(Options *options); daemonize(Options *options);
/**
* Deletes the pidfile which was created when MPD started.
*/
void
daemonize_delete_pidfile(void);
#endif #endif

View File

@ -201,6 +201,9 @@ int main(int argc, char *argv[])
parseOptions(argc, argv, &options); parseOptions(argc, argv, &options);
daemonize_init(config_get_string(CONF_USER, NULL),
config_get_path(CONF_PID_FILE));
if (options.kill) if (options.kill)
daemonize_kill(); daemonize_kill();
@ -295,13 +298,13 @@ int main(int argc, char *argv[])
archive_plugin_deinit_all(); archive_plugin_deinit_all();
#endif #endif
music_pipe_free(); music_pipe_free();
daemonize_delete_pidfile();
config_global_finish(); config_global_finish();
tag_pool_deinit(); tag_pool_deinit();
songvec_deinit(); songvec_deinit();
dirvec_deinit(); dirvec_deinit();
idle_deinit(); idle_deinit();
stats_global_finish(); stats_global_finish();
daemonize_finish();
close_log_files(); close_log_files();
return EXIT_SUCCESS; return EXIT_SUCCESS;