~ubuntu-dev/ubuntu/lucid/mpd/lucid-201002101854

« back to all changes in this revision

Viewing changes to src/daemon.c

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2009-10-23 12:38:20 UTC
  • mfrom: (1.1.12 upstream) (2.2.6 sid)
  • Revision ID: james.westby@ubuntu.com-20091023123820-rxt21lmekscxbkbt
Tags: 0.15.4-1ubuntu1
* Merge from debian unstable, Ubuntu remaining changes:
  - debian/control:
    + Don't build-depends on libmikmod2-dev (Debian bug #510675).
    + Move avahi-daemon from Suggests field to Recommends field.
  - debian/mpd.init.d:
    + Read mpd user from mpd.conf.
* Also fixes LP: #332332.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2003-2009 The Music Player Daemon Project
 
3
 * http://www.musicpd.org
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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.
 
18
 */
 
19
 
 
20
#include "daemon.h"
 
21
 
 
22
#include <glib.h>
 
23
 
 
24
#include <stdio.h>
 
25
#include <stdlib.h>
 
26
#include <unistd.h>
 
27
#include <errno.h>
 
28
#include <string.h>
 
29
#include <sys/types.h>
 
30
#include <sys/stat.h>
 
31
#include <fcntl.h>
 
32
 
 
33
#ifndef WIN32
 
34
#include <signal.h>
 
35
#include <pwd.h>
 
36
#include <grp.h>
 
37
#endif
 
38
 
 
39
#undef G_LOG_DOMAIN
 
40
#define G_LOG_DOMAIN "daemon"
 
41
 
 
42
#ifndef WIN32
 
43
 
 
44
/** the Unix user name which MPD runs as */
 
45
static char *user_name;
 
46
 
 
47
/** the Unix user id which MPD runs as */
 
48
static uid_t user_uid;
 
49
 
 
50
/** the Unix group id which MPD runs as */
 
51
static gid_t user_gid;
 
52
 
 
53
/** the absolute path of the pidfile */
 
54
static char *pidfile;
 
55
 
 
56
#endif
 
57
 
 
58
void
 
59
daemonize_kill(void)
 
60
{
 
61
#ifndef WIN32
 
62
        FILE *fp;
 
63
        int pid, ret;
 
64
 
 
65
        if (pidfile == NULL)
 
66
                g_error("no pid_file specified in the config file");
 
67
 
 
68
        fp = fopen(pidfile, "r");
 
69
        if (fp == NULL)
 
70
                g_error("unable to open pid file \"%s\": %s",
 
71
                        pidfile, g_strerror(errno));
 
72
 
 
73
        if (fscanf(fp, "%i", &pid) != 1) {
 
74
                g_error("unable to read the pid from file \"%s\"",
 
75
                        pidfile);
 
76
        }
 
77
        fclose(fp);
 
78
 
 
79
        ret = kill(pid, SIGTERM);
 
80
        if (ret < 0)
 
81
                g_error("unable to kill proccess %i: %s",
 
82
                        pid, g_strerror(errno));
 
83
 
 
84
        exit(EXIT_SUCCESS);
 
85
#else
 
86
        g_error("--kill is not available on WIN32");
 
87
#endif
 
88
}
 
89
 
 
90
void
 
91
daemonize_close_stdin(void)
 
92
{
 
93
        int fd = open("/dev/null", O_RDONLY);
 
94
 
 
95
        if (fd < 0)
 
96
                close(STDIN_FILENO);
 
97
        else if (fd != STDIN_FILENO) {
 
98
                dup2(fd, STDIN_FILENO);
 
99
                close(fd);
 
100
        }
 
101
}
 
102
 
 
103
void
 
104
daemonize_set_user(void)
 
105
{
 
106
#ifndef WIN32
 
107
        if (user_name == NULL)
 
108
                return;
 
109
 
 
110
        /* get uid */
 
111
        if (setgid(user_gid) == -1) {
 
112
                g_error("cannot setgid for user \"%s\": %s",
 
113
                        user_name, g_strerror(errno));
 
114
        }
 
115
#ifdef _BSD_SOURCE
 
116
        /* init suplementary groups
 
117
         * (must be done before we change our uid)
 
118
         */
 
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));
 
123
        }
 
124
#endif
 
125
 
 
126
        /* set uid */
 
127
        if (setuid(user_uid) == -1) {
 
128
                g_error("cannot change to uid of user \"%s\": %s",
 
129
                        user_name, g_strerror(errno));
 
130
        }
 
131
#endif
 
132
}
 
133
 
 
134
#ifndef G_OS_WIN32
 
135
static void
 
136
daemonize_detach(void)
 
137
{
 
138
        pid_t pid;
 
139
 
 
140
        /* flush all file handles before duplicating the buffers */
 
141
 
 
142
        fflush(NULL);
 
143
 
 
144
        /* detach from parent process */
 
145
 
 
146
        pid = fork();
 
147
        if (pid < 0)
 
148
                g_error("fork() failed: %s", g_strerror(errno));
 
149
 
 
150
        if (pid > 0)
 
151
                /* exit the parent process */
 
152
                _exit(EXIT_SUCCESS);
 
153
 
 
154
        /* release the current working directory */
 
155
 
 
156
        if (chdir("/") < 0)
 
157
                g_error("problems changing to root directory");
 
158
 
 
159
        /* detach from the current session */
 
160
 
 
161
        setsid();
 
162
 
 
163
        g_debug("daemonized!");
 
164
}
 
165
#endif
 
166
 
 
167
void
 
168
daemonize(bool detach)
 
169
{
 
170
#ifndef WIN32
 
171
        FILE *fp = NULL;
 
172
 
 
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+");
 
178
                if (!fp) {
 
179
                        g_error("could not create pid file \"%s\": %s",
 
180
                                pidfile, g_strerror(errno));
 
181
                }
 
182
        }
 
183
 
 
184
        if (detach)
 
185
                daemonize_detach();
 
186
 
 
187
        if (pidfile != NULL) {
 
188
                g_debug("writing pid file");
 
189
                fprintf(fp, "%lu\n", (unsigned long)getpid());
 
190
                fclose(fp);
 
191
        }
 
192
#else
 
193
        /* no daemonization on WIN32 */
 
194
        (void)detach;
 
195
#endif
 
196
}
 
197
 
 
198
void
 
199
daemonize_init(const char *user, const char *_pidfile)
 
200
{
 
201
#ifndef WIN32
 
202
        if (user != NULL && strcmp(user, g_get_user_name()) != 0) {
 
203
                struct passwd *pwd;
 
204
 
 
205
                user_name = g_strdup(user);
 
206
 
 
207
                pwd = getpwnam(user_name);
 
208
                if (pwd == NULL)
 
209
                        g_error("no such user \"%s\"", user_name);
 
210
 
 
211
                user_uid = pwd->pw_uid;
 
212
                user_gid = pwd->pw_gid;
 
213
 
 
214
                /* this is needed by libs such as arts */
 
215
                g_setenv("HOME", pwd->pw_dir, true);
 
216
        }
 
217
 
 
218
        pidfile = g_strdup(_pidfile);
 
219
#else
 
220
        (void)user;
 
221
        (void)_pidfile;
 
222
#endif
 
223
}
 
224
 
 
225
void
 
226
daemonize_finish(void)
 
227
{
 
228
#ifndef WIN32
 
229
        if (pidfile != NULL)
 
230
                unlink(pidfile);
 
231
 
 
232
        g_free(user_name);
 
233
        g_free(pidfile);
 
234
#endif
 
235
}