chiark / gitweb /
man: replace syslog name in man page by configured name
[elogind.git] / src / sd-daemon.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 /***
4   Copyright 2010 Lennart Poettering
5
6   Permission is hereby granted, free of charge, to any person
7   obtaining a copy of this software and associated documentation files
8   (the "Software"), to deal in the Software without restriction,
9   including without limitation the rights to use, copy, modify, merge,
10   publish, distribute, sublicense, and/or sell copies of the Software,
11   and to permit persons to whom the Software is furnished to do so,
12   subject to the following conditions:
13
14   The above copyright notice and this permission notice shall be
15   included in all copies or substantial portions of the Software.
16
17   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   SOFTWARE.
25 ***/
26
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31
32 #include "sd-daemon.h"
33
34 int sd_listen_fds(int unset_environment) {
35
36 #ifdef DISABLE_SYSTEMD
37         return 0;
38 #else
39         int r;
40         const char *e;
41         char *p = NULL;
42         unsigned long l;
43
44         if (!(e = getenv("LISTEN_PID"))) {
45                 r = 0;
46                 goto finish;
47         }
48
49         errno = 0;
50         l = strtoul(e, &p, 10);
51
52         if (errno != 0) {
53                 r = -errno;
54                 goto finish;
55         }
56
57         if (!p || *p || l <= 0) {
58                 r = -EINVAL;
59                 goto finish;
60         }
61
62         /* Is this for us? */
63         if (getpid() != (pid_t) l) {
64                 r = 0;
65                 goto finish;
66         }
67
68         if (!(e = getenv("LISTEN_FDS"))) {
69                 r = 0;
70                 goto finish;
71         }
72
73         errno = 0;
74         l = strtoul(e, &p, 10);
75
76         if (errno != 0) {
77                 r = -errno;
78                 goto finish;
79         }
80
81         if (!p || *p) {
82                 r = -EINVAL;
83                 goto finish;
84         }
85
86         r = (int) l;
87
88 finish:
89         if (unset_environment) {
90                 unsetenv("LISTEN_PID");
91                 unsetenv("LISTEN_FDS");
92         }
93
94         return r;
95 #endif
96 }