2
* Copyright (C) 2003-2009 The Music Player Daemon Project
3
* http://www.musicpd.org
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License along
16
* with this program; if not, write to the Free Software Foundation, Inc.,
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29
#include <sys/types.h>
40
#define G_LOG_DOMAIN "daemon"
44
/** the Unix user name which MPD runs as */
45
static char *user_name;
47
/** the Unix user id which MPD runs as */
48
static uid_t user_uid;
50
/** the Unix group id which MPD runs as */
51
static gid_t user_gid;
53
/** the absolute path of the pidfile */
66
g_error("no pid_file specified in the config file");
68
fp = fopen(pidfile, "r");
70
g_error("unable to open pid file \"%s\": %s",
71
pidfile, g_strerror(errno));
73
if (fscanf(fp, "%i", &pid) != 1) {
74
g_error("unable to read the pid from file \"%s\"",
79
ret = kill(pid, SIGTERM);
81
g_error("unable to kill proccess %i: %s",
82
pid, g_strerror(errno));
86
g_error("--kill is not available on WIN32");
91
daemonize_close_stdin(void)
93
int fd = open("/dev/null", O_RDONLY);
97
else if (fd != STDIN_FILENO) {
98
dup2(fd, STDIN_FILENO);
104
daemonize_set_user(void)
107
if (user_name == NULL)
111
if (setgid(user_gid) == -1) {
112
g_error("cannot setgid for user \"%s\": %s",
113
user_name, g_strerror(errno));
116
/* init suplementary groups
117
* (must be done before we change our uid)
119
if (initgroups(user_name, user_gid) == -1) {
120
g_warning("cannot init supplementary groups "
121
"of user \"%s\": %s",
122
user_name, g_strerror(errno));
127
if (setuid(user_uid) == -1) {
128
g_error("cannot change to uid of user \"%s\": %s",
129
user_name, g_strerror(errno));
136
daemonize_detach(void)
140
/* flush all file handles before duplicating the buffers */
144
/* detach from parent process */
148
g_error("fork() failed: %s", g_strerror(errno));
151
/* exit the parent process */
154
/* release the current working directory */
157
g_error("problems changing to root directory");
159
/* detach from the current session */
163
g_debug("daemonized!");
168
daemonize(bool detach)
173
if (pidfile != NULL) {
174
/* do this before daemon'izing so we can fail gracefully if we can't
175
* write to the pid file */
176
g_debug("opening pid file");
177
fp = fopen(pidfile, "w+");
179
g_error("could not create pid file \"%s\": %s",
180
pidfile, g_strerror(errno));
187
if (pidfile != NULL) {
188
g_debug("writing pid file");
189
fprintf(fp, "%lu\n", (unsigned long)getpid());
193
/* no daemonization on WIN32 */
199
daemonize_init(const char *user, const char *_pidfile)
202
if (user != NULL && strcmp(user, g_get_user_name()) != 0) {
205
user_name = g_strdup(user);
207
pwd = getpwnam(user_name);
209
g_error("no such user \"%s\"", user_name);
211
user_uid = pwd->pw_uid;
212
user_gid = pwd->pw_gid;
214
/* this is needed by libs such as arts */
215
g_setenv("HOME", pwd->pw_dir, true);
218
pidfile = g_strdup(_pidfile);
226
daemonize_finish(void)