Branch data Line data Source code
1 : : /* stripped down version */
2 : : /***
3 : : Copyright 2010 Lennart Poettering
4 : :
5 : : Permission is hereby granted, free of charge, to any person
6 : : obtaining a copy of this software and associated documentation files
7 : : (the "Software"), to deal in the Software without restriction,
8 : : including without limitation the rights to use, copy, modify, merge,
9 : : publish, distribute, sublicense, and/or sell copies of the Software,
10 : : and to permit persons to whom the Software is furnished to do so,
11 : : subject to the following conditions:
12 : :
13 : : The above copyright notice and this permission notice shall be
14 : : included in all copies or substantial portions of the Software.
15 : :
16 : : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 : : EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 : : MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 : : NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 : : BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 : : ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 : : CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 : : SOFTWARE.
24 : : ***/
25 : :
26 : : #ifndef _GNU_SOURCE
27 : : # define _GNU_SOURCE
28 : : #endif
29 : :
30 : : #include <stdlib.h>
31 : : #include <unistd.h>
32 : : #include <fcntl.h>
33 : : #include <errno.h>
34 : :
35 : : #include "sd-daemon.h"
36 : :
37 : : #if (__GNUC__ >= 4)
38 : : # ifdef SD_EXPORT_SYMBOLS
39 : : /* Export symbols */
40 : : # define _sd_export_ __attribute__ ((visibility("default")))
41 : : # else
42 : : /* Don't export the symbols */
43 : : # define _sd_export_ __attribute__ ((visibility("hidden")))
44 : : # endif
45 : : #else
46 : : # define _sd_export_
47 : : #endif
48 : :
49 : 0 : _sd_export_ int sd_listen_fds(int unset_environment) {
50 : :
51 : : #if defined(DISABLE_SYSTEMD) || !defined(__linux__)
52 : : return 0;
53 : : #else
54 : : int r, fd;
55 : : const char *e;
56 : 0 : char *p = NULL;
57 : : unsigned long l;
58 : :
59 : 0 : e = getenv("LISTEN_PID");
60 [ # # ]: 0 : if (!e) {
61 : : r = 0;
62 : : goto finish;
63 : : }
64 : :
65 : 0 : errno = 0;
66 : 0 : l = strtoul(e, &p, 10);
67 : :
68 [ # # ]: 0 : if (errno > 0) {
69 : 0 : r = -errno;
70 : 0 : goto finish;
71 : : }
72 : :
73 [ # # ][ # # ]: 0 : if (!p || p == e || *p || l <= 0) {
[ # # ][ # # ]
74 : : r = -EINVAL;
75 : : goto finish;
76 : : }
77 : :
78 : : /* Is this for us? */
79 [ # # ]: 0 : if (getpid() != (pid_t) l) {
80 : : r = 0;
81 : : goto finish;
82 : : }
83 : :
84 : 0 : e = getenv("LISTEN_FDS");
85 [ # # ]: 0 : if (!e) {
86 : : r = 0;
87 : : goto finish;
88 : : }
89 : :
90 : 0 : errno = 0;
91 : 0 : l = strtoul(e, &p, 10);
92 : :
93 [ # # ]: 0 : if (errno > 0) {
94 : 0 : r = -errno;
95 : 0 : goto finish;
96 : : }
97 : :
98 [ # # ][ # # ]: 0 : if (!p || p == e || *p) {
[ # # ]
99 : : r = -EINVAL;
100 : : goto finish;
101 : : }
102 : :
103 [ # # ]: 0 : for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + (int) l; fd ++) {
104 : : int flags;
105 : :
106 : 0 : flags = fcntl(fd, F_GETFD);
107 [ # # ]: 0 : if (flags < 0) {
108 : 0 : r = -errno;
109 : 0 : goto finish;
110 : : }
111 : :
112 [ # # ]: 0 : if (flags & FD_CLOEXEC)
113 : 0 : continue;
114 : :
115 [ # # ]: 0 : if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
116 : 0 : r = -errno;
117 : 0 : goto finish;
118 : : }
119 : : }
120 : :
121 : : r = (int) l;
122 : :
123 : : finish:
124 [ # # ]: 0 : if (unset_environment) {
125 : 0 : unsetenv("LISTEN_PID");
126 : 0 : unsetenv("LISTEN_FDS");
127 : : }
128 : :
129 : 0 : return r;
130 : : #endif
131 : : }
132 : :
|