View | Details | Raw Unified | Return to bug 656104
Collapse All | Expand All

(-)a/ChangeLog (+2 lines)
Lines 1-3 Link Here
1
- acquire /dev/log socket optionally from systemd
2
  thanks to Lennart Poettering for this patch
1
---------------------------------------------------------------------------
3
---------------------------------------------------------------------------
2
Version 5.6.2  [V5-STABLE] (rgerhards), 2010-11-30
4
Version 5.6.2  [V5-STABLE] (rgerhards), 2010-11-30
3
- bugfix: compile failed on systems without epoll_create1()
5
- bugfix: compile failed on systems without epoll_create1()
(-)a/plugins/imuxsock/Makefile.am (-2 / +2 lines)
Lines 1-6 Link Here
1
pkglib_LTLIBRARIES = imuxsock.la
1
pkglib_LTLIBRARIES = imuxsock.la
2
2
3
imuxsock_la_SOURCES = imuxsock.c
3
imuxsock_la_SOURCES = imuxsock.c
4
imuxsock_la_CPPFLAGS = -I$(top_srcdir) $(PTHREADS_CFLAGS) $(RSRT_CFLAGS)
4
imuxsock_la_CPPFLAGS = -DSD_EXPORT_SYMBOLS -I$(top_srcdir) $(PTHREADS_CFLAGS) $(RSRT_CFLAGS)
5
imuxsock_la_LDFLAGS = -module -avoid-version
5
imuxsock_la_LDFLAGS = -module -avoid-version
6
imuxsock_la_LIBADD = 
6
imuxsock_la_LIBADD = $(RSRT_LIBS)
(-)a/plugins/imuxsock/imuxsock.c (-2 / +42 lines)
Lines 47-52 Link Here
47
#include "prop.h"
47
#include "prop.h"
48
#include "debug.h"
48
#include "debug.h"
49
#include "unlimited_select.h"
49
#include "unlimited_select.h"
50
#include "sd-daemon.h"
50
51
51
MODULE_TYPE_INPUT
52
MODULE_TYPE_INPUT
52
53
Lines 190-195 static int create_unix_socket(const char *path, int bCreatePath) Link Here
190
	if (path[0] == '\0')
191
	if (path[0] == '\0')
191
		return -1;
192
		return -1;
192
193
194
       if (strcmp(path, _PATH_LOG) == 0) {
195
               int r;
196
197
               /* Check whether an FD was passed in from systemd. If
198
                * so, it's the /dev/log socket, so use it. */
199
200
               r = sd_listen_fds(0);
201
               if (r < 0) {
202
                       errmsg.LogError(-r, NO_ERRCODE, "Failed to acquire systemd socket");
203
                       return -1;
204
               }
205
206
               if (r > 1) {
207
                       errmsg.LogError(EINVAL, NO_ERRCODE, "Wrong number of systemd sockets passed");
208
                       return -1;
209
               }
210
211
               if (r == 1) {
212
                       fd = SD_LISTEN_FDS_START;
213
                       r = sd_is_socket_unix(fd, SOCK_DGRAM, -1, _PATH_LOG, 0);
214
                       if (r < 0) {
215
                               errmsg.LogError(-r, NO_ERRCODE, "Failed to verify systemd socket type");
216
                               return -1;
217
                       }
218
219
                       if (!r) {
220
                               errmsg.LogError(EINVAL, NO_ERRCODE, "Passed systemd socket of wrong type");
221
                               return -1;
222
                       }
223
224
                       return fd;
225
               }
226
       }
227
193
	unlink(path);
228
	unlink(path);
194
229
195
	memset(&sunx, 0, sizeof(sunx));
230
	memset(&sunx, 0, sizeof(sunx));
Lines 395-402 CODESTARTafterRun Link Here
395
		if (funix[i] != -1)
430
		if (funix[i] != -1)
396
			close(funix[i]);
431
			close(funix[i]);
397
432
398
	/* Clean-up files. */
433
       /* Clean-up files. If systemd passed us a socket it is
399
	for(i = startIndexUxLocalSockets; i < nfunix; i++)
434
        * systemd's job to clean it up.*/
435
       if (sd_listen_fds(0) > 0)
436
               i = 1;
437
       else
438
               i = startIndexUxLocalSockets;
439
       for(; i < nfunix; i++)
400
		if (funixn[i] && funix[i] != -1)
