summaryrefslogtreecommitdiffstats
path: root/libdaemon/dexec.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2008-07-28 20:50:57 +0200
committerLennart Poettering <lennart@poettering.net>2008-07-28 20:50:57 +0200
commit7892e9b63584b3bc9965e1d4e7d0188ba6fab8c5 (patch)
treef7ad3f144958609f92973fc3cd4ce812cae1a50f /libdaemon/dexec.c
parentfbfaf950e6299952f9d2add5bb69c617f0bd91a9 (diff)
update licenses/file headers
Diffstat (limited to 'libdaemon/dexec.c')
-rw-r--r--libdaemon/dexec.c85
1 files changed, 42 insertions, 43 deletions
diff --git a/libdaemon/dexec.c b/libdaemon/dexec.c
index 0a21777..f0d8415 100644
--- a/libdaemon/dexec.c
+++ b/libdaemon/dexec.c
@@ -1,23 +1,22 @@
-/* $Id: exec.c 31 2003-11-05 22:04:26Z lennart $ */
-
-/*
- * This file is part of libdaemon.
- *
- * libdaemon is free software; you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * libdaemon is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with libdaemon; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA
- */
+/***
+ This file is part of libdaemon.
+
+ Copyright 2003-2008 Lennart Poettering
+
+ libdaemon is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation, either version 2.1 of the
+ License, or (at your option) any later version.
+
+ libdaemon is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with libdaemon. If not, see
+ <http://www.gnu.org/licenses/>.
+***/
#ifdef HAVE_CONFIG_H
#include <config.h>
@@ -59,21 +58,21 @@ int daemon_execv(const char *dir, int *ret, const char *prog, va_list ap) {
daemon_log(LOG_ERR, "pipe() failed: %s", strerror(errno));
return -1;
}
-
+
if ((pid = fork()) < 0) {
daemon_log(LOG_ERR, "fork() failed: %s", strerror(errno));
return -1;
-
+
} else if (pid == 0) {
char *args[MAX_ARGS];
int i;
-
+
if (p[1] != 1)
dup2(p[1], 1);
if (p[1] != 2)
dup2(p[1], 2);
-
+
if (p[0] > 2)
close(p[0]);
@@ -87,28 +86,28 @@ int daemon_execv(const char *dir, int *ret, const char *prog, va_list ap) {
}
daemon_close_all(-1);
-
+
umask(0022); /* Set up a sane umask */
-
+
if (dir && chdir(dir) < 0) {
daemon_log(LOG_WARNING, "Failed to change to directory '%s'", dir);
chdir("/");
}
-
+
for (i = 0; i < MAX_ARGS-1; i++)
if (!(args[i] = va_arg(ap, char*)))
break;
args[i] = NULL;
-
+
execv(prog, args);
-
+
daemon_log(LOG_ERR, "execv(%s) failed: %s", prog, strerror(errno));
-
+
_exit(EXIT_FAILURE);
}
-
+
close(p[1]);
-
+
FD_ZERO(&fds);
FD_SET(p[0], &fds);
sigfd = daemon_signal_fd();
@@ -135,41 +134,41 @@ int daemon_execv(const char *dir, int *ret, const char *prog, va_list ap) {
break;
buf[n] = c;
-
+
if (c == '\n' || n >= sizeof(buf) - 2) {
if (c != '\n') n++;
buf[n] = 0;
- if (buf[0])
- daemon_log(LOG_INFO, "client: %s", buf);
-
+ if (buf[0])
+ daemon_log(LOG_INFO, "client: %s", buf);
+
n = 0;
} else
n++;
}
-
+
if (FD_ISSET(sigfd, &qfds)) {
int sig;
-
+
if ((sig = daemon_signal_next()) < 0) {
daemon_log(LOG_ERR, "daemon_signal_next(): %s", strerror(errno));
break;
}
-
+
if (sig != SIGCHLD) {
daemon_log(LOG_WARNING, "Killing child.");
kill(pid, SIGTERM);
}
}
}
-
+
if (n > 0) {
buf[n] = 0;
- daemon_log(LOG_WARNING, "client: %s", buf);
+ daemon_log(LOG_WARNING, "client: %s", buf);
}
close(p[0]);
-
+
for (;;) {
if (waitpid(pid, &r, 0) < 0) {
@@ -193,7 +192,7 @@ int daemon_execv(const char *dir, int *ret, const char *prog, va_list ap) {
int daemon_exec(const char *dir, int *ret, const char *prog, ...) {
va_list ap;
int r;
-
+
va_start(ap, prog);
r = daemon_execv(dir, ret, prog, ap);
va_end(ap);