368 lines
7.7 KiB
Diff
368 lines
7.7 KiB
Diff
!
|
|
! Be able to write errors on creating of pid file on
|
|
! the current terminal (bug #394787)
|
|
!
|
|
---
|
|
klogd.c | 78 +++++++++++++++++++++++++++++++++++---------------------------
|
|
pidfile.c | 39 +++++++++++++++++++++++++++----
|
|
syslogd.c | 60 +++++++++++++++++++++++++++++------------------
|
|
3 files changed, 117 insertions(+), 60 deletions(-)
|
|
|
|
--- klogd.c
|
|
+++ klogd.c 2022-10-17 11:12:11.438652739 +0000
|
|
@@ -268,6 +268,8 @@
|
|
#include <stdarg.h>
|
|
#include <paths.h>
|
|
#include <stdlib.h>
|
|
+#include <sys/types.h>
|
|
+#include <sys/wait.h>
|
|
#include "klogd.h"
|
|
#include "ksyms.h"
|
|
#ifndef TESTING
|
|
@@ -366,13 +368,12 @@ static void CloseLogSrc()
|
|
/*
|
|
* Signal handler to terminate the parent process.
|
|
*/
|
|
+static volatile sig_atomic_t leave;
|
|
#ifndef TESTING
|
|
void doexit(sig)
|
|
-
|
|
int sig;
|
|
-
|
|
{
|
|
- exit (0);
|
|
+ leave++;
|
|
}
|
|
#endif
|
|
|
|
@@ -1110,14 +1111,41 @@ int main(argc, argv)
|
|
{
|
|
if (!check_pid(PidFile))
|
|
{
|
|
- signal (SIGTERM, doexit);
|
|
- if ( fork() == 0 )
|
|
- {
|
|
- auto int fl;
|
|
- int num_fds = getdtablesize();
|
|
+ pid_t pid;
|
|
+ int n=0, num_fds, fl, status;
|
|
|
|
+ leave = 0;
|
|
+ signal (SIGTERM, doexit);
|
|
+ switch ((pid = fork())) {
|
|
+ default:
|
|
+ retry:
|
|
+ /*
|
|
+ * Parent process
|
|
+ */
|
|
+ switch (waitpid(pid, &status, WNOHANG|WUNTRACED)) {
|
|
+ case -1:
|
|
+ if (errno == EINTR)
|
|
+ goto retry;
|
|
+ break;
|
|
+ case 0:
|
|
+ if (leave)
|
|
+ exit(0);
|
|
+ usleep(10*1000);
|
|
+ if (++n < 30000)
|
|
+ goto retry;
|
|
+ default:
|
|
+ break;
|
|
+ }
|
|
+ case -1:
|
|
+ /*
|
|
+ * Not reached unless something major went wrong.
|
|
+ */
|
|
+ exit(1);
|
|
+ case 0:
|
|
signal (SIGTERM, SIG_DFL);
|
|
-
|
|
+
|
|
+ num_fds = getdtablesize();
|
|
+
|
|
/* This is the child closing its file descriptors. */
|
|
for (fl= 0; fl <= num_fds; ++fl)
|
|
{
|
|
@@ -1128,17 +1156,12 @@ int main(argc, argv)
|
|
}
|
|
|
|
setsid();
|
|
- }
|
|
- else
|
|
- {
|
|
- /*
|
|
- * Parent process
|
|
- */
|
|
- sleep(300);
|
|
- /*
|
|
- * Not reached unless something major went wrong.
|
|
- */
|
|
- exit(1);
|
|
+
|
|
+ /* tuck my process id away */
|
|
+ if (!write_pid(PidFile))
|
|
+ Terminate();
|
|
+
|
|
+ break;
|
|
}
|
|
}
|
|
else
|
|
@@ -1147,19 +1170,6 @@ int main(argc, argv)
|
|
exit(1);
|
|
}
|
|
}
|
|
-
|
|
-
|
|
- /* tuck my process id away */
|
|
- if (!check_pid(PidFile))
|
|
- {
|
|
- if (!write_pid(PidFile))
|
|
- Terminate();
|
|
- }
|
|
- else
|
|
- {
|
|
- fputs("klogd: Already running.\n", stderr);
|
|
- Terminate();
|
|
- }
|
|
#endif
|
|
|
|
/* Signal setups. */
|
|
@@ -1225,6 +1235,8 @@ int main(argc, argv)
|
|
{
|
|
if ( change_state )
|
|
ChangeLogging();
|
|
+ if (leave)
|
|
+ exit(0);
|
|
switch ( logsrc )
|
|
{
|
|
case kernel:
|
|
--- pidfile.c
|
|
+++ pidfile.c 2022-10-17 11:11:16.627630427 +0000
|
|
@@ -23,6 +23,7 @@
|
|
* Sat Aug 19 13:24:33 MET DST 1995: Martin Schulze
|
|
* First version (v0.2) released
|
|
*/
|
|
+#define USE_FCNTL 1
|
|
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
@@ -31,6 +32,10 @@
|
|
#include <string.h>
|
|
#include <errno.h>
|
|
#include <signal.h>
|
|
+#if defined(USE_FCNTL) && (USE_FCNTL != 0)
|
|
+# include <unistd.h>
|
|
+# include <fcntl.h>
|
|
+#endif
|
|
|
|
/* read_pid
|
|
*
|
|
@@ -86,6 +91,9 @@ int write_pid (char *pidfile)
|
|
FILE *f;
|
|
int fd;
|
|
int pid;
|
|
+#if defined(USE_FCNTL) && (USE_FCNTL != 0)
|
|
+ struct flock lck;
|
|
+#endif
|
|
|
|
if ( ((fd = open(pidfile, O_RDWR|O_CREAT|O_TRUNC, 0644)) == -1)
|
|
|| ((f = fdopen(fd, "r+")) == NULL) ) {
|
|
@@ -93,23 +101,46 @@ int write_pid (char *pidfile)
|
|
return 0;
|
|
}
|
|
|
|
+#if defined(USE_FCNTL) && (USE_FCNTL != 0)
|
|
+ memset(&lck, 0, sizeof (struct flock));
|
|
+ lck.l_type = F_WRLCK;
|
|
+ lck.l_whence = SEEK_SET;
|
|
+
|
|
+ if (fcntl(fd, F_SETLK, &lck) == -1) {
|
|
+ fclose(f);
|
|
+ memset(&lck, 0, sizeof (struct flock));
|
|
+ fcntl(fd, F_GETLK, &lck);
|
|
+ fprintf(stderr, "Can't lock, lock is held by pid %d.\n", lck.l_pid);
|
|
+ return 0;
|
|
+ }
|
|
+#else
|
|
if (flock(fd, LOCK_EX|LOCK_NB) == -1) {
|
|
fscanf(f, "%d", &pid);
|
|
fclose(f);
|
|
- printf("Can't lock, lock is held by pid %d.\n", pid);
|
|
+ fprintf(stderr, "Can't lock, lock is held by pid %d.\n", pid);
|
|
return 0;
|
|
}
|
|
+#endif
|
|
|
|
pid = getpid();
|
|
if (!fprintf(f,"%d\n", pid)) {
|
|
- printf("Can't write pid , %s.\n", strerror(errno));
|
|
+ fprintf(stderr, "Can't write pid , %s.\n", strerror(errno));
|
|
close(fd);
|
|
return 0;
|
|
}
|
|
fflush(f);
|
|
|
|
- if (flock(fd, LOCK_UN) == -1) {
|
|
- printf("Can't unlock pidfile %s, %s.\n", pidfile, strerror(errno));
|
|
+#if defined(USE_FCNTL) && (USE_FCNTL != 0)
|
|
+ memset(&lck, 0, sizeof (struct flock));
|
|
+ lck.l_type = F_UNLCK;
|
|
+ lck.l_whence = SEEK_SET;
|
|
+
|
|
+ if (fcntl(fd, F_SETLK, &lck) == -1)
|
|
+#else
|
|
+ if (flock(fd, LOCK_UN) == -1)
|
|
+#endif
|
|
+ {
|
|
+ fprintf(stderr,"Can't unlock pidfile %s, %s.\n", pidfile, strerror(errno));
|
|
close(fd);
|
|
return 0;
|
|
}
|
|
--- syslogd.c
|
|
+++ syslogd.c 2022-10-17 11:11:16.627630427 +0000
|
|
@@ -22,7 +22,7 @@ char copyright2[] =
|
|
#endif /* not lint */
|
|
|
|
#if !defined(lint) && !defined(NO_SCCS)
|
|
-static char sccsid[] = "@(#)syslogd.c 5.27 (Berkeley) 10/10/88";
|
|
+static char sccsid[] __attribute__ ((unused)) = "@(#)syslogd.c 5.27 (Berkeley) 10/10/88";
|
|
#endif /* not lint */
|
|
|
|
/*
|
|
@@ -900,7 +900,9 @@ int main(argc, argv)
|
|
fd_set readfds;
|
|
|
|
#ifndef TESTING
|
|
+#ifdef SYSLOG_UNIXAF
|
|
int fd;
|
|
+#endif
|
|
#ifdef SYSLOG_INET
|
|
#ifdef INET6
|
|
struct sockaddr_storage frominet;
|
|
@@ -915,7 +917,9 @@ int main(argc, argv)
|
|
int ch;
|
|
struct hostent *hent;
|
|
|
|
+#if defined(SYSLOG_UNIXAF) || defined(TESTING)
|
|
char line[MAXLINE +1];
|
|
+#endif
|
|
extern int optind;
|
|
extern char *optarg;
|
|
int maxfds;
|
|
@@ -1033,11 +1037,20 @@ int main(argc, argv)
|
|
exit(1);
|
|
case 0:
|
|
signal (SIGTERM, SIG_DFL);
|
|
-
|
|
+
|
|
+ /* tuck my process id away */
|
|
+ dprintf("Writing pidfile.\n");
|
|
+ if (!write_pid(PidFile))
|
|
+ {
|
|
+ dprintf("Can't write pid.\n");
|
|
+ exit(1);
|
|
+ }
|
|
+
|
|
num_fds = getdtablesize();
|
|
for (i = 0; i < num_fds; i++)
|
|
(void) close(i);
|
|
untty();
|
|
+ break;
|
|
}
|
|
}
|
|
else
|
|
@@ -1047,36 +1060,37 @@ int main(argc, argv)
|
|
}
|
|
}
|
|
else
|
|
+ {
|
|
#endif
|
|
debugging_on = 1;
|
|
#ifndef SYSV
|
|
- else
|
|
setlinebuf(stdout);
|
|
#endif
|
|
|
|
#ifndef TESTING
|
|
- /* tuck my process id away */
|
|
- if ( !Debug )
|
|
- {
|
|
- dprintf("Writing pidfile.\n");
|
|
- if (!check_pid(PidFile))
|
|
+ /* tuck my process id away */
|
|
+ if ( !Debug )
|
|
{
|
|
- if (!write_pid(PidFile))
|
|
+ dprintf("Writing pidfile.\n");
|
|
+ if (!check_pid(PidFile))
|
|
+ {
|
|
+ if (!write_pid(PidFile))
|
|
+ {
|
|
+ dprintf("Can't write pid.\n");
|
|
+ if (getpid() != ppid)
|
|
+ kill (ppid, SIGTERM);
|
|
+ exit(1);
|
|
+ }
|
|
+ }
|
|
+ else
|
|
{
|
|
- dprintf("Can't write pid.\n");
|
|
+ dprintf("Pidfile (and pid) already exist.\n");
|
|
if (getpid() != ppid)
|
|
kill (ppid, SIGTERM);
|
|
exit(1);
|
|
}
|
|
- }
|
|
- else
|
|
- {
|
|
- dprintf("Pidfile (and pid) already exist.\n");
|
|
- if (getpid() != ppid)
|
|
- kill (ppid, SIGTERM);
|
|
- exit(1);
|
|
- }
|
|
- } /* if ( !Debug ) */
|
|
+ } /* if ( !Debug ) */
|
|
+ }
|
|
#endif
|
|
|
|
consfile.f_type = F_CONSOLE;
|
|
@@ -1738,7 +1752,7 @@ void logmsg(pri, msg, from, flags)
|
|
int flags;
|
|
{
|
|
register struct filed *f;
|
|
- int fac, prilev, lognum;
|
|
+ int fac, prilev;
|
|
int msglen;
|
|
char *timestamp;
|
|
#ifdef __gnu_linux__
|
|
@@ -1746,6 +1760,8 @@ void logmsg(pri, msg, from, flags)
|
|
#else
|
|
#ifndef SYSV
|
|
sigset_t omask;
|
|
+#else
|
|
+ int lognum;
|
|
#endif
|
|
#endif
|
|
|
|
@@ -2512,11 +2528,9 @@ void init()
|
|
register int i, lognum;
|
|
register FILE *cf;
|
|
register struct filed *f;
|
|
-#ifndef TESTING
|
|
#ifndef SYSV
|
|
register struct filed **nextp = (struct filed **) 0;
|
|
#endif
|
|
-#endif
|
|
register char *p;
|
|
register unsigned int Forwarding = 0;
|
|
#ifdef CONT_LINE
|
|
@@ -2601,7 +2615,7 @@ void init()
|
|
#else
|
|
*nextp = (struct filed *)calloc(1, sizeof(*f));
|
|
cfline("*.ERR\t" _PATH_CONSOLE, *nextp);
|
|
- (*nextp)->f_next = (struct filed *)calloc(1, sizeof(*f)) /* ASP */
|
|
+ (*nextp)->f_next = (struct filed *)calloc(1, sizeof(*f)); /* ASP */
|
|
cfline("*.PANIC\t*", (*nextp)->f_next);
|
|
#endif
|
|
Initialized = 1;
|