all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [bug#57387] [PATCH] gnu: Add restartd.
@ 2022-08-24 15:59 Nicolas Graves via Guix-patches via
  2022-08-24 18:09 ` Maxime Devos
                   ` (6 more replies)
  0 siblings, 7 replies; 23+ messages in thread
From: Nicolas Graves via Guix-patches via @ 2022-08-24 15:59 UTC (permalink / raw)
  To: 57387; +Cc: ngraves

* gnu/packages/admin.scm (restartd): New variable.
---
 gnu/packages/admin.scm | 67 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 67 insertions(+)

diff --git a/gnu/packages/admin.scm b/gnu/packages/admin.scm
index a4c5189222..17a400bdf6 100644
--- a/gnu/packages/admin.scm
+++ b/gnu/packages/admin.scm
@@ -51,6 +51,7 @@
 ;;; Copyright © 2021 Artyom V. Poptsov <poptsov.artyom@gmail.com>
 ;;; Copyright © 2022 Wamm K. D. <jaft.r@outlook.com>
 ;;; Copyright © 2022 Roman Riabenko <roman@riabenko.com>
+;;; Copyright © 2022 Nicolas Graves <ngraves@ngraves.fr>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -5455,6 +5456,72 @@ (define-public fail2ban
 mechanisms if you really want to protect services.")
     (license license:gpl2+)))
 