440
		if (funixn[i] && funix[i] != -1)
401
			unlink((char*) funixn[i]);
441
			unlink((char*) funixn[i]);
402
	/* free no longer needed string */
442
	/* free no longer needed string */
(-)a/runtime/Makefile.am (-2 / +7 lines)
Lines 82-87 librsyslog_la_SOURCES = \ Link Here
82
	prop.h \
82
	prop.h \
83
	cfsysline.c \
83
	cfsysline.c \
84
	cfsysline.h \
84
	cfsysline.h \
85
       sd-daemon.c \
86
       sd-daemon.h \
85
	\
87
	\
86
	\
88
	\
87
	../action.h \
89
	../action.h \
Lines 100-108 librsyslog_la_SOURCES = \ Link Here
100
# runtime or will no longer be needed. -- rgerhards, 2008-06-13
102
# runtime or will no longer be needed. -- rgerhards, 2008-06-13
101
103
102
if WITH_MODDIRS
104
if WITH_MODDIRS
103
librsyslog_la_CPPFLAGS = -D_PATH_MODDIR=\"$(pkglibdir)/:$(moddirs)\" $(PTHREADS_CFLAGS)
105
librsyslog_la_CPPFLAGS = -DSD_EXPORT_SYMBOLS -D_PATH_MODDIR=\"$(pkglibdir)/:$(moddirs)\" $(PTHREADS_CFLAGS)
104
else
106
else
105
librsyslog_la_CPPFLAGS = -D_PATH_MODDIR=\"$(pkglibdir)/\" -I$(top_srcdir) $(PTHREADS_CFLAGS)
107
librsyslog_la_CPPFLAGS = -DSD_EXPORT_SYMBOLS -D_PATH_MODDIR=\"$(pkglibdir)/\" -I$(top_srcdir) $(PTHREADS_CFLAGS)
106
endif
108
endif
107
#librsyslog_la_LDFLAGS = -module -avoid-version
109
#librsyslog_la_LDFLAGS = -module -avoid-version
108
librsyslog_la_LIBADD =  $(DL_LIBS) $(RT_LIBS)
110
librsyslog_la_LIBADD =  $(DL_LIBS) $(RT_LIBS)
Lines 178-180 lmnsd_gtls_la_LDFLAGS = -module -avoid-version Link Here
178
lmnsd_gtls_la_LIBADD = $(GNUTLS_LIBS)
180
lmnsd_gtls_la_LIBADD = $(GNUTLS_LIBS)
179
endif
181
endif
180
182
183
update-systemd:
184
       curl http://cgit.freedesktop.org/systemd/plain/src/sd-daemon.c > sd-daemon.c
185
       curl http://cgit.freedesktop.org/systemd/plain/src/sd-daemon.h > sd-daemon.h
(-)a/runtime/sd-daemon.c (+435 lines)
Line 0 Link Here
1
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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
#ifndef _GNU_SOURCE
28
#define _GNU_SOURCE
29
#endif
30
31
#include <sys/types.h>
32
#include <sys/stat.h>
33
#include <sys/socket.h>
34
#include <sys/un.h>
35
#include <sys/fcntl.h>
36
#include <netinet/in.h>
37
#include <stdlib.h>
38
#include <errno.h>
39
#include <unistd.h>
40
#include <string.h>
41
#include <stdarg.h>
42
#include <stdio.h>
43
44
#include "sd-daemon.h"
45
46
int sd_listen_fds(int unset_environment) {
47
48
#if defined(DISABLE_SYSTEMD) || !defined(__linux__)
49
        return 0;
50
#else
51
        int r, fd;
52
        const char *e;
53
        char *p = NULL;
54
        unsigned long l;
55
56
        if (!(e = getenv("LISTEN_PID"))) {
57
                r = 0;
58
                goto finish;
59
        }
60
61
        errno = 0;
62
        l = strtoul(e, &p, 10);
63
64
        if (errno != 0) {
65
                r = -errno;
66
                goto finish;
67
        }
68
69
        if (!p || *p || l <= 0) {
70
                r = -EINVAL;
71
                goto finish;
72
        }
73
74
        /* Is this for us? */
75
        if (getpid() != (pid_t) l) {
76
                r = 0;
77
                goto finish;
78
        }
79
80
        if (!(e = getenv("LISTEN_FDS"))) {
81
                r = 0;
82
                goto finish;
83
        }
84
85
        errno = 0;
86
        l = strtoul(e, &p, 10);
87
88
        if (errno != 0) {
89
                r = -errno;
90
                goto finish;
91
        }
92
93
        if (!p || *p) {
94
                r = -EINVAL;
95
                goto finish;
96
        }
97
98
        for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + (int) l; fd ++) {
99
                int flags;
100
101
                if ((flags = fcntl(fd, F_GETFD)) < 0) {
102
                        r = -errno;
103
                        goto finish;
104
                }
105
106
                if (flags & FD_CLOEXEC)
107
                        continue;
108
109
                if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
110
                        r = -errno;
111
                        goto finish;
112
                }
