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

(-)a/src/sd-daemon.c (-2 / +40 lines)
Lines 44-50 Link Here
44
44
45
#include "sd-daemon.h"
45
#include "sd-daemon.h"
46
46
47
int sd_listen_fds(int unset_environment) {
47
static int _sd_listen_fds(int unset_environment, int close_on_exec) {
48
48
49
#if defined(DISABLE_SYSTEMD) || !defined(__linux__)
49
#if defined(DISABLE_SYSTEMD) || !defined(__linux__)
50
        return 0;
50
        return 0;
Lines 107-113 int sd_listen_fds(int unset_environment) { Link Here
107
                if (flags & FD_CLOEXEC)
107
                if (flags & FD_CLOEXEC)
108
                        continue;
108
                        continue;
109
109
110
                if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
110
                if (close_on_exec && fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
111
                        r = -errno;
111
                        r = -errno;
112
                        goto finish;
112
                        goto finish;
113
                }
113
                }
Lines 125-130 finish: Link Here
125
#endif
125
#endif
126
}
126
}
127
127
128
int sd_listen_fds(int unset_environment) {
129
	return _sd_listen_fds(unset_environment, 1);
130
}
131
132
int sd_listen_fds_check(void) {
133
	return _sd_listen_fds(0, 0);
134
}
135
136
void sd_listen_pid_inherit(void) {
137
	const char *env;
138
139
	/*
140
	  Type=forking & systemd socket activation:
141
	  fetch listen pid and update to ours,
142
	  when it is set to pid of our parent.
143
	*/
144
	if ((env = getenv("LISTEN_PID"))) {
145
		char *ptr;
146
		long l;
147
148
		errno = 0;
149
		l = strtol(env, &ptr, 10);
150
		if (errno != 0)
151
			return;
152
		if (ptr == env)
153
			return;
154
		if (*ptr != '\0')
155
			return;
156
		if (l < 0)
157
			return;
158
		if (getppid() == (pid_t)l) {
159
			char buf[24];
160
			snprintf(buf, sizeof(buf), "%d", getpid());
161
			setenv("LISTEN_PID", buf, 1);
162
		}
163
	}
164
}
165
128
int sd_is_fifo(int fd, const char *path) {
166
int sd_is_fifo(int fd, const char *path) {
129
        struct stat st_fd;
167
        struct stat st_fd;
130
168
(-)a/src/sd-daemon.h (-1 / +17 lines)
Lines 120-125 extern "C" { Link Here
120
int sd_listen_fds(int unset_environment) _sd_hidden_;
120
int sd_listen_fds(int unset_environment) _sd_hidden_;
121
121
122
/*
122
/*
123
  Same to sd_listen_fds, but does not make any modifications to
124
  the sockets (FD_CLOEXEC) or unset of the environment variables.
125
  Intended use of this function is to avoid socket closing in
126
  daemons started in Type=forking mode [before the fork call],
127
  so they stay open for the child process.
128
*/
129
int sd_listen_fds_check(void) _sd_hidden_;
130
131
/*
132
  Update the $LISTEN_PID variable to own PID, when it matches the
133
  parent. Intended use of this function is to inherit the sockets
134
  in child process of a daemon started in Type=forking mode [after
135
  the fork call].
136
*/
137
void sd_listen_pid_inherit(void)  _sd_hidden_;
138
139
/*
123
  Helper call for identifying a passed file descriptor. Returns 1 if
140
  Helper call for identifying a passed file descriptor. Returns 1 if
124
  the file descriptor is a FIFO in the file system stored under the
141
  the file descriptor is a FIFO in the file system stored under the
125
  specified path, 0 otherwise. If path is NULL a path name check will
142
  specified path, 0 otherwise. If path is NULL a path name check will
126
- 

Return to bug 656259