+(define-public restartd
+  (let* ((commit "7044125ac55056f2663536f7137170edf92ebd75")
+         (revision "1.1"))
+    (package
+      (name "restartd")
+      (version (git-version "0.2.3" revision commit))
+      (source
+       (origin
+         (method git-fetch)
+         (uri (git-reference
+               (url "https://github.com/ajraymond/restartd")
+               (commit commit)))
+         (file-name (git-file-name name version))
+         (sha256
+          (base32
+           "1m1np00b4zvvwx63gzysbi38i5vj1jsjvh2s0p9czl6dzyz582z0"))
+         (patches
+          (list
+           ;; Fix segfault when run as normal user
+           (origin
+             (method url-fetch)
+             (uri
+              (string-append "https://patch-diff.githubusercontent.com/raw"
+                             "/ajraymond/restartd/pull/6.patch"))
+             (sha256
+              (base32
+               "1cqhy6fngvql9ynacrf4f2nc7mzypvdbab5nil96qlclfvb3far8")))
+           ;; Fix compilation with gcc-10+
+           (origin
+             (method url-fetch)
+             (uri
+              (string-append "https://patch-diff.githubusercontent.com/raw"
+                             "/ajraymond/restartd/pull/7.patch"))
+             (sha256
+              (base32
+               "0fk33af8sgrgxibmkyjlv3j8jikgbp4mkj84yamvhv38ic6x2rw6")))))))
+      (build-system gnu-build-system)
+      (arguments
+       (list
+        #:tests? #f ; no tests
+        #:phases
+        #~(modify-phases %standard-phases
+            (add-after 'unpack 'ensure-compilation
+              (lambda _
+                (substitute* "Makefile"
+                  (("CC \\?= gcc") "CC = gcc"))))
+            (delete  'configure)
+            (replace 'install
+              (lambda _
+                (install-file "restartd.conf" (string-append #$output "/etc"))
+                (install-file "restartd" (string-append #$output "/sbin"))
+                (install-file "restartd.8"
+                              (string-append #$output "/share/man/man8"))
+                (mkdir-p (string-append #$output "/share/man/fr/man8"))
+                (copy-file
+                 "restartd.fr.8"
+                 (string-append #$output "/share/man/fr/man8/restartd.8")))))))
+      (home-page "https://launchpad.net/debian/+source/restartd")
+      (synopsis "Daemon for restarting processes")
+      (description "This package provides a daemon for checking running and not
+running processes.  It reads the /proc directory every n seconds and does a
+POSIX regexp on the process names.  You can execute a script or a program if
+the process is or is not running.  The daemon can only be called by the root
+user, but can use @code{sudo -u user} in the process called if needed.")
+      (license license:gpl2))))
+
 (define-public rex
   (package
     (name "rex")
-- 
2.37.2


<#secure method=pgpmime mode=sign>

-- 
Best regards,
Nicolas Graves




^ permalink raw reply related	[flat|nested] 23+ messages in thread

* [bug#57387] [PATCH] gnu: Add restartd.
  2022-08-24 15:59 [bug#57387] [PATCH] gnu: Add restartd Nicolas Graves via Guix-patches via
@ 2022-08-24 18:09 ` Maxime Devos
  2022-08-24 18:14   ` ( via Guix-patches via
  2022-08-25  9:14 ` Nicolas Graves via Guix-patches via
                   ` (5 subsequent siblings)
  6 siblings, 1 reply; 23+ messages in thread
From: Maxime Devos @ 2022-08-24 18:09 UTC (permalink / raw)
  To: Nicolas Graves, 57387


[-- Attachment #1.1.1: Type: text/plain, Size: 1612 bytes --]

On 24-08-2022 17:59, Nicolas Graves via Guix-patches via wrote:

> +         (revision "1.1"))

"1.1" -> "1" ('revision' must be a string with a number, as expected by 
https://issues.guix.gnu.org/53144).

Also, why is an unreleased version used? I assume because of lack of 
activity upstream, but a comment would have been useful.

> +                (substitute* "Makefile"
> +                  (("CC \\?= gcc") "CC = gcc"))))
For cross-compilation, use (cc-for-target)

According to Debian, the home-page is 
<https://github.com/ajraymond/restartd>.

Looking at some source files, it appears to be gpl2-or-later, not gpl2-only.

In restartd.c, it is missing error checking for 'fopen', 'malloc' and 
'strdup'.

in config.c, it is missing error checking for 'malloc'  (and for printf 
on stderr, but there isn't much that can be odone in that case). Also 
printf on stdout, though less important.

On the patches: IIUC, in case additional commits are added to the PR, 
the patch is updated appropriately, so these patches are volatile. Even 
if that doesn't happen, the patch generation algorithm could change 
later. As such, copying these into gnu/packages/patches + search-pathes 
is recommended

> You can execute a script or a program if the process is or is not running.
Maybe: 'The daemon runs an user-provided script based once a program 
starts running or stops running.' That seems a little clearer English to 
me ('it is running or not running' is always true, and scripts / 
programs can be run, but it's not a claim about restartd).

Greetings,
Maxime.


[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 929 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 236 bytes --]

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [bug#57387] [PATCH] gnu: Add restartd.
  2022-08-24 18:09 ` Maxime Devos
@ 2022-08-24 18:14   ` ( via Guix-patches via
  0 siblings, 0 replies; 23+ messages in thread
From: ( via Guix-patches via @ 2022-08-24 18:14 UTC (permalink / raw)
  To: Maxime Devos, Nicolas Graves, 57387

On Wed Aug 24, 2022 at 7:09 PM BST, Maxime Devos wrote:
> > +                (substitute* "Makefile"
> > +                  (("CC \\?= gcc") "CC = gcc"))))
> For cross-compilation, use (cc-for-target)

Also, you can replace this substitution with make-flags:

  #:make-flags
  #~(list (string-append "CC=" #$(cc-for-target)))

    -- (




^ permalink raw reply	[flat|nested] 23+ messages in thread

* [bug#57387] [PATCH] gnu: Add restartd.
  2022-08-24 15:59 [bug#57387] [PATCH] gnu: Add restartd Nicolas Graves via Guix-patches via
  2022-08-24 18:09 ` Maxime Devos
@ 2022-08-25  9:14 ` Nicolas Graves via Guix-patches via
  2022-08-25  9:16 ` Nicolas Graves via Guix-patches via
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 23+ messages in thread
From: Nicolas Graves via Guix-patches via @ 2022-08-25  9:14 UTC (permalink / raw)
  To: 57387

* gnu/packages/admin.scm (restartd): New variable.
---
 gnu/packages/admin.scm                        | 53 +++++++++++++++++++
 .../patches/restartd-fix-compilation.patch    | 53 +++++++++++++++++++
 .../restartd-fix-segfault-normal-user.patch   | 45 ++++++++++++++++
 3 files changed, 151 insertions(+)
 create mode 100644 gnu/packages/patches/restartd-fix-compilation.patch
 create mode 100644 gnu/packages/patches/restartd-fix-segfault-normal-user.patch

diff --git a/gnu/packages/admin.scm b/gnu/packages/admin.scm
index a4c5189222..17ebd5fd7b 100644
--- a/gnu/packages/admin.scm
+++ b/gnu/packages/admin.scm
@@ -51,6 +51,7 @@
 ;;; Copyright © 2021 Artyom V. Poptsov <poptsov.artyom@gmail.com>
 ;;; Copyright © 2022 Wamm K. D. <jaft.r@outlook.com>
 ;;; Copyright © 2022 Roman Riabenko <roman@riabenko.com>
+;;; Copyright © 2022 Nicolas Graves <ngraves@ngraves.fr>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -5455,6 +5456,58 @@ (define-public fail2ban
 mechanisms if you really want to protect services.")
     (license license:gpl2+)))

+(define-public restartd
+  (let* ((commit "7044125ac55056f2663536f7137170edf92ebd75")
+         ;; Version is 0.2.4 in the version file in the repo
+         ;; but not in github tags.
+         ;; It is released as 0.2.3-1.1 for other distributions.
+         ;; Probably because of the lack of activity upstream.
+         (revision "1"))
+    (package
+      (name "restartd")
+      (version (git-version "0.2.3" revision commit))
+      (source
+       (origin
+         (method git-fetch)
+         (uri (git-reference
+               (url "https://github.com/ajraymond/restartd")
+               (commit commit)))
+         (file-name (git-file-name name version))
+         (sha256
+          (base32
+           "1m1np00b4zvvwx63gzysbi38i5vj1jsjvh2s0p9czl6dzyz582z0"))
+         (patches
+          (search-patches "restartd-fix-segfault-normal-user.patch"
+                          "restartd-fix-compilation.patch"))))
+      (build-system gnu-build-system)
+      (arguments
+       (list
+        #:tests? #f ; no tests
+        #:make-flags
+        #~(list (string-append "CC=" #$(cc-for-target)))
+        #:phases
+        #~(modify-phases %standard-phases
+            (delete  'configure)
+            (replace 'install
+              (lambda _
+                (install-file "restartd.conf" (string-append #$output "/etc"))
+                (install-file "restartd" (string-append #$output "/sbin"))
+                (install-file "restartd.8"
+                              (string-append #$output "/share/man/man8"))
+                (mkdir-p (string-append #$output "/share/man/fr/man8"))
+                (copy-file
+                 "restartd.fr.8"
+                 (string-append #$output "/share/man/fr/man8/restartd.8")))))))
+      (home-page "https://launchpad.net/debian/+source/restartd")
+      (synopsis "Daemon for restarting processes")
+      (description "This package provides a daemon for checking running and not
+running processes.  It reads the /proc directory every n seconds and does a
+POSIX regexp on the process names.  The daemon runs an user-provided script
+when it detects a program in the running processes, or an alternate script if
+it doesn't detect the program.  The daemon can only be called by the root
+user, but can use @code{sudo -u user} in the process called if needed.")
+      (license license:gpl2+))))
+
 (define-public rex
   (package
     (name "rex")
diff --git a/gnu/packages/patches/restartd-fix-compilation.patch b/gnu/packages/patches/restartd-fix-compilation.patch
new file mode 100644
index 0000000000..a380205675
--- /dev/null
+++ b/gnu/packages/patches/restartd-fix-compilation.patch
@@ -0,0 +1,53 @@
+From 84edc2042bb0a82e65bd4d165b706519886b4075 Mon Sep 17 00:00:00 2001
+From: Sudip Mukherjee <sudipm.mukherjee@gmail.com>
+Date: Mon, 10 Aug 2020 19:39:30 +0100
+Subject: [PATCH] Fix build with gcc-10
+
+Bug: https://bugs.debian.org/957761
+Signed-off-by: Sudip Mukherjee <sudipm.mukherjee@gmail.com>
+---
+ config.h   | 12 ++++++------
+ restartd.c |  7 +++++++
+ 2 files changed, 13 insertions(+), 6 deletions(-)
+
+diff --git a/config.h b/config.h
+index fabaa2b..a79b28e 100644
+--- a/config.h
++++ b/config.h
+@@ -25,12 +25,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];
+diff --git a/restartd.c b/restartd.c
+index 2aa720c..617b298 100644
+--- a/restartd.c
++++ b/restartd.c
+@@ -35,6 +35,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)
+ {
diff --git a/gnu/packages/patches/restartd-fix-segfault-normal-user.patch b/gnu/packages/patches/restartd-fix-segfault-normal-user.patch
new file mode 100644
index 0000000000..5c28c3b2ad
--- /dev/null
+++ b/gnu/packages/patches/restartd-fix-segfault-normal-user.patch
@@ -0,0 +1,45 @@
+From 67d7308ba52d2c605d0175c14c34ff09c9d322fb Mon Sep 17 00:00:00 2001
+From: Yin Kangkai <yinkangkai@xiaomi.com>
+Date: Sat, 1 Oct 2016 13:52:58 +0800
+Subject: [PATCH] Fix segfault when run as normal user
+
+Also exit immediately when "restartd -h"
+---
+ restartd.c | 9 +++++++--
+ 1 file changed, 7 insertions(+), 2 deletions(-)
+
+diff --git a/restartd.c b/restartd.c
+index 2aa720c..1d46fe1 100644
+--- a/restartd.c
++++ b/restartd.c
+@@ -118,6 +118,7 @@ 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);
+         }
+     }
+
+@@ -187,6 +188,10 @@ int main(int argc, char *argv[])
+         }
+
+         out_proc = fopen("/var/run/restartd.pid", "wt");
++	if (!out_proc) {
++		syslog(LOG_ERR, "Failed to open /var/run/restartd.pid");
++		return -1;
++	}
+         fprintf(out_proc, "%d", getpid());
+         fclose(out_proc);
+
+@@ -244,9 +249,9 @@ int main(int argc, char *argv[])
+                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");
--
2.37.2




^ permalink raw reply related	[flat|nested] 23+ messages in thread

* [bug#57387] [PATCH] gnu: Add restartd.
  2022-08-24 15:59 [bug#57387] [PATCH] gnu: Add restartd Nicolas Graves via Guix-patches via
  2022-08-24 18:09 ` Maxime Devos
  2022-08-25  9:14 ` Nicolas Graves via Guix-patches via
@ 2022-08-25  9:16 ` Nicolas Graves via Guix-patches via
  2022-08-25 10:34   ` Maxime Devos
  2022-08-25 11:33   ` Maxime Devos
  2022-08-26  7:32 ` Nicolas Graves via Guix-patches via
                   ` (3 subsequent siblings)
  6 siblings, 2 replies; 23+ messages in thread
From: Nicolas Graves via Guix-patches via @ 2022-08-25  9:16 UTC (permalink / raw)
  To: 57387


Made suggested changes, rephrased the description.
But I never learned C and am not at ease for adding checks in C source
code, sorry.

-- 
Best regards,
Nicolas Graves




^ permalink raw reply	[flat|nested] 23+ messages in thread

* [bug#57387] [PATCH] gnu: Add restartd.
  2022-08-25  9:16 ` Nicolas Graves via Guix-patches via
@ 2022-08-25 10:34   ` Maxime Devos
  2022-08-25 11:33   ` Maxime Devos
  1 sibling, 0 replies; 23+ messages in thread
From: Maxime Devos @ 2022-08-25 10:34 UTC (permalink / raw)
  To: Nicolas Graves, 57387


[-- Attachment #1.1.1: Type: text/plain, Size: 245 bytes --]


On 25-08-2022 11:16, Nicolas Graves via Guix-patches via wrote:
> Made suggested changes, rephrased the description.
> But I never learned C and am not at ease for adding checks in C source
> code, sorry.
>
I'll give it a try myself ...

[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 929 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 236 bytes --]

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [bug#57387] [PATCH] gnu: Add restartd.
  2022-08-25  9:16 ` Nicolas Graves via Guix-patches via
  2022-08-25 10:34   ` Maxime Devos
@ 2022-08-25 11:33   ` Maxime Devos
  1 sibling, 0 replies; 23+ messages in thread
From: Maxime Devos @ 2022-08-25 11:33 UTC (permalink / raw)
  To: Nicolas Graves, 57387


[-- Attachment #1.1.1.1: Type: text/plain, Size: 384 bytes --]


On 25-08-2022 11:16, Nicolas Graves via Guix-patches via wrote:
> Made suggested changes, rephrased the description.
> But I never learned C and am not at ease for adding checks in C source
> code, sorry.
>
I have made a PR at <https://github.com/ajraymond/restartd/pull/8> 
<https://github.com/ajraymond/restartd/pull/8> adding some error checking.

Greetings,
Maxime.


[-- Attachment #1.1.1.2: Type: text/html, Size: 817 bytes --]

[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 929 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 236 bytes --]

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [bug#57387] [PATCH] gnu: Add restartd.
  2022-08-24 15:59 [bug#57387] [PATCH] gnu: Add restartd Nicolas Graves via Guix-patches via
                   ` (2 preceding siblings ...)
  2022-08-25  9:16 ` Nicolas Graves via Guix-patches via
@ 2022-08-26  7:32 ` Nicolas Graves via Guix-patches via
  2022-08-26 14:03   ` Maxime Devos
  2022-08-26 22:16 ` Nicolas Graves via Guix-patches via
                   ` (2 subsequent siblings)
  6 siblings, 1 reply; 23+ messages in thread
From: Nicolas Graves via Guix-patches via @ 2022-08-26  7:32 UTC (permalink / raw)
  To: 57387

* gnu/packages/admin.scm (restartd): New variable.
---
 gnu/packages/admin.scm                        |  54 ++++
 ...td-add-error-handling-for-robustness.patch | 232 ++++++++++++++++++
 .../patches/restartd-fix-compilation.patch    |  53 ++++
 3 files changed, 339 insertions(+)
 create mode 100644 gnu/packages/patches/restartd-add-error-handling-for-robustness.patch
 create mode 100644 gnu/packages/patches/restartd-fix-compilation.patch

diff --git a/gnu/packages/admin.scm b/gnu/packages/admin.scm
index a4c5189222..57de594bee 100644
--- a/gnu/packages/admin.scm
+++ b/gnu/packages/admin.scm
@@ -51,6 +51,7 @@
 ;;; Copyright © 2021 Artyom V. Poptsov <poptsov.artyom@gmail.com>
 ;;; Copyright © 2022 Wamm K. D. <jaft.r@outlook.com>
 ;;; Copyright © 2022 Roman Riabenko <roman@riabenko.com>
+;;; Copyright © 2022 Nicolas Graves <ngraves@ngraves.fr>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -5455,6 +5456,59 @@ (define-public fail2ban
 mechanisms if you really want to protect services.")
     (license license:gpl2+)))

+(define-public restartd
+  (let* ((commit "7044125ac55056f2663536f7137170edf92ebd75")
+         ;; Version is 0.2.4 in the version file in the repo
+         ;; but not in github tags.
+         ;; It is released as 0.2.3-1.1 for other distributions.
+         ;; Probably because of the lack of activity upstream.
+         (revision "1"))
+    (package
+      (name "restartd")
+      (version (git-version "0.2.3" revision commit))
+      (source
+       (origin
+         (method git-fetch)
+         (uri (git-reference
+               (url "https://github.com/ajraymond/restartd")
+               (commit commit)))
+         (file-name (git-file-name name version))
+         (sha256
+          (base32
+           "1m1np00b4zvvwx63gzysbi38i5vj1jsjvh2s0p9czl6dzyz582z0"))
+         (patches
+          (search-patches
+           "restartd-fix-compilation.patch"
+           "restartd-add-error-handling-for-robustness.patch"))))
+      (build-system gnu-build-system)
+      (arguments
+       (list
+        #:tests? #f ; no tests
+        #:make-flags
+        #~(list (string-append "CC=" #$(cc-for-target)))
+        #:phases
+        #~(modify-phases %standard-phases
+            (delete  'configure)
+            (replace 'install
+              (lambda _
+                (install-file "restartd.conf" (string-append #$output "/etc"))
+                (install-file "restartd" (string-append #$output "/sbin"))
+                (install-file "restartd.8"
+                              (string-append #$output "/share/man/man8"))
+                (mkdir-p (string-append #$output "/share/man/fr/man8"))
+                (copy-file
+                 "restartd.fr.8"
+                 (string-append #$output "/share/man/fr/man8/restartd.8")))))))
+      (home-page "https://launchpad.net/debian/+source/restartd")
+      (synopsis "Daemon for restarting processes")
+      (description "This package provides a daemon for checking running and not
+running processes.  It reads the /proc directory every n seconds and does a
+POSIX regexp on the process names.  The daemon runs an user-provided script
+when it detects a program in the running processes, or an alternate script if
+it doesn't detect the program.  The daemon can only be called by the root
+user, but can use @code{sudo -u user} in the process called if needed.")
+      (license license:gpl2+))))
+
 (define-public rex
   (package
     (name "rex")
diff --git a/gnu/packages/patches/restartd-add-error-handling-for-robustness.patch b/gnu/packages/patches/restartd-add-error-handling-for-robustness.patch
new file mode 100644
index 0000000000..f0abcf1dac
--- /dev/null
+++ b/gnu/packages/patches/restartd-add-error-handling-for-robustness.patch
@@ -0,0 +1,232 @@
+From ec606eb15091ff8ffa672500413128c29814f8ad Mon Sep 17 00:00:00 2001
+From: Yin Kangkai <yinkangkai@xiaomi.com>, Maxime Devos <maximedevos@telenet.be>
+Date: Thu, 25 Aug 2022 14:29:25 +0200
+Subject: [PATCH] Add some error handling for robustness.
+
+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?
+This patch also avoids segmentation fault when run from non-root user.
+---
+ config.c   | 19 ++++++++++----
+ config.h   |  2 ++
+ restartd.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++--------
+ 3 files changed, 79 insertions(+), 15 deletions(-)
+
+diff --git a/config.c b/config.c
+index f307b8a..94907e3 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..b5a134a 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
+@@ -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..df0a6eb 100644
+--- a/restartd.c
++++ b/restartd.c
+@@ -1,6 +1,8 @@
+ /* 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) 2016 Yin Kangkai <yinkangkai@xiaomi.com>
++ * 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
+@@ -52,6 +54,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 +88,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 +137,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 +155,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 +217,18 @@ 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 +277,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");
++	     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 +314,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.2
+
diff --git a/gnu/packages/patches/restartd-fix-compilation.patch b/gnu/packages/patches/restartd-fix-compilation.patch
new file mode 100644
index 0000000000..a380205675
--- /dev/null
+++ b/gnu/packages/patches/restartd-fix-compilation.patch
@@ -0,0 +1,53 @@
+From 84edc2042bb0a82e65bd4d165b706519886b4075 Mon Sep 17 00:00:00 2001
+From: Sudip Mukherjee <sudipm.mukherjee@gmail.com>
+Date: Mon, 10 Aug 2020 19:39:30 +0100
+Subject: [PATCH] Fix build with gcc-10
+
+Bug: https://bugs.debian.org/957761
+Signed-off-by: Sudip Mukherjee <sudipm.mukherjee@gmail.com>
+---
+ config.h   | 12 ++++++------
+ restartd.c |  7 +++++++
+ 2 files changed, 13 insertions(+), 6 deletions(-)
+
+diff --git a/config.h b/config.h
+index fabaa2b..a79b28e 100644
+--- a/config.h
++++ b/config.h
+@@ -25,12 +25,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];
+diff --git a/restartd.c b/restartd.c
+index 2aa720c..617b298 100644
+--- a/restartd.c
++++ b/restartd.c
+@@ -35,6 +35,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)
+ {
--
2.37.2


<#secure method=pgpmime mode=sign>

--
Best regards,
Nicolas Graves




^ permalink raw reply related	[flat|nested] 23+ messages in thread

* [bug#57387] [PATCH] gnu: Add restartd.
  2022-08-26  7:32 ` Nicolas Graves via Guix-patches via
@ 2022-08-26 14:03   ` Maxime Devos
  2022-08-26 16:57     ` Nicolas Graves via Guix-patches via
  0 siblings, 1 reply; 23+ messages in thread
From: Maxime Devos @ 2022-08-26 14:03 UTC (permalink / raw)
  To: Nicolas Graves, 57387


[-- Attachment #1.1.1: Type: text/plain, Size: 2252 bytes --]

On 26-08-2022 09:32, Nicolas Graves via Guix-patches via wrote:

> [...]
> +         (patches
> +          (search-patches
> +           "restartd-fix-compilation.patch"
> +           "restartd-add-error-handling-for-robustness.patch"))))
> [...]
>   (define-public rex
>     (package
>       (name "rex")
> diff --git a/gnu/packages/patches/restartd-add-error-handling-for-robustness.patch b/gnu/packages/patches/restartd-add-error-handling-for-robustness.patch
> new file mode 100644
> index 0000000000..f0abcf1dac
> --- /dev/null
> +++ b/gnu/packages/patches/restartd-add-error-handling-for-robustness.patch
> @@ -0,0 +1,232 @@
> +From ec606eb15091ff8ffa672500413128c29814f8ad Mon Sep 17 00:00:00 2001
> +From: Yin Kangkai <yinkangkai@xiaomi.com>, Maxime Devos <maximedevos@telenet.be>
A bit of a nitpick, but Yin Kagkai was not involved in this patch, I 
wrote it myself ...
> +Date: Thu, 25 Aug 2022 14:29:25 +0200
> +Subject: [PATCH] Add some error handling for robustness.
> +
> +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?
> +This patch also avoids segmentation fault when run from non-root user.
> +---
> +[..]
> +            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); */

... the commenting syslog thing was an unrelated patch, maybe various 
commits were squashed?

When I tried out the patch, I added a local copy of 
https://patch-diff.githubusercontent.com/raw/ajraymond/restartd/pull/8.patch 
to the list of patches and "guix build" succeeded, there was no need to 
squash things.

Greetings,
Maaxime.



[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 929 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 236 bytes --]

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [bug#57387] [PATCH] gnu: Add restartd.
  2022-08-26 14:03   ` Maxime Devos
@ 2022-08-26 16:57     ` Nicolas Graves via Guix-patches via
  2022-08-26 19:07       ` Maxime Devos
  0 siblings, 1 reply; 23+ messages in thread
From: Nicolas Graves via Guix-patches via @ 2022-08-26 16:57 UTC (permalink / raw)
  To: Maxime Devos, 57387


> A bit of a nitpick, but Yin Kagkai was not involved in this patch, I
> wrote it myself ...
> ... the commenting syslog thing was an unrelated patch, maybe various
> commits were squashed?

Sorry, I should have explained.
Applying both pull requests 6 and 8 failed because 3 lines were common
to both.

I resolved that by merging both and recreating a new patch, thus giving
credit to both authors. Is that OK ?

-- 
Best regards,
Nicolas Graves




^ permalink raw reply	[flat|nested] 23+ messages in thread

* [bug#57387] [PATCH] gnu: Add restartd.
  2022-08-26 16:57     ` Nicolas Graves via Guix-patches via
@ 2022-08-26 19:07       ` Maxime Devos
  0 siblings, 0 replies; 23+ messages in thread
From: Maxime Devos @ 2022-08-26 19:07 UTC (permalink / raw)
  To: Nicolas Graves, 57387


[-- Attachment #1.1.1.1: Type: text/plain, Size: 930 bytes --]

On 26-08-2022 18:57, Nicolas Graves wrote:

>> A bit of a nitpick, but Yin Kagkai was not involved in this patch, I
>> wrote it myself ...
>> ... the commenting syslog thing was an unrelated patch, maybe various
>> commits were squashed?
> Sorry, I should have explained.
> Applying both pull requests 6 and 8 failed because 3 lines were common
> to both.
Hm, I didn't encounter that, maybe I accidentally removed one of the 
pre-existing patches or even removed my own patch.
> I resolved that by merging both and recreating a new patch, thus giving
> credit to both authors. Is that OK ?

OK, but some commit messages are getting lost, maybe add a link to the 
two upstream PRs to make clear it's a combination of multiple 
independent -- Yin Kangkai did some presumably useful things, but it's 
not adding error handling unlike what the current patch message and file 
name implies.

Greetings,
Maxime.


[-- Attachment #1.1.1.2: Type: text/html, Size: 1526 bytes --]

[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 929 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 236 bytes --]

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [bug#57387] [PATCH] gnu: Add restartd.
  2022-08-24 15:59 [bug#57387] [PATCH] gnu: Add restartd Nicolas Graves via Guix-patches via
                   ` (3 preceding siblings ...)
  2022-08-26  7:32 ` Nicolas Graves via Guix-patches via
@ 2022-08-26 22:16 ` Nicolas Graves via Guix-patches via
  2022-08-28 17:38   ` Maxime Devos
                     ` (2 more replies)
  2024-02-03 13:36 ` [bug#57387] [PATCH v2] " Nicolas Graves via Guix-patches via
  2024-02-03 14:06 ` [bug#57387] [PATCH v3] " Nicolas Graves via Guix-patches via
  6 siblings, 3 replies; 23+ messages in thread
From: Nicolas Graves via Guix-patches via @ 2022-08-26 22:16 UTC (permalink / raw)
  To: 57387

* gnu/packages/admin.scm (restartd): New variable.
---
 gnu/packages/admin.scm                        |  54 ++++
 ...td-add-error-handling-for-robustness.patch | 242 ++++++++++++++++++
 .../patches/restartd-fix-compilation.patch    |  53 ++++
 3 files changed, 349 insertions(+)
 create mode 100644 gnu/packages/patches/restartd-add-error-handling-for-robustness.patch
 create mode 100644 gnu/packages/patches/restartd-fix-compilation.patch

diff --git a/gnu/packages/admin.scm b/gnu/packages/admin.scm
index a4c5189222..57de594bee 100644
--- a/gnu/packages/admin.scm
+++ b/gnu/packages/admin.scm
@@ -51,6 +51,7 @@
 ;;; Copyright © 2021 Artyom V. Poptsov <poptsov.artyom@gmail.com>
 ;;; Copyright © 2022 Wamm K. D. <jaft.r@outlook.com>
 ;;; Copyright © 2022 Roman Riabenko <roman@riabenko.com>
+;;; Copyright © 2022 Nicolas Graves <ngraves@ngraves.fr>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -5455,6 +5456,59 @@ (define-public fail2ban
 mechanisms if you really want to protect services.")
     (license license:gpl2+)))

+(define-public restartd
+  (let* ((commit "7044125ac55056f2663536f7137170edf92ebd75")
+         ;; Version is 0.2.4 in the version file in the repo
+         ;; but not in github tags.
+         ;; It is released as 0.2.3-1.1 for other distributions.
+         ;; Probably because of the lack of activity upstream.
+         (revision "1"))
+    (package
+      (name "restartd")
+      (version (git-version "0.2.3" revision commit))
+      (source
+       (origin
+         (method git-fetch)
+         (uri (git-reference
+               (url "https://github.com/ajraymond/restartd")
+               (commit commit)))
+         (file-name (git-file-name name version))
+         (sha256
+          (base32
+           "1m1np00b4zvvwx63gzysbi38i5vj1jsjvh2s0p9czl6dzyz582z0"))
+         (patches
+          (search-patches
+           "restartd-fix-compilation.patch"
+           "restartd-add-error-handling-for-robustness.patch"))))
+      (build-system gnu-build-system)
+      (arguments
+       (list
+        #:tests? #f ; no tests
+        #:make-flags
+        #~(list (string-append "CC=" #$(cc-for-target)))
+        #:phases
+        #~(modify-phases %standard-phases
+            (delete  'configure)
+            (replace 'install
+              (lambda _
+                (install-file "restartd.conf" (string-append #$output "/etc"))
+                (install-file "restartd" (string-append #$output "/sbin"))
+                (install-file "restartd.8"
+                              (string-append #$output "/share/man/man8"))
+                (mkdir-p (string-append #$output "/share/man/fr/man8"))
+                (copy-file
+                 "restartd.fr.8"
+                 (string-append #$output "/share/man/fr/man8/restartd.8")))))))
+      (home-page "https://launchpad.net/debian/+source/restartd")
+      (synopsis "Daemon for restarting processes")
+      (description "This package provides a daemon for checking running and not
+running processes.  It reads the /proc directory every n seconds and does a
+POSIX regexp on the process names.  The daemon runs an user-provided script
+when it detects a program in the running processes, or an alternate script if
+it doesn't detect the program.  The daemon can only be called by the root
+user, but can use @code{sudo -u user} in the process called if needed.")
+      (license license:gpl2+))))
+
 (define-public rex
   (package
     (name "rex")
diff --git a/gnu/packages/patches/restartd-add-error-handling-for-robustness.patch b/gnu/packages/patches/restartd-add-error-handling-for-robustness.patch
new file mode 100644
index 0000000000..33f5f06555
--- /dev/null
+++ b/gnu/packages/patches/restartd-add-error-handling-for-robustness.patch
@@ -0,0 +1,242 @@
+From ec606eb15091ff8ffa672500413128c29814f8ad Mon Sep 17 00:00:00 2001
+From: Maxime Devos <maximedevos@telenet.be>, Yin Kangkai <yinkangkai@xiaomi.com>
+Date: Thu, 25 Aug 2022 14:29:25 +0200
+Subject: [PATCH] Add some error handling for robustness.
+
+This patch is a merge of two pull requests for the restartd project.
+
+Maxime Devos <maximedevos@telenet.be>
+https://github.com/ajraymond/restartd/pull/8
+
+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?
+
+Yin Kangkai <yinkangkai@xiaomi.com>
+https://github.com/ajraymond/restartd/pull/6
+
+This patch also avoids segmentation fault when run from non-root user.
+
+---
+ config.c   | 19 ++++++++++----
+ config.h   |  2 ++
+ restartd.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++--------
+ 3 files changed, 79 insertions(+), 15 deletions(-)
+
+diff --git a/config.c b/config.c
+index f307b8a..94907e3 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..b5a134a 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
+@@ -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..df0a6eb 100644
+--- a/restartd.c
++++ b/restartd.c
+@@ -1,6 +1,8 @@
+ /* 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) 2016 Yin Kangkai <yinkangkai@xiaomi.com>
++ * 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
+@@ -52,6 +54,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 +88,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 +137,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 +155,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 +217,18 @@ 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 +277,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");
++	     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 +314,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.2
+
diff --git a/gnu/packages/patches/restartd-fix-compilation.patch b/gnu/packages/patches/restartd-fix-compilation.patch
new file mode 100644
index 0000000000..a380205675
--- /dev/null
+++ b/gnu/packages/patches/restartd-fix-compilation.patch
@@ -0,0 +1,53 @@
+From 84edc2042bb0a82e65bd4d165b706519886b4075 Mon Sep 17 00:00:00 2001
+From: Sudip Mukherjee <sudipm.mukherjee@gmail.com>
+Date: Mon, 10 Aug 2020 19:39:30 +0100
+Subject: [PATCH] Fix build with gcc-10
+
+Bug: https://bugs.debian.org/957761
+Signed-off-by: Sudip Mukherjee <sudipm.mukherjee@gmail.com>
+---
+ config.h   | 12 ++++++------
+ restartd.c |  7 +++++++
+ 2 files changed, 13 insertions(+), 6 deletions(-)
+
+diff --git a/config.h b/config.h
+index fabaa2b..a79b28e 100644
+--- a/config.h
++++ b/config.h
+@@ -25,12 +25,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];
+diff --git a/restartd.c b/restartd.c
+index 2aa720c..617b298 100644
+--- a/restartd.c
++++ b/restartd.c
+@@ -35,6 +35,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)
+ {
--
2.37.2

--
Best regards,
Nicolas Graves




^ permalink raw reply related	[flat|nested] 23+ messages in thread

* [bug#57387] [PATCH] gnu: Add restartd.
  2022-08-26 22:16 ` Nicolas Graves via Guix-patches via
@ 2022-08-28 17:38   ` Maxime Devos
  2022-09-11 14:53   ` Mathieu Othacehe
  2022-09-14  7:48   ` Mathieu Othacehe
  2 siblings, 0 replies; 23+ messages in thread
From: Maxime Devos @ 2022-08-28 17:38 UTC (permalink / raw)
  To: Nicolas Graves, 57387


[-- Attachment #1.1.1: Type: text/plain, Size: 65 bytes --]

This merge seems ok to me.

Thanks,
Maxime (not a committer)

[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 929 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 236 bytes --]

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [bug#57387] [PATCH] gnu: Add restartd.
  2022-08-26 22:16 ` Nicolas Graves via Guix-patches via
  2022-08-28 17:38   ` Maxime Devos
@ 2022-09-11 14:53   ` Mathieu Othacehe
  2022-09-11 18:13     ` Nicolas Graves via Guix-patches via
  2022-09-14  7:48   ` Mathieu Othacehe
  2 siblings, 1 reply; 23+ messages in thread
From: Mathieu Othacehe @ 2022-09-11 14:53 UTC (permalink / raw)
  To: Nicolas Graves; +Cc: 57387


Hello,

> * gnu/packages/admin.scm (restartd): New variable.

I have the following error when trying to build the package:

--8<---------------cut here---------------start------------->8---
2 out of 8 hunks FAILED -- saving rejects to file restartd.c.rej
source is at 'restartd-0.2.3-1.7044125-checkout'
applying '/gnu/store/gayczr3cvcxlqafxz9pclk6b8jhxhl75-restartd-fix-compilation.patch'...
applying '/gnu/store/d1sllhia0ninbfrq9agw2272dwm1v103-restartd-add-error-handling-for-robustness.patch'...
Backtrace:
           5 (primitive-load "/gnu/store/skbm6f65x56bhq5sd8w7fys5q49…")
In ice-9/eval.scm:
    619:8  4 (_ #(#(#<directory (guile-user) 7ffff3fd7c80> "res…") #))
In ice-9/boot-9.scm:
    142:2  3 (dynamic-wind #<procedure 7ffff3f862c0 at ice-9/eval.s…> …)
In ice-9/eval.scm:
    619:8  2 (_ #(#(#<directory (guile-user) 7ffff3fd7c80>)))
In srfi/srfi-1.scm:
    634:9  1 (for-each #<procedure apply-patch (a)> _)
In guix/build/utils.scm:
    762:6  0 (invoke "/gnu/store/z39hnrwds1dgcbpfgj8dnv2cngjb2xbl-p…" …)

guix/build/utils.scm:762:6: In procedure invoke:
ERROR:
  1. &invoke-error:
      program: "/gnu/store/z39hnrwds1dgcbpfgj8dnv2cngjb2xbl-patch-2.7.6/bin/patch"
      arguments: ("--force" "--no-backup-if-mismatch" "-p1" "--input" "/gnu/store/d1sllhia0ninbfrq9agw2272dwm1v103-restartd-add-error-handling-for-robustness.patch")
      exit-status: 1
      term-signal: #f
      stop-signal: #f
--8<---------------cut here---------------end--------------->8---

Mathieu




^ permalink raw reply	[flat|nested] 23+ messages in thread

* [bug#57387] [PATCH] gnu: Add restartd.
  2022-09-11 14:53   ` Mathieu Othacehe
@ 2022-09-11 18:13     ` Nicolas Graves via Guix-patches via
  2022-09-12  7:36       ` Mathieu Othacehe
  0 siblings, 1 reply; 23+ messages in thread
From: Nicolas Graves via Guix-patches via @ 2022-09-11 18:13 UTC (permalink / raw)
  To: Mathieu Othacehe; +Cc: 57387


On 2022-09-11 16:53, Mathieu Othacehe wrote:
> I have the following error when trying to build the package:
>
> --8<---------------cut here---------------start------------->8---
> 2 out of 8 hunks FAILED -- saving rejects to file restartd.c.rej

I can't reproduce, don't have the error when compiling again with
--check. Can you provide contents of restartd.c.rej?

Thanks!

Nicolas

-- 
Best regards,
Nicolas Graves




^ permalink raw reply	[flat|nested] 23+ messages in thread

* [bug#57387] [PATCH] gnu: Add restartd.
  2022-09-11 18:13     ` Nicolas Graves via Guix-patches via
@ 2022-09-12  7:36       ` Mathieu Othacehe
  2022-09-13  8:45         ` Nicolas Graves via Guix-patches via
  0 siblings, 1 reply; 23+ messages in thread
From: Mathieu Othacehe @ 2022-09-12  7:36 UTC (permalink / raw)
  To: Nicolas Graves; +Cc: 57387

[-- Attachment #1: Type: text/plain, Size: 167 bytes --]


Hello,

> I can't reproduce, don't have the error when compiling again with
> --check. Can you provide contents of restartd.c.rej?

Please find it attached.

Mathieu

[-- Attachment #2: restartd.c.rej --]
[-- Type: application/octet-stream, Size: 1010 bytes --]

--- restartd.c
+++ restartd.c
@@ -137,10 +156,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) {
@@ -152,9 +174,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);

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [bug#57387] [PATCH] gnu: Add restartd.
  2022-09-12  7:36       ` Mathieu Othacehe
@ 2022-09-13  8:45         ` Nicolas Graves via Guix-patches via
  0 siblings, 0 replies; 23+ messages in thread
From: Nicolas Graves via Guix-patches via @ 2022-09-13  8:45 UTC (permalink / raw)
  To: Mathieu Othacehe; +Cc: 57387


> Please find it attached.
Thanks!

I don't understand why there should be a reject, just by looking at the
patches, I don't see why these hunks would collide.

I don't have a clue on how to fix this. I'll merge both locally
and regenerating a single patch that by definition can't collide.

-- 
Best regards,
Nicolas Graves




^ permalink raw reply	[flat|nested] 23+ messages in thread

* [bug#57387] [PATCH] gnu: Add restartd.
  2022-08-26 22:16 ` Nicolas Graves via Guix-patches via
  2022-08-28 17:38   ` Maxime Devos
  2022-09-11 14:53   ` Mathieu Othacehe
@ 2022-09-14  7:48   ` Mathieu Othacehe
  2024-02-03 14:07     ` Nicolas Graves via Guix-patches via
  2 siblings, 1 reply; 23+ messages in thread
From: Mathieu Othacehe @ 2022-09-14  7:48 UTC (permalink / raw)
  To: Nicolas Graves; +Cc: 57387


Hello Nicolas,

>  ...td-add-error-handling-for-robustness.patch | 242 ++++++++++++++++++
>  .../patches/restartd-fix-compilation.patch    |  53 ++++

Both patches also need to be added to the dist_patch_DATA variable of
gnu/local.mk.

Thanks,

Mathieu




^ permalink raw reply	[flat|nested] 23+ messages in thread

* [bug#57387] [PATCH] gnu: Add restartd.
       [not found] <87edvu1hy9.fsf@ngraves.fr>
@ 2022-09-29 13:14 ` Nicolas Graves via Guix-patches via
  0 siblings, 0 replies; 23+ messages in thread
From: Nicolas Graves via Guix-patches via @ 2022-09-29 13:14 UTC (permalink / raw)
  To: 57387; +Cc: othacehe, maximedevos

* gnu/packages/admin.scm (restartd): New variable.
---
 gnu/packages/admin.scm                        |  51 +++
 .../patches/restartd-update-robust.patch      | 295 ++++++++++++++++++
 2 files changed, 346 insertions(+)
 create mode 100644 gnu/packages/patches/restartd-update-robust.patch

diff --git a/gnu/packages/admin.scm b/gnu/packages/admin.scm
index 1a213adfdd..c696384211 100644
--- a/gnu/packages/admin.scm
+++ b/gnu/packages/admin.scm
@@ -53,6 +53,7 @@
 ;;; Copyright © 2022 Roman Riabenko <roman@riabenko.com>
 ;;; Copyright © 2022 Petr Hodina <phodina@protonmail.com>
 ;;; Copyright © 2022 Andreas Rammhold <andreas@rammhold.de>
+;;; Copyright © 2022 Nicolas Graves <ngraves@ngraves.fr>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -5489,6 +5490,56 @@ (define-public fail2ban
 mechanisms if you really want to protect services.")
     (license license:gpl2+)))
 
+(define-public restartd
+  (let* ((commit "7044125ac55056f2663536f7137170edf92ebd75")
+         ;; Version is 0.2.4 in the version file in the repo
+         ;; but not in github tags.
+         ;; It is released as 0.2.3-1.1 for other distributions.
+         ;; Probably because of the lack of activity upstream.
+         (revision "1"))
+    (package
+      (name "restartd")
+      (version (git-version "0.2.3" revision commit))
+      (source
+       (origin
+         (method git-fetch)
+         (uri (git-reference
+               (url "https://github.com/ajraymond/restartd")
+               (commit commit)))
+         (file-name (git-file-name name version))
+         (sha256
+          (base32
+           "1m1np00b4zvvwx63gzysbi38i5vj1jsjvh2s0p9czl6dzyz582z0"))
+         (patches (search-patches "restartd-update-robust.patch"))))
+      (build-system gnu-build-system)
+      (arguments
+       (list
+        #:tests? #f ; no tests
+        #:make-flags
+        #~(list (string-append "CC=" #$(cc-for-target)))
+        #:phases
+        #~(modify-phases %standard-phases
+            (delete  'configure)
+            (replace 'install
+              (lambda _
+                (install-file "restartd.conf" (string-append #$output "/etc"))
+                (install-file "restartd" (string-append #$output "/sbin"))
+                (install-file "restartd.8"
+                              (string-append #$output "/share/man/man8"))
+                (mkdir-p (string-append #$output "/share/man/fr/man8"))
+                (copy-file
+                 "restartd.fr.8"
+                 (string-append #$output "/share/man/fr/man8/restartd.8")))))))
+      (home-page "https://launchpad.net/debian/+source/restartd")
+      (synopsis "Daemon for restarting processes")
+      (description "This package provides a daemon for checking running and not
+running processes.  It reads the /proc directory every n seconds and does a
+POSIX regexp on the process names.  The daemon runs an user-provided script
+when it detects a program in the running processes, or an alternate script if
+it doesn't detect the program.  The daemon can only be called by the root
+user, but can use @code{sudo -u user} in the process called if needed.")
+      (license license:gpl2+))))
+
 (define-public rex
   (package
     (name "rex")
diff --git a/gnu/packages/patches/restartd-update-robust.patch b/gnu/packages/patches/restartd-update-robust.patch
new file mode 100644
index 0000000000..d279ebd8ff
--- /dev/null
+++ b/gnu/packages/patches/restartd-update-robust.patch
@@ -0,0 +1,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
+

-- 
Best regards,
Nicolas Graves




^ permalink raw reply related	[flat|nested] 23+ messages in thread

* [bug#57387] [PATCH v2] gnu: Add restartd.
  2022-08-24 15:59 [bug#57387] [PATCH] gnu: Add restartd Nicolas Graves via Guix-patches via
                   ` (4 preceding siblings ...)
  2022-08-26 22:16 ` Nicolas Graves via Guix-patches via
@ 2024-02-03 13:36 ` Nicolas Graves via Guix-patches via
  2024-02-03 14:06 ` [bug#57387] [PATCH v3] " Nicolas Graves via Guix-patches via
  6 siblings, 0 replies; 23+ messages in thread
From: Nicolas Graves via Guix-patches via @ 2024-02-03 13:36 UTC (permalink / raw)
  To: 57387; +Cc: maximedevos, ngraves, othacehe

* gnu/packages/admin.scm (restartd): New variable.
---
 gnu/packages/admin.scm                        |  51 +++
 .../patches/restartd-update-robust.patch      | 295 ++++++++++++++++++
 2 files changed, 346 insertions(+)
 create mode 100644 gnu/packages/patches/restartd-update-robust.patch

diff --git a/gnu/packages/admin.scm b/gnu/packages/admin.scm
index fcf05992d8..6940638c75 100644
--- a/gnu/packages/admin.scm
+++ b/gnu/packages/admin.scm
@@ -62,6 +62,7 @@
 ;;; Copyright © 2023 Bruno Victal <mirai@makinata.eu>
 ;;; Copyright © 2023 Tobias Kortkamp <tobias.kortkamp@gmail.com>
 ;;; Copyright © 2023 Jaeme Sifat <jaeme@runbox.com>
+;;; Copyright © 2023 Nicolas Graves <ngraves@ngraves.fr>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -5814,6 +5815,56 @@ (define-public fail2ban
 mechanisms if you really want to protect services.")
     (license license:gpl2+)))
 
+(define-public restartd
+  (let* ((commit "7044125ac55056f2663536f7137170edf92ebd75")
+         ;; Version is 0.2.4 in the version file in the repo
+         ;; but not in github tags.
+         ;; It is released as 0.2.3-1.1 for other distributions.
+         ;; Probably because of the lack of activity upstream.
+         (revision "1"))
+    (package
+      (name "restartd")
+      (version (git-version "0.2.3" revision commit))
+      (source
+       (origin
+         (method git-fetch)
+         (uri (git-reference
+               (url "https://github.com/ajraymond/restartd")
+               (commit commit)))
+         (file-name (git-file-name name version))
+         (sha256
+          (base32
+           "1m1np00b4zvvwx63gzysbi38i5vj1jsjvh2s0p9czl6dzyz582z0"))
+         (patches (search-patches "restartd-update-robust.patch"))))
+      (build-system gnu-build-system)
+      (arguments
+       (list
+        #:tests? #f ; no tests
+        #:make-flags
+        #~(list (string-append "CC=" #$(cc-for-target)))
+        #:phases
+        #~(modify-phases %standard-phases
+            (delete  'configure)
+            (replace 'install
+              (lambda _
+                (install-file "restartd.conf" (string-append #$output "/etc"))
+                (install-file "restartd" (string-append #$output "/sbin"))
+                (install-file "restartd.8"
+                              (string-append #$output "/share/man/man8"))
+                (mkdir-p (string-append #$output "/share/man/fr/man8"))
+                (copy-file
+                 "restartd.fr.8"
+                 (string-append #$output "/share/man/fr/man8/restartd.8")))))))
+      (home-page "https://launchpad.net/debian/+source/restartd")
+      (synopsis "Daemon for restarting processes")
+      (description "This package provides a daemon for checking running and not
+running processes.  It reads the /proc directory every n seconds and does a
+POSIX regexp on the process names.  The daemon runs an user-provided script
+when it detects a program in the running processes, or an alternate script if
+it doesn't detect the program.  The daemon can only be called by the root
+user, but can use @code{sudo -u user} in the process called if needed.")
+      (license license:gpl2+))))
+
 (define-public rex
   (package
     (name "rex")
diff --git a/gnu/packages/patches/restartd-update-robust.patch b/gnu/packages/patches/restartd-update-robust.patch
new file mode 100644
index 0000000000..d279ebd8ff
--- /dev/null
+++ b/gnu/packages/patches/restartd-update-robust.patch
@@ -0,0 +1,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
+
-- 
2.41.0





^ permalink raw reply related	[flat|nested] 23+ messages in thread

* [bug#57387] [PATCH v3] gnu: Add restartd.
  2022-08-24 15:59 [bug#57387] [PATCH] gnu: Add restartd Nicolas Graves via Guix-patches via
                   ` (5 preceding siblings ...)
  2024-02-03 13:36 ` [bug#57387] [PATCH v2] " Nicolas Graves via Guix-patches via
@ 2024-02-03 14:06 ` Nicolas Graves via Guix-patches via
  2024-02-18 14:54   ` bug#57387: " Ludovic Courtès
  6 siblings, 1 reply; 23+ messages in thread
From: Nicolas Graves via Guix-patches via @ 2024-02-03 14:06 UTC (permalink / raw)
  To: 57387; +Cc: maximedevos, ngraves, othacehe

* gnu/packages/admin.scm (restartd): New variable.
* gnu/packages/patches/restartd-update-robust.patch: Add file.
* gnu/local.mk: Register gnu/packages/patches/restartd-update-robust.patch.

Change-Id: I38b9b70e97b64f006a86d7618a75f1ec3ed8b034
---
 gnu/local.mk                                  |   1 +
 gnu/packages/admin.scm                        |  51 +++
 .../patches/restartd-update-robust.patch      | 295 ++++++++++++++++++
 3 files changed, 347 insertions(+)
 create mode 100644 gnu/packages/patches/restartd-update-robust.patch

diff --git a/gnu/local.mk b/gnu/local.mk
index fbdb285695..6e4c3b277b 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -1981,6 +1981,7 @@ dist_patch_DATA =						\
   %D%/packages/patches/racket-rktio-bin-sh.patch		\
   %D%/packages/patches/racket-zuo-bin-sh.patch			\
   %D%/packages/patches/remake-impure-dirs.patch			\
+  %D%/packages/patches/restartd-update-robust.patch             \
   %D%/packages/patches/restic-0.9.6-fix-tests-for-go1.15.patch	\
   %D%/packages/patches/rng-tools-revert-build-randstat.patch	\
   %D%/packages/patches/rocclr-5.6.0-enable-gfx800.patch		\
diff --git a/gnu/packages/admin.scm b/gnu/packages/admin.scm
index fcf05992d8..6940638c75 100644
--- a/gnu/packages/admin.scm
+++ b/gnu/packages/admin.scm
@@ -62,6 +62,7 @@
 ;;; Copyright © 2023 Bruno Victal <mirai@makinata.eu>
 ;;; Copyright © 2023 Tobias Kortkamp <tobias.kortkamp@gmail.com>
 ;;; Copyright © 2023 Jaeme Sifat <jaeme@runbox.com>
+;;; Copyright © 2023 Nicolas Graves <ngraves@ngraves.fr>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -5814,6 +5815,56 @@ (define-public fail2ban
 mechanisms if you really want to protect services.")
     (license license:gpl2+)))
 
+(define-public restartd
+  (let* ((commit "7044125ac55056f2663536f7137170edf92ebd75")
+         ;; Version is 0.2.4 in the version file in the repo
+         ;; but not in github tags.
+         ;; It is released as 0.2.3-1.1 for other distributions.
+         ;; Probably because of the lack of activity upstream.
+         (revision "1"))
+    (package
+      (name "restartd")
+      (version (git-version "0.2.3" revision commit))
+      (source
+       (origin
+         (method git-fetch)
+         (uri (git-reference
+               (url "https://github.com/ajraymond/restartd")
+               (commit commit)))
+         (file-name (git-file-name name version))
+         (sha256
+          (base32
+           "1m1np00b4zvvwx63gzysbi38i5vj1jsjvh2s0p9czl6dzyz582z0"))
+         (patches (search-patches "restartd-update-robust.patch"))))
+      (build-system gnu-build-system)
+      (arguments
+       (list
+        #:tests? #f ; no tests
+        #:make-flags
+        #~(list (string-append "CC=" #$(cc-for-target)))
+        #:phases
+        #~(modify-phases %standard-phases
+            (delete  'configure)
+            (replace 'install
+              (lambda _
+                (install-file "restartd.conf" (string-append #$output "/etc"))
+                (install-file "restartd" (string-append #$output "/sbin"))
+                (install-file "restartd.8"
+                              (string-append #$output "/share/man/man8"))
+                (mkdir-p (string-append #$output "/share/man/fr/man8"))
+                (copy-file
+                 "restartd.fr.8"
+                 (string-append #$output "/share/man/fr/man8/restartd.8")))))))
+      (home-page "https://launchpad.net/debian/+source/restartd")
+      (synopsis "Daemon for restarting processes")
+      (description "This package provides a daemon for checking running and not
+running processes.  It reads the /proc directory every n seconds and does a
+POSIX regexp on the process names.  The daemon runs an user-provided script
+when it detects a program in the running processes, or an alternate script if
+it doesn't detect the program.  The daemon can only be called by the root
+user, but can use @code{sudo -u user} in the process called if needed.")
+      (license license:gpl2+))))
+
 (define-public rex
   (package
     (name "rex")
diff --git a/gnu/packages/patches/restartd-update-robust.patch b/gnu/packages/patches/restartd-update-robust.patch
new file mode 100644
index 0000000000..d279ebd8ff
--- /dev/null
+++ b/gnu/packages/patches/restartd-update-robust.patch
@@ -0,0 +1,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
+
-- 
2.41.0





^ permalink raw reply related	[flat|nested] 23+ messages in thread

* [bug#57387] [PATCH] gnu: Add restartd.
  2022-09-14  7:48   ` Mathieu Othacehe
@ 2024-02-03 14:07     ` Nicolas Graves via Guix-patches via
  0 siblings, 0 replies; 23+ messages in thread
From: Nicolas Graves via Guix-patches via @ 2024-02-03 14:07 UTC (permalink / raw)
  To: Mathieu Othacehe; +Cc: 57387


Hi Mathieu,

Done in a rebased v3, you can ignore the v2. Sorry for the time it took.

Nicolas

On 2022-09-14 09:48, Mathieu Othacehe wrote:

> Hello Nicolas,
>
>>  ...td-add-error-handling-for-robustness.patch | 242 ++++++++++++++++++
>>  .../patches/restartd-fix-compilation.patch    |  53 ++++
>
> Both patches also need to be added to the dist_patch_DATA variable of
> gnu/local.mk.
>
> Thanks,
>
> Mathieu
>
>
>

-- 
Best regards,
Nicolas Graves




^ permalink raw reply	[flat|nested] 23+ messages in thread

* bug#57387: [PATCH v3] gnu: Add restartd.
  2024-02-03 14:06 ` [bug#57387] [PATCH v3] " Nicolas Graves via Guix-patches via
@ 2024-02-18 14:54   ` Ludovic Courtès
  0 siblings, 0 replies; 23+ messages in thread
From: Ludovic Courtès @ 2024-02-18 14:54 UTC (permalink / raw)
  To: Nicolas Graves; +Cc: othacehe, 57387-done

[-- Attachment #1: Type: text/plain, Size: 363 bytes --]

Nicolas Graves <ngraves@ngraves.fr> skribis:

> * gnu/packages/admin.scm (restartd): New variable.
> * gnu/packages/patches/restartd-update-robust.patch: Add file.
> * gnu/local.mk: Register gnu/packages/patches/restartd-update-robust.patch.
>
> Change-Id: I38b9b70e97b64f006a86d7618a75f1ec3ed8b034

Applied with the changes below, thanks!

Ludo’.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Type: text/x-patch, Size: 1401 bytes --]

diff --git a/gnu/packages/admin.scm b/gnu/packages/admin.scm
index 9f51427297..47c82e9678 100644
--- a/gnu/packages/admin.scm
+++ b/gnu/packages/admin.scm
@@ -5886,12 +5886,13 @@ (define-public restartd
                  (string-append #$output "/share/man/fr/man8/restartd.8")))))))
       (home-page "https://launchpad.net/debian/+source/restartd")
       (synopsis "Daemon for restarting processes")
-      (description "This package provides a daemon for checking running and not
-running processes.  It reads the /proc directory every n seconds and does a
-POSIX regexp on the process names.  The daemon runs an user-provided script
-when it detects a program in the running processes, or an alternate script if
-it doesn't detect the program.  The daemon can only be called by the root
-user, but can use @code{sudo -u user} in the process called if needed.")
+      (description "This package provides a daemon for checking running and
+not running processes.  It reads the @file{/proc} directory every @var{n}
+seconds and does a POSIX regexp on the process names.  The daemon runs a
+user-provided script when it detects a program in the running processes, or an
+alternate script if it doesn't detect the program.  The daemon can only be
+called by the root user, but can use @command{sudo -u user} in the process
+called if needed.")
       (license license:gpl2+))))
 
 (define-public rex

^ permalink raw reply related	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2024-02-18 14:55 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-08-24 15:59 [bug#57387] [PATCH] gnu: Add restartd Nicolas Graves via Guix-patches via
2022-08-24 18:09 ` Maxime Devos
2022-08-24 18:14   ` ( via Guix-patches via
2022-08-25  9:14 ` Nicolas Graves via Guix-patches via
2022-08-25  9:16 ` Nicolas Graves via Guix-patches via
2022-08-25 10:34   ` Maxime Devos
2022-08-25 11:33   ` Maxime Devos
2022-08-26  7:32 ` Nicolas Graves via Guix-patches via
2022-08-26 14:03   ` Maxime Devos
2022-08-26 16:57     ` Nicolas Graves via Guix-patches via
2022-08-26 19:07       ` Maxime Devos
2022-08-26 22:16 ` Nicolas Graves via Guix-patches via
2022-08-28 17:38   ` Maxime Devos
2022-09-11 14:53   ` Mathieu Othacehe
2022-09-11 18:13     ` Nicolas Graves via Guix-patches via
2022-09-12  7:36       ` Mathieu Othacehe
2022-09-13  8:45         ` Nicolas Graves via Guix-patches via
2022-09-14  7:48   ` Mathieu Othacehe
2024-02-03 14:07     ` Nicolas Graves via Guix-patches via
2024-02-03 13:36 ` [bug#57387] [PATCH v2] " Nicolas Graves via Guix-patches via
2024-02-03 14:06 ` [bug#57387] [PATCH v3] " Nicolas Graves via Guix-patches via
2024-02-18 14:54   ` bug#57387: " Ludovic Courtès
     [not found] <87edvu1hy9.fsf@ngraves.fr>
2022-09-29 13:14 ` [bug#57387] [PATCH] " Nicolas Graves via Guix-patches via

Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.