unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
blob d279ebd8ffadb5c454efb831455a82f78414d165 10162 bytes (raw)
name: gnu/packages/patches/restartd-update-robust.patch 	 # note: path name is non-authoritative(*)

  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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
 
From 01cd2d15a9bf1109e0e71b3e31b835d63dcf9cd8 Mon Sep 17 00:00:00 2001
From: Maxime Devos <maximedevos@telenet.be>, Yin Kangkai <yinkangkai@xiaomi.com>, Sudip Mukherjee <sudipm.mukherjee@gmail.com>
Subject: [PATCH]

Fix segfault when run as normal user

Also exit immediately when "restartd -h"
Signed-off-by: Yin Kangkai <yinkangkai@xiaomi.com>

Fix build with gcc-10

Bug: https://bugs.debian.org/957761
Signed-off-by: Sudip Mukherjee <sudipm.mukherjee@gmail.com>

Handle memory allocation failures.

This makes the code a little more robust.
Signed-off-by: Maxime Devos <maximedevos@telenet.be>

Handle fopen failures.

This makes the code a little more robust. What if /var/run does not
exist, or we do not have permission to open
/var/run/restartd.pid (EPERM?) due to SELinux misconfiguration?
Signed-off-by: Maxime Devos <maximedevos@telenet.be>

Handle printf and fprintf failures.

This makes the code a little more robust. What if the write was
refused to the underlying device being removed?

The --help, debug and stderr printfs were ignored because there error
handling does not appear important to me.
Signed-off-by: Maxime Devos <maximedevos@telenet.be>

Handle fclose failures when writing.

This makes the code a little more robust. What if a quotum is exceeded?
Signed-off-by: Maxime Devos <maximedevos@telenet.be>
---
 config.c   | 19 +++++++++----
 config.h   | 14 ++++++----
 restartd.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++-------
 3 files changed, 92 insertions(+), 21 deletions(-)

diff --git a/config.c b/config.c
index f307b8a..5cc0271 100644
--- a/config.c
+++ b/config.c
@@ -57,7 +57,11 @@ int read_config(void)
     config_process_number = 0;
 
     line1 = (char *) malloc(MAX_LINE_LENGTH);
+    if (!line1)
+      oom_failure();
     line2 = (char *) malloc(MAX_LINE_LENGTH);
+    if (!line2)
+      oom_failure();
 
     if ((config_fd = fopen(config_file, "rt")) == NULL) {
         fprintf(stderr, "Error at opening config file: %s\n", config_file);
@@ -155,11 +159,16 @@ void dump_config(void) {
     int i;
 
     for(i=0; i<config_process_number; i++) {
-        printf("ID=%d\n  name=%s\n  regexp=%s\n  running=%s\n  not_running=%s\n", i,
-            config_process[i].name,
-            config_process[i].regexp,
-            config_process[i].running,
-            config_process[i].not_running);
+      if (printf("ID=%d\n  name=%s\n  regexp=%s\n  running=%s\n  not_running=%s\n", i,
+                 config_process[i].name,
+                 config_process[i].regexp,
+                 config_process[i].running,
+                 config_process[i].not_running) < 0) {
+        /* Maybe stdout points to a file and a file system quotum was exceeded? */
+        fprintf(stderr, "Failed to dump the configuration. Exiting.\n");
+        syslog(LOG_ERR, "Failed to dump the configuration. Exiting.");
+        exit(1);
+      }
     }
 
 }
diff --git a/config.h b/config.h
index fabaa2b..2ba1cbe 100644
--- a/config.h
+++ b/config.h
@@ -1,6 +1,7 @@
 /* restartd - Process checker and/or restarter daemon
  * Copyright (C) 2000-2002 Tibor Koleszar <oldw@debian.org>
  * Copyright (C) 2006 Aurélien GÉRÔME <ag@roxor.cx>
+ * Copyright (C) 2022 Maxime Devos <maximedevos@telenet.be>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -25,12 +26,12 @@
 
 #define DEFAULT_CONFIG "/etc/restartd.conf"
 
-int debug;
-int config_process_number;
-int check_interval;
-int foreground;
-struct config_process_type *config_process;
-char *config_file;
+extern int debug;
+extern int config_process_number;
+extern int check_interval;
+extern int foreground;
+extern struct config_process_type *config_process;
+extern char *config_file;
 
 typedef struct config_process_type {
   char name[64];
@@ -43,5 +44,6 @@ typedef struct config_process_type {
 
 int read_config(/* char *config_file */);
 void dump_config(void);
+void oom_failure(void);
 
 #endif /* RESTARTD_CONFIG_H */
diff --git a/restartd.c b/restartd.c
index 2aa720c..aa74334 100644
--- a/restartd.c
+++ b/restartd.c
@@ -1,6 +1,7 @@
 /* restartd - Process checker and/or restarter daemon
  * Copyright (C) 2000-2002 Tibor Koleszar <oldw@debian.org>
  * Copyright (C) 2006 Aurélien GÉRÔME <ag@roxor.cx>
+ * Copyright (C) 2022 Maxime Devos <maximedevos@telenet.be>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -35,6 +36,13 @@
 
 #include "config.h"
 
+int debug;
+int config_process_number;
+int check_interval;
+int foreground;
+struct config_process_type *config_process;
+char *config_file;
+
 /* SIGTERM & SIGHUP handler */
 void got_signal(int sig)
 {
@@ -52,6 +60,17 @@ void got_signal(int sig)
     }
 }
 