113
        }
114
115
        r = (int) l;
116
117
finish:
118
        if (unset_environment) {
119
                unsetenv("LISTEN_PID");
120
                unsetenv("LISTEN_FDS");
121
        }
122
123
        return r;
124
#endif
125
}
126
127
int sd_is_fifo(int fd, const char *path) {
128
        struct stat st_fd;
129
130
        if (fd < 0)
131
                return -EINVAL;
132
133
        memset(&st_fd, 0, sizeof(st_fd));
134
        if (fstat(fd, &st_fd) < 0)
135
                return -errno;
136
137
        if (!S_ISFIFO(st_fd.st_mode))
138
                return 0;
139
140
        if (path) {
141
                struct stat st_path;
142
143
                memset(&st_path, 0, sizeof(st_path));
144
                if (stat(path, &st_path) < 0) {
145
146
                        if (errno == ENOENT || errno == ENOTDIR)
147
                                return 0;
148
149
                        return -errno;
150
                }
151
152
                return
153
                        st_path.st_dev == st_fd.st_dev &&
154
                        st_path.st_ino == st_fd.st_ino;
155
        }
156
157
        return 1;
158
}
159
160
static int sd_is_socket_internal(int fd, int type, int listening) {
161
        struct stat st_fd;
162
163
        if (fd < 0 || type < 0)
164
                return -EINVAL;
165
166
        if (fstat(fd, &st_fd) < 0)
167
                return -errno;
168
169
        if (!S_ISSOCK(st_fd.st_mode))
170
                return 0;
171
172
        if (type != 0) {
173
                int other_type = 0;
174
                socklen_t l = sizeof(other_type);
175
176
                if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &other_type, &l) < 0)
177
                        return -errno;
178
179
                if (l != sizeof(other_type))
180
                        return -EINVAL;
181
182
                if (other_type != type)
183
                        return 0;
184
        }
185
186
        if (listening >= 0) {
187
                int accepting = 0;
188
                socklen_t l = sizeof(accepting);
189
190
                if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &accepting, &l) < 0)
191
                        return -errno;
192
193
                if (l != sizeof(accepting))
194
                        return -EINVAL;
195
196
                if (!accepting != !listening)
197
                        return 0;
198
        }
199
200
        return 1;
