From 9ad1decbb80792220dae648e411e2213252b46da Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 11 Jul 2016 23:49:07 +0200 Subject: Conditionally store realpath(argv0) so we can reexecute even if argv0 is relative MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/gammad.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/gammad.c b/src/gammad.c index 708e5df..6dcb31f 100644 --- a/src/gammad.c +++ b/src/gammad.c @@ -42,6 +42,12 @@ */ char* argv0; +/** + * The real pathname of the process's binary, + * `NULL` if `argv0` is satisfactory + */ +char* argv0_real = NULL; + /** * Array of all outputs */ @@ -674,6 +680,12 @@ static int initialise(int method, const char *sitename, int preserve, int foregr if (listen(socketfd, SOMAXCONN) < 0) goto fail; + /* Get the real pathname of the process's binary, in case + * it is relative, so we can re-execute without problem. */ + if ((*argv0 != '/') && strchr(argv0, '/')) + if (!(argv0_real = realpath(argv0, NULL))) + goto fail; + /* Change directory to / to avoid blocking umounting */ if (chdir("/") < 0) perror(argv0); @@ -860,6 +872,7 @@ static void destroy(void) if (pidpath) unlink(pidpath); free(pidpath); + free(argv0_real); } @@ -876,6 +889,9 @@ static void usage(void) /** * Must not be started without stdin, stdout, or stderr (may be /dev/null) * + * argv[0] must refer to the real command name or pathname, + * otherwise, re-execute will not work + * * The process closes stdout when the socket has been created * * @signal SIGUSR1 Re-execute to updated process image -- cgit v1.2.3-70-g09d2