+/* Ignoring out-of-memory failures is risky on systems without virtual memory
+   where additionally at address 0 there is actually something important
+   mapped. Additionally, while often on Linux the OOM killer will kill processes
+   where an OOM happens, this is not always the case and there exist other systems
+   without an OOM killer (e.g. the Hurd). */
+void oom_failure()
+{
+  syslog(LOG_ERR, "Failed to allocate memory. Exiting.");
+  exit(1);
+}
+
 int main(int argc, char *argv[])
 {
     DIR *procdir_id;
@@ -75,15 +94,21 @@ int main(int argc, char *argv[])
 
     /* Options */
     config_file = strdup(DEFAULT_CONFIG);
+    if (!config_file)
+      oom_failure();
+
     list_only = 0;
 
     for(i = 0; i < argc; i++) {
         if (!strcmp(argv[i], "-c") || !strcmp(argv[i], "--config")) {
             config_file = strdup(argv[i + 1]);
+	    if (!config_file)
+	      oom_failure();
         }
         if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version")) {
             printf("restard %s - Copyright 2000-2002 Tibor Koleszar <oldw@debian.org>\n"
-                   "                Copyright 2006 Aurélien GÉRÔME <ag@roxor.cx>\n",
+                   "                Copyright 2006 Aurélien GÉRÔME <ag@roxor.cx>\n"
+                   "                Copyright 2022 Maxime Devos <maximedevos@telenet.be>\n",
                    VERSION);
             exit(0);
         }
@@ -118,10 +143,13 @@ int main(int argc, char *argv[])
                     "  -i <interval_sec>: the check interval in second\n"
                     "  -l               : list configuration options\n"
                     "  -h               : help\n\n", VERSION);
+	    exit(0);
         }
     }
 
     config_process = malloc(sizeof(struct config_process_type) * 128);
+    if (!config_process)
+      oom_failure();
   
     read_config();
     if (list_only) {
@@ -133,9 +161,17 @@ int main(int argc, char *argv[])
            config_process_number);
   
     procdir_dirent = malloc(sizeof(struct dirent));
+    if (!procdir_dirent)
+      oom_failure();
     proc_cmdline_str = (char *) malloc(1024);
+    if (!proc_cmdline_str)
+      oom_failure();
     proc_cmdline_name = (char *) malloc(1024);
+    if (!proc_cmdline_name)
+      oom_failure();
     regc = malloc(1024);
+    if (!regc)
+      oom_failure();
   
     /* Catch signals */
     signal(SIGTERM, got_signal);
@@ -187,8 +223,19 @@ int main(int argc, char *argv[])
         }
 
         out_proc = fopen("/var/run/restartd.pid", "wt");
-        fprintf(out_proc, "%d", getpid());
-        fclose(out_proc);
+	if (!out_proc) {
+		syslog(LOG_ERR, "Failed to open /var/run/restartd.pid");
+		return -1;
+	}
+        if (fprintf(out_proc, "%d", getpid()) < 0) {
+          syslog(LOG_ERR, "Failed to write to /var/run/restartd.pid. Exiting.");
+          return -1;
+        }
+        if (fclose(out_proc) < 0) { /* errors can happen when flushing the buffer */
+          syslog(LOG_ERR, "Failed to write to /var/run/restartd.pid. Exiting.");
+          return -1;
+        }
+
 
         while(1) {
             if ((procdir_id = opendir("/proc")) == NULL) {
@@ -237,16 +284,23 @@ int main(int argc, char *argv[])
            now = time(NULL);
 
            out_proc = fopen("/var/run/restartd", "wt");
+           if (!out_proc) {
+             syslog(LOG_ERR, "Failed to open /var/run/restartd.pid");
+             return -1;
+           }
 
-           fprintf(out_proc, "%s\n", ctime(&now));
+           if (fprintf(out_proc, "%s\n", ctime(&now)) < 0) {
+             syslog(LOG_ERR, "Failed to write to /var/run/restartd. Exiting.");
+             return -1;
+           }
 
            for(i=0; i<config_process_number; i++) {
                if (strlen(config_process[i].processes) > 0) {
                    if (strlen(config_process[i].running) > 0) {
                        strcpy(config_process[i].status, "running");
-                       syslog(LOG_INFO, "%s is running, executing '%s'",
+                       /* syslog(LOG_INFO, "%s is running, executing '%s'",
                               config_process[i].name,
-                              config_process[i].running);
+                              config_process[i].running); */
                        system(config_process[i].running);
                     } else {
                         strcpy(config_process[i].status, "running");
@@ -267,12 +321,18 @@ int main(int argc, char *argv[])
                     strcpy(config_process[i].status, "not running");
                 }
 
-                fprintf(out_proc, "%-12s %-12s      %s\n",
-                        config_process[i].name, config_process[i].status,
-                        config_process[i].processes);
+                if (fprintf(out_proc, "%-12s %-12s      %s\n",
+                            config_process[i].name, config_process[i].status,
+                            config_process[i].processes) < 0) {
+                  syslog(LOG_ERR, "Failed to write to /var/run/restartd. Exiting.");
+                  return -1;
+                }
             }
 
-            fclose(out_proc);
+            if (fclose(out_proc) < 0) {
+              syslog(LOG_ERR, "Failed to write to /var/run/restartd.pid. Exiting.");
+              return -1;
+            }
 
             sleep(check_interval);
         }
-- 
2.37.3


debug log:

solving d279ebd8ff ...
found d279ebd8ff in https://git.savannah.gnu.org/cgit/guix.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).