1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
From: Georges Khaznadar <georgesk@debian.org>
Date: Wed, 11 Oct 2023 11:43:19 +0200
Subject: Improve waiting for spawned editor
Refine the process of waiting for the spawned editor. Also, block signals while
waiting.
Fix provided by Steve Greenland <stevegr@debian.org>.
Forwarded: no
Last-Update: 2015-12-22
---
crontab.c | 59 +++++++++++++++++++++++++++++++++--------------------------
1 file changed, 33 insertions(+), 26 deletions(-)
diff --git a/crontab.c b/crontab.c
index 0514cba..8389f96 100644
--- a/crontab.c
+++ b/crontab.c
@@ -31,6 +31,7 @@ static char rcsid[] = "$Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $";
#include "cron.h"
#include <errno.h>
#include <fcntl.h>
+#include <signal.h>
#include <sys/file.h>
#include <sys/stat.h>
#ifdef USE_UTIMES
@@ -408,6 +409,11 @@ edit_cmd() {
* close and reopen the file around the edit.
*/
+ /* Turn off signals. */
+ (void)signal(SIGHUP, SIG_IGN);
+ (void)signal(SIGINT, SIG_IGN);
+ (void)signal(SIGQUIT, SIG_IGN);
+
switch (pid = fork()) {
case -1:
perror("fork");
@@ -438,33 +444,34 @@ edit_cmd() {
}
/* parent */
- xpid = wait(&waiter);
- if (xpid != pid) {
- fprintf(stderr, "%s: wrong PID (%d != %d) from \"%s\"\n",
- ProgramName, xpid, pid, editor);
- goto fatal;
- }
- if (WIFEXITED(waiter) && WEXITSTATUS(waiter)) {
- fprintf(stderr, "%s: \"%s\" exited with status %d\n",
- ProgramName, editor, WEXITSTATUS(waiter));
- goto fatal;
- }
- if (WIFSIGNALED(waiter)) {
- fprintf(stderr,
- "%s: \"%s\" killed; signal %d (%score dumped)\n",
- ProgramName, editor, WTERMSIG(waiter),
- WCOREDUMP(waiter) ?"" :"no ");
- goto fatal;
- }
- if (fstat(t, &statbuf) < 0) {
- perror("fstat");
- goto fatal;
- }
- if (mtime == statbuf.st_mtime) {
- fprintf(stderr, "%s: no changes made to crontab\n",
- ProgramName);
- goto remove;
+ while (1) {
+ xpid = waitpid(pid, &waiter, WUNTRACED);
+ if (xpid == -1) {
+ fprintf(stderr, "%s: waitpid() failed waiting for PID %d from \"%s\": %s\n",
+ ProgramName, pid, editor, strerror(errno));
+ } else if (xpid != pid) {
+ fprintf(stderr, "%s: wrong PID (%d != %d) from \"%s\"\n",
+ ProgramName, xpid, pid, editor);
+ goto fatal;
+ } else if (WIFSTOPPED(waiter)) {
+ /* raise(WSTOPSIG(waiter)); Not needed and breaks in job control shell*/
+ } else if (WIFEXITED(waiter) && WEXITSTATUS(waiter)) {
+ fprintf(stderr, "%s: \"%s\" exited with status %d\n",
+ ProgramName, editor, WEXITSTATUS(waiter));
+ goto fatal;
+ } else if (WIFSIGNALED(waiter)) {
+ fprintf(stderr,
+ "%s: \"%s\" killed; signal %d (%score dumped)\n",
+ ProgramName, editor, WTERMSIG(waiter),
+ WCOREDUMP(waiter) ?"" :"no ");
+ goto fatal;
+ } else
+ break;
}
+ (void)signal(SIGHUP, SIG_DFL);
+ (void)signal(SIGINT, SIG_DFL);
+ (void)signal(SIGQUIT, SIG_DFL);
+
fprintf(stderr, "%s: installing new crontab\n", ProgramName);
switch (replace_cmd()) {
case 0:
|