summarylogtreecommitdiffstats
path: root/01-pidfile.patch
blob: 073e35ae892e02246b6bee2b65fd5bd88c37ee9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
01-pidfile.patch by Morgon Kanter <morgon@surgo.net> and Martín Ferrari
http://sourceforge.net/support/tracker.php?aid=762822

<martin.ferrari@gmail.com>
 This patch changes main.c so clients write their PID-file as
 well as servers. It also allows a tag to be added to the filename.

diff -pur 1/Makefile.in 2/Makefile.in
--- 1/Makefile.in	2013-07-08 00:36:44.000000000 +0400
+++ 2/Makefile.in	2014-11-02 12:41:42.746487973 +0300
@@ -39,12 +39,12 @@ MAN_DIR  = @mandir@
 ETC_DIR  = @sysconfdir@
 VAR_DIR  = @localstatedir@
 
-PID_FILE = ${VAR_DIR}/run/vtund.pid
+PID_DIR = ${VAR_DIR}/run
 CFG_FILE = ${ETC_DIR}/vtund.conf
 STAT_DIR = ${VAR_DIR}/log/vtund
 LOCK_DIR = ${VAR_DIR}/lock/vtund
 
-DEFS = -DVTUN_CONFIG_FILE=\"$(CFG_FILE)\" -DVTUN_PID_FILE=\"$(PID_FILE)\" \
+DEFS = -DVTUN_CONFIG_FILE=\"$(CFG_FILE)\" -DVTUN_PID_DIR=\"$(PID_DIR)\" \
        -DVTUN_STAT_DIR=\"$(STAT_DIR)\" -DVTUN_LOCK_DIR=\"$(LOCK_DIR)\"
 
 OBJS = main.o cfg_file.tab.o cfg_file.lex.o server.o client.o lib.o \
diff -pur 1/main.c 2/main.c
--- 1/main.c	2014-11-02 12:56:00.021701942 +0300
+++ 2/main.c	2014-11-02 12:53:57.520368795 +0300
@@ -50,7 +50,7 @@
 struct vtun_opts vtun;
 struct vtun_host default_host;
 
-static void write_pid(void);
+static void write_pid(char *, char *);
 static void reread_config(int sig);
 static void usage(void);
 
@@ -240,7 +240,7 @@ int main(int argc, char *argv[], char *e
 
 	if( vtun.svr_type == VTUN_STAND_ALONE ){
 #ifdef HAVE_WORKING_FORK
-	   write_pid();
+	   write_pid("server", NULL);
 #else
 	   vtun_syslog(LOG_ERR,"Standalone server is not supported. Use -i");
 	   exit(1);
@@ -250,6 +250,7 @@ int main(int argc, char *argv[], char *e
 	server(sock);
      } else {	
         init_title(argc,argv,env,"vtund[c]: ");
+       	write_pid(host->host, vtun.svr_name);
         client(host);
      }
 
@@ -260,15 +261,29 @@ int main(int argc, char *argv[], char *e
 
 /* 
  * Very simple PID file creation function. Used by server.
- * Overrides existing file. 
+ * Overrides existing file. Optionally adds session name and host name to the
+ * pidfile name (this naming is very confusing, as the session is referred as
+ * host most of the time)
  */
-static void write_pid(void)
+static void write_pid(char *session, char *host)
 {
+     char fn[1024];
      FILE *f;
 
-     if( !(f=fopen(VTUN_PID_FILE,"w")) ){
-        vtun_syslog(LOG_ERR,"Can't write PID file");
-        return;
+     if(session != NULL && host != NULL) {
+	  snprintf(fn, sizeof(fn), "%s/vtund.%s-%s.pid", VTUN_PID_DIR, session,
+			  host);
+     } else if(session != NULL) {
+	  snprintf(fn, sizeof(fn), "%s/vtund.%s.pid", VTUN_PID_DIR, session);
+     } else {
+          snprintf(fn, sizeof(fn), "%s/vtund.pid", VTUN_PID_DIR);
+     }
+     /* Make sure the PID file is not there before opening it for writing. */
+     unlink(fn);
+
+     if( !(f = fopen(fn, "w")) ) {
+          syslog(LOG_ERR, "Can't write PID file %s: %s", fn, strerror(errno));
+          return;
      }
 
      fprintf(f,"%d",(int)getpid());