File: daemon-posix.c

package info (click to toggle)
transmission 3.00-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 33,160 kB
  • sloc: ansic: 81,055; objc: 18,449; cpp: 16,303; sh: 4,470; javascript: 4,281; makefile: 1,081; xml: 139
file content (225 lines) | stat: -rw-r--r-- 4,498 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 * This file Copyright (C) 2015 Mnemosyne LLC
 *
 * It may be used under the GNU GPL versions 2 or 3
 * or any future license endorsed by Mnemosyne LLC.
 *
 */

#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <stdlib.h> /* abort(), daemon(), exit() */
#include <fcntl.h> /* open() */
#include <unistd.h> /* fork(), setsid(), chdir(), dup2(), close(), pipe() */

#include <libtransmission/transmission.h>
#include <libtransmission/error.h>
#include <libtransmission/utils.h>

#include "daemon.h"

/***
****
***/

static dtr_callbacks const* callbacks = NULL;
static void* callback_arg = NULL;

static int signal_pipe[2];

/***
****
***/

static void set_system_error(tr_error** error, int code, char const* message)
{
    tr_error_set(error, code, "%s (%d): %s", message, code, tr_strerror(code));
}

/***
****
***/

static void handle_signal(int sig)
{
    switch (sig)
    {
    case SIGHUP:
        callbacks->on_reconfigure(callback_arg);
        break;

    case SIGINT:
    case SIGTERM:
        callbacks->on_stop(callback_arg);
        break;

    default:
        assert(!"Unexpected signal");
    }
}

static void send_signal_to_pipe(int sig)
{
    int const old_errno = errno;

    if (write(signal_pipe[1], &sig, sizeof(sig)) == -1)
    {
        abort();
    }

    errno = old_errno;
}

static void* signal_handler_thread_main(void* arg UNUSED)
{
    int sig;

    while (read(signal_pipe[0], &sig, sizeof(sig)) == sizeof(sig) && sig != 0)
    {
        handle_signal(sig);
    }

    return NULL;
}

static bool create_signal_pipe(tr_error** error)
{
    if (pipe(signal_pipe) == -1)
    {
        set_system_error(error, errno, "pipe() failed");
        return false;
    }

    return true;
}

static void destroy_signal_pipe(void)
{
    close(signal_pipe[0]);
    close(signal_pipe[1]);
}

static bool create_signal_handler_thread(pthread_t* thread, tr_error** error)
{
    if (!create_signal_pipe(error))
    {
        return false;
    }

    if ((errno = pthread_create(thread, NULL, &signal_handler_thread_main, NULL)) != 0)
    {
        set_system_error(error, errno, "pthread_create() failed");
        destroy_signal_pipe();
        return false;
    }

    return true;
}

static void destroy_signal_handler_thread(pthread_t thread)
{
    send_signal_to_pipe(0);
    pthread_join(thread, NULL);

    destroy_signal_pipe();
}

static bool setup_signal_handler(int sig, tr_error** error)
{
    assert(sig != 0);

    if (signal(sig, &send_signal_to_pipe) == SIG_ERR)
    {
        set_system_error(error, errno, "signal() failed");
        return false;
    }

    return true;
}

/***
****
***/

bool dtr_daemon(dtr_callbacks const* cb, void* cb_arg, bool foreground, int* exit_code, tr_error** error)
{
    callbacks = cb;
    callback_arg = cb_arg;

    *exit_code = 1;

    if (!foreground)
    {
#if defined(HAVE_DAEMON) && !defined(__APPLE__) && !defined(__UCLIBC__)

        if (daemon(true, false) == -1)
        {
            set_system_error(error, errno, "daemon() failed");
            return false;
        }

#else

        /* this is loosely based off of glibc's daemon() implementation
         * http://sourceware.org/git/?p=glibc.git;a=blob_plain;f=misc/daemon.c */

        switch (fork())
        {
        case -1:
            set_system_error(error, errno, "fork() failed");
            return false;

        case 0:
            break;

        default:
            *exit_code = 0;
            return true;
        }

        if (setsid() == -1)
        {
            set_system_error(error, errno, "setsid() failed");
            return false;
        }

        /*
        if (chdir("/") == -1)
        {
            set_system_error(error, errno, "chdir() failed");
            return false;
        }
        */

        {
            int const fd = open("/dev/null", O_RDWR, 0);
            dup2(fd, STDIN_FILENO);
            dup2(fd, STDOUT_FILENO);
            dup2(fd, STDERR_FILENO);
            close(fd);
        }

#endif
    }

    pthread_t signal_thread;

    if (!create_signal_handler_thread(&signal_thread, error))
    {
        return false;
    }

    if (!setup_signal_handler(SIGINT, error) || !setup_signal_handler(SIGTERM, error) || !setup_signal_handler(SIGHUP, error))
    {
        destroy_signal_handler_thread(signal_thread);
        return false;
    }

    *exit_code = cb->on_start(cb_arg, foreground);

    destroy_signal_handler_thread(signal_thread);

    return true;
}