201
}
202
203
union sockaddr_union {
204
        struct sockaddr sa;
205
        struct sockaddr_in in4;
206
        struct sockaddr_in6 in6;
207
        struct sockaddr_un un;
208
        struct sockaddr_storage storage;
209
};
210
211
int sd_is_socket(int fd, int family, int type, int listening) {
212
        int r;
213
214
        if (family < 0)
215
                return -EINVAL;
216
217
        if ((r = sd_is_socket_internal(fd, type, listening)) <= 0)
218
                return r;
219
220
        if (family > 0) {
221
                union sockaddr_union sockaddr;
222
                socklen_t l;
223
224
                memset(&sockaddr, 0, sizeof(sockaddr));
225
                l = sizeof(sockaddr);
226
227
                if (getsockname(fd, &sockaddr.sa, &l) < 0)
228
                        return -errno;
229
230
                if (l < sizeof(sa_family_t))
231
                        return -EINVAL;
232
233
                return sockaddr.sa.sa_family == family;
234
        }
235
236
        return 1;
237
}
238
239
int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) {
240
        union sockaddr_union sockaddr;
241
        socklen_t l;
242
        int r;
243
244
        if (family != 0 && family != AF_INET && family != AF_INET6)
245
                return -EINVAL;
246
247
        if ((r = sd_is_socket_internal(fd, type, listening)) <= 0)
248
                return r;
249
250
        memset(&sockaddr, 0, sizeof(sockaddr));
251
        l = sizeof(sockaddr);
252
253
        if (getsockname(fd, &sockaddr.sa, &l) < 0)
254
                return -errno;
255
256
        if (l < sizeof(sa_family_t))
257
                return -EINVAL;
258
259
        if (sockaddr.sa.sa_family != AF_INET &&
260
            sockaddr.sa.sa_family != AF_INET6)
261
                return 0;
262
263
        if (family > 0)
264
                if (sockaddr.sa.sa_family != family)
265
                        return 0;
266
267
        if (port > 0) {
268
                if (sockaddr.sa.sa_family == AF_INET) {
269
                        if (l < sizeof(struct sockaddr_in))
270
                                return -EINVAL;
271
272
                        return htons(port) == sockaddr.in4.sin_port;
273
                } else {
274
                        if (l < sizeof(struct sockaddr_in6))
275
                                return -EINVAL;
276
277
                        return htons(port) == sockaddr.in6.sin6_port;
278
                }
279
        }
280
281
        return 1;
282
}
283
284
int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) {
285
        union sockaddr_union sockaddr;
286
        socklen_t l;
287
        int r;
288
289
        if ((r = sd_is_socket_internal(fd, type, listening)) <= 0)
290
                return r;
291
292
        memset(&sockaddr, 0, sizeof(sockaddr));
293
        l = sizeof(sockaddr);
294
295
        if (getsockname(fd, &sockaddr.sa, &l) < 0)
296
                return -errno;
297
298
        if (l < sizeof(sa_family_t))
299
                return -EINVAL;
300
301
        if (sockaddr.sa.sa_family != AF_UNIX)
302
                return 0;
303
304
        if (path) {
305
                if (length <= 0)
306
                        length = strlen(path);
307
308
                if (length <= 0)
309
                        /* Unnamed socket */
310
                        return l == sizeof(sa_family_t);
311
312
                if (path[0])
313
                        /* Normal path socket */
314
                        return
315
                                (l >= sizeof(sa_family_t) + length + 1) &&
316
                                memcmp(path, sockaddr.un.sun_path, length+1) == 0;
317
                else
318
                        /* Abstract namespace socket */
319
                        return
320
                                (l == sizeof(sa_family_t) + length) &&
321
                                memcmp(path, sockaddr.un.sun_path, length) == 0;
322
        }
323
324
        return 1;
325
}
326
327
int sd_notify(int unset_environment, const char *state) {
328
#if defined(DISABLE_SYSTEMD) || !defined(__linux__) || !defined(SOCK_CLOEXEC)
329
        return 0;
330
#else
331
        int fd = -1, r;
332
        struct msghdr msghdr;
333
        struct iovec iovec;
334
        union sockaddr_union sockaddr;
335
        const char *e;
336
337
        if (!state) {
338
                r = -EINVAL;
339
                goto finish;
340
        }
341
342
        if (!(e = getenv("NOTIFY_SOCKET")))
343
                return 0;
344
345
        /* Must be an abstract socket, or an absolute path */
346
        if ((e[0] != '@' && e[0] != '/') || e[1] == 0) {
347
                r = -EINVAL;
348
                goto finish;
349
        }
350
351
        if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) {
352
                r = -errno;
353
                goto finish;
354
        }
355
356
        memset(&sockaddr, 0, sizeof(sockaddr));
357
        sockaddr.sa.sa_family = AF_UNIX;
358
        strncpy(sockaddr.un.sun_path, e, sizeof(sockaddr.un.sun_path));
359
360
        if (sockaddr.un.sun_path[0] == '@')
361
                sockaddr.un.sun_path[0] = 0;
362
363
        memset(&iovec, 0, sizeof(iovec));
364
        iovec.iov_base = (char*) state;
365
        iovec.iov_len = strlen(state);
366
367
        memset(&msghdr, 0, sizeof(msghdr));
368
        msghdr.msg_name = &sockaddr;
369
        msghdr.msg_namelen = sizeof(sa_family_t) + strlen(e);
370
371
        if (msghdr.msg_namelen > sizeof(struct sockaddr_un))
372
                msghdr.msg_namelen = sizeof(struct sockaddr_un);
373
374
        msghdr.msg_iov = &iovec;
375
        msghdr.msg_iovlen = 1;
