~ubuntu-branches/ubuntu/raring/maradns/raring

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
Subject: --pid <file> argument to create pid file for child process
 This patch is being deployed by upstream and we want to keep
 our changes compatible with upstream.
Author: Yarin
Origin: http://maradns.org
Last-Updated: 2011-06-13
Reviewed-by: Nicholas Bamber <nicholas@periapt.co.uk>
--- a/tools/duende.c
+++ b/tools/duende.c
@@ -146,10 +146,19 @@
     int exit_status;
     pid_t pid, log_pid;
     int stream1[2]; /* Used for piping */
+    int exec_argv_offset = 1; /* Also used to determine PID writing */
     if(argv[0] == NULL || argv[1] == NULL) {
-        printf("Usage: duende [program] [arguments]\n");
+        printf("Usage: duende (--pid=/path/to/file) [program] [arguments]\n");
         exit(1);
         }
+    if(!strncasecmp(argv[1],"--pid=",6)) {
+        if(argv[2] == NULL) {
+            printf(
+		"Usage: duende (--pid=/path/to/file) [program] [arguments]\n");
+            exit(1);
+            }
+        exec_argv_offset = 2;
+        }
 
     /* Let children know that duende is running */
     if(setenv("DUENDE_IS_RUNNING","1",0) != 0) {
@@ -164,6 +173,18 @@
     /* The child becomes a full-fledged daemon */
     setpgid(0,0); /* No longer visible in 'ps' without the 'auxw' argument */
 
+    /* Write our PID to a file if the user so desires us to */
+    if(exec_argv_offset == 2) {
+        FILE *fp_pid = fopen(argv[1] + 6,"w");
+        if(!fp_pid) {
+            syslog(LOG_ALERT,"Fatal writing, to PID file, error\n");
+            exit(1);
+            }
+        unsigned int local_pid = getpid();
+        fprintf(fp_pid,"%u",local_pid);
+        fclose(fp_pid);
+        }
+
     /* Sysadmins expect HUP to reload, so we set that up */
     signal(SIGHUP,handle_hup);
     signal(SIGTERM,handle_term);
@@ -193,10 +214,10 @@
                 syslog(LOG_ALERT,"Fatal dup2 error 2");
                 exit(5);
                 }
-            argv[0] = argv[1];
-            execvp(argv[1],argv + 1);
+            argv[0] = argv[exec_argv_offset];
+            execvp(argv[exec_argv_offset],argv + exec_argv_offset);
             /* OK, not found */
-            printf("duende: %s: Command can't run, terminating\n",argv[1]);
+            printf("duende: %s: Command can't run, terminating\n",argv[exec_argv_offset]);
             syslog(LOG_ALERT,"Command can't run, terminating\n");
             exit(1);
             }
@@ -206,7 +227,7 @@
         log_pid = fork();
         if(log_pid == 0) { /* Child to syslog all of MaraDNS' output */
             argv[0] = "duende-log-helper";
-            log_helper(argv[1],stream1[0]);
+            log_helper(argv[exec_argv_offset],stream1[0]);
             syslog(LOG_ALERT,"log_helper finished, terminating\n");
             exit(1);
             }