376
377
        if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
378
                r = -errno;
379
                goto finish;
380
        }
381
382
        r = 1;
383
384
finish:
385
        if (unset_environment)
386
                unsetenv("NOTIFY_SOCKET");
387
388
        if (fd >= 0)
389
                close(fd);
390
391
        return r;
392
#endif
393
}
394
395
int sd_notifyf(int unset_environment, const char *format, ...) {
396
#if defined(DISABLE_SYSTEMD) || !defined(__linux__)
397
        return 0;
398
#else
399
        va_list ap;
400
        char *p = NULL;
401
        int r;
402
403
        va_start(ap, format);
404
        r = vasprintf(&p, format, ap);
405
        va_end(ap);
406
407
        if (r < 0 || !p)
408
                return -ENOMEM;
409
410
        r = sd_notify(unset_environment, p);
411
        free(p);
412
413
        return r;
414
#endif
415
}
416
417
int sd_booted(void) {
418
#if defined(DISABLE_SYSTEMD) || !defined(__linux__)
419
        return 0;
420
#else
421
422
        struct stat a, b;
423
424
        /* We simply test whether the systemd cgroup hierarchy is
425
         * mounted */
426
427
        if (lstat("/sys/fs/cgroup", &a) < 0)
428
                return 0;
429
430
        if (lstat("/sys/fs/cgroup/systemd", &b) < 0)
431
                return 0;
432
433
        return a.st_dev != b.st_dev;
434
#endif
435
}
(-)a/runtime/sd-daemon.h (+261 lines)
Line 0 Link Here
1
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3
#ifndef foosddaemonhfoo
4
#define foosddaemonhfoo
5
6
/***
7
  Copyright 2010 Lennart Poettering
8
9
  Permission is hereby granted, free of charge, to any person
10
  obtaining a copy of this software and associated documentation files
11
  (the "Software"), to deal in the Software without restriction,
12
  including without limitation the rights to use, copy, modify, merge,
13
  publish, distribute, sublicense, and/or sell copies of the Software,
14
  and to permit persons to whom the Software is furnished to do so,
15
  subject to the following conditions:
16
17
  The above copyright notice and this permission notice shall be
18
  included in all copies or substantial portions of the Software.
19
20
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
  SOFTWARE.
28
***/
29
30
#include <sys/types.h>
31
#include <inttypes.h>
32
33
#ifdef __cplusplus
34
extern "C" {
35
#endif
36
37
/*
38
  Reference implementation of a few systemd related interfaces for
39
  writing daemons. These interfaces are trivial to implement. To
40
  simplify porting we provide this reference implementation.
41
  Applications are welcome to reimplement the algorithms described
42
  here if they do not want to include these two source files.
43
44
  The following functionality is provided:
45
46
  - Support for logging with log levels on stderr
47
  - File descriptor passing for socket-based activation
48
  - Daemon startup and status notification
49
  - Detection of systemd boots
50
51
  You may compile this with -DDISABLE_SYSTEMD to disable systemd
52
  support. This makes all those calls NOPs that are directly related to
53
  systemd (i.e. only sd_is_xxx() will stay useful).
54
55
  Since this is drop-in code we don't want any of our symbols to be
56
  exported in any case. Hence we declare hidden visibility for all of
57
  them.
58
59
  You may find an up-to-date version of these source files online:
60
61
  http://cgit.freedesktop.org/systemd/plain/src/sd-daemon.h
62
  http://cgit.freedesktop.org/systemd/plain/src/sd-daemon.c
63
64
  This should compile on non-Linux systems, too, but with the
65
  exception of the sd_is_xxx() calls all functions will become NOPs.
66
67
  See sd-daemon(7) for more information.
68
*/
69
70
#if (__GNUC__ >= 4)
71
#define _sd_printf_attr_(a,b) __attribute__ ((format (printf, a, b)))
72
#  if defined(SD_EXPORT_SYMBOLS)
73
#    define _sd_hidden_
74
#  else
75
#    define _sd_hidden_ __attribute__ ((visibility("hidden")))
76
#  endif
77
#else
78
#define _sd_printf_attr_(a,b)
79
#define _sd_hidden_
80
#endif
81
82
/*
83
  Log levels for usage on stderr:
84
85
          fprintf(stderr, SD_NOTICE "Hello World!\n");
86
87
  This is similar to printk() usage in the kernel.
88
*/
89
#define SD_EMERG   "<0>"  /* system is unusable */
90
#define SD_ALERT   "<1>"  /* action must be taken immediately */
91
#define SD_CRIT    "<2>"  /* critical conditions */
92
#define SD_ERR     "<3>"  /* error conditions */
93
#define SD_WARNING "<4>"  /* warning conditions */
94
#define SD_NOTICE  "<5>"  /* normal but significant condition */
95
#define SD_INFO    "<6>"  /* informational */
96
#define SD_DEBUG   "<7>"  /* debug-level messages */
97
98
/* The first passed file descriptor is fd 3 */
99
#define SD_LISTEN_FDS_START 3
100
101
/*
102
  Returns how many file descriptors have been passed, or a negative
103
  errno code on failure. Optionally, removes the $LISTEN_FDS and
104
  $LISTEN_PID file descriptors from the environment (recommended, but
105
  problematic in threaded environments). If r is the return value of
106
  this function you'll find the file descriptors passed as fds
107
  SD_LISTEN_FDS_START to SD_LISTEN_FDS_START+r-1. Returns a negative
108
  errno style error code on failure. This function call ensures that
109
  the FD_CLOEXEC flag is set for the passed file descriptors, to make
110
  sure they are not passed on to child processes. If FD_CLOEXEC shall
111
  not be set, the caller needs to unset it after this call for all file
112
  descriptors that are used.
113
114
  See sd_listen_fds(3) for more information.
115
*/
116
int sd_listen_fds(int unset_environment) _sd_hidden_;
117
118
/*
119
  Helper call for identifying a passed file descriptor. Returns 1 if
120
  the file descriptor is a FIFO in the file system stored under the
121
  specified path, 0 otherwise. If path is NULL a path name check will
122
  not be done and the call only verifies if the file descriptor
123
  refers to a FIFO. Returns a negative errno style error code on
124
  failure.
125
126
  See sd_is_fifo(3) for more information.
127
*/
128
int sd_is_fifo(int fd, const char *path) _sd_hidden_;
129
130
/*
131
  Helper call for identifying a passed file descriptor. Returns 1 if
132
  the file descriptor is a socket of the specified family (AF_INET,
133
  ...) and type (SOCK_DGRAM, SOCK_STREAM, ...), 0 otherwise. If
134
  family is 0 a socket family check will not be done. If type is 0 a
135
  socket type check will not be done and the call only verifies if
136
  the file descriptor refers to a socket. If listening is > 0 it is
137
  verified that the socket is in listening mode. (i.e. listen() has
138
  been called) If listening is == 0 it is verified that the socket is
139
  not in listening mode. If listening is < 0 no listening mode check
140
  is done. Returns a negative errno style error code on failure.
141
142
  See sd_is_socket(3) for more information.
143
*/
144
int sd_is_socket(int fd, int family, int type, int listening) _sd_hidden_;
145
146
/*
147
  Helper call for identifying a passed file descriptor. Returns 1 if
148
  the file descriptor is an Internet socket, of the specified family
149
  (either AF_INET or AF_INET6) and the specified type (SOCK_DGRAM,
150
  SOCK_STREAM, ...), 0 otherwise. If version is 0 a protocol version
151
  check is not done. If type is 0 a socket type check will not be
152
  done. If port is 0 a socket port check will not be done. The
153
  listening flag is used the same way as in sd_is_socket(). Returns a
154
  negative errno style error code on failure.
155
156
  See sd_is_socket_inet(3) for more information.
157
*/
158
int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) _sd_hidden_;
159
160
/*
161
  Helper call for identifying a passed file descriptor. Returns 1 if
162
  the file descriptor is an AF_UNIX socket of the specified type
163
  (SOCK_DGRAM, SOCK_STREAM, ...) and path, 0 otherwise. If type is 0
164
  a socket type check will not be done. If path is NULL a socket path
165
  check will not be done. For normal AF_UNIX sockets set length to
166
  0. For abstract namespace sockets set length to the length of the
167
  socket name (including the initial 0 byte), and pass the full
168
  socket path in path (including the initial 0 byte). The listening
169
  flag is used the same way as in sd_is_socket(). Returns a negative
170
  errno style error code on failure.
171
172
  See sd_is_socket_unix(3) for more information.
173
*/
174
int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) _sd_hidden_;
175
176
/*
177
  Informs systemd about changed daemon state. This takes a number of
178
  newline separated environment-style variable assignments in a
179
  string. The following variables are known:
180
181
     READY=1      Tells systemd that daemon startup is finished (only
182
                  relevant for services of Type=notify). The passed
183
                  argument is a boolean "1" or "0". Since there is
184
                  little value in signalling non-readiness the only
185
                  value daemons should send is "READY=1".
186
187
     STATUS=...   Passes a single-line status string back to systemd
188
                  that describes the daemon state. This is free-from
189
                  and can be used for various purposes: general state
190
                  feedback, fsck-like programs could pass completion
191
                  percentages and failing programs could pass a human
192
                  readable error message. Example: "STATUS=Completed
193
                  66% of file system check..."
194
195
     ERRNO=...    If a daemon fails, the errno-style error code,
196
                  formatted as string. Example: "ERRNO=2" for ENOENT.
197
198
     BUSERROR=... If a daemon fails, the D-Bus error-style error
199
                  code. Example: "BUSERROR=org.freedesktop.DBus.Error.TimedOut"
200
201
     MAINPID=...  The main pid of a daemon, in case systemd did not
202
                  fork off the process itself. Example: "MAINPID=4711"
203
204
  Daemons can choose to send additional variables. However, it is
205
  recommened to prefix variable names not listed above with X_.
206
207
  Returns a negative errno-style error code on failure. Returns > 0
208
  if systemd could be notified, 0 if it couldn't possibly because
209
  systemd is not running.
210
211
  Example: When a daemon finished starting up, it could issue this
212
  call to notify systemd about it:
213
214
     sd_notify(0, "READY=1");
215
216
  See sd_notifyf() for more complete examples.
217
218
  See sd_notify(3) for more information.
219
*/
220
int sd_notify(int unset_environment, const char *state) _sd_hidden_;
221
222
/*
223
  Similar to sd_notify() but takes a format string.
224
225
  Example 1: A daemon could send the following after initialization:
226
227
     sd_notifyf(0, "READY=1\n"
228
                   "STATUS=Processing requests...\n"
229
                   "MAINPID=%lu",
230
                   (unsigned long) getpid());
231
232
  Example 2: A daemon could send the following shortly before
233
  exiting, on failure:
234
235
     sd_notifyf(0, "STATUS=Failed to start up: %s\n"
236
                   "ERRNO=%i",
237
                   strerror(errno),
238
                   errno);
239
240
  See sd_notifyf(3) for more information.
241
*/
242
int sd_notifyf(int unset_environment, const char *format, ...) _sd_printf_attr_(2,3) _sd_hidden_;
243
244
/*
245
  Returns > 0 if the system was booted with systemd. Returns < 0 on
246
  error. Returns 0 if the system was not booted with systemd. Note
247
  that all of the functions above handle non-systemd boots just
248
  fine. You should NOT protect them with a call to this function. Also
249
  note that this function checks whether the system, not the user
250
  session is controlled by systemd. However the functions above work
251
  for both session and system services.
252
253
  See sd_booted(3) for more information.
254
*/
255
int sd_booted(void) _sd_hidden_;
256
257
#ifdef __cplusplus
258
}
259
#endif
260
261
#endif
(-)a/tools/syslogd.c (+3 lines)
Lines 135-140 Link Here
135
#include "net.h"
135
#include "net.h"
136
#include "vm.h"
136
#include "vm.h"
137
#include "prop.h"
137
#include "prop.h"
138
#include "sd-daemon.h"
138
139
139
/* definitions for objects we access */
140
/* definitions for objects we access */
140
DEFobjCurrIf(obj)
141
DEFobjCurrIf(obj)
Lines 2431-2436 doGlblProcessInit(void) Link Here
2431
			num_fds = getdtablesize();
2432
			num_fds = getdtablesize();
2432
			close(0);
2433
			close(0);
2433
			/* we keep stdout and stderr open in case we have to emit something */
2434
			/* we keep stdout and stderr open in case we have to emit something */
2435
2436
                       if (sd_listen_fds(0) <= 0)
2434
			for (i = 3; i < num_fds; i++)
2437
			for (i = 3; i < num_fds; i++)
2435
				(void) close(i);
2438
				(void) close(i);
2436
			untty();
2439
			untty();

Return to bug 656104