File: pidfile

package info (click to toggle)
flog 1.8%2Borig-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, trixie
  • size: 168 kB
  • sloc: ansic: 1,278; makefile: 48; sh: 12
file content (122 lines) | stat: -rw-r--r-- 3,369 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
--- a/README
+++ b/README
@@ -58,6 +58,8 @@ version 1.6 2005-06-02
 	Now compiles on OpenBSD
 version 1.7 2005-09-01
 	Added -l option to limit file length (patch by dsong@teramail.com)
+2007-06-20
+	Added -p to write pid file  (patch by manon@manon.de)
 version 1.8 2009-02-04
 	Added -z option, but the default is now to buffer, not to truncate log if disk gets full.
 
--- a/flog.c
+++ b/flog.c
@@ -31,9 +31,11 @@ struct FileInfo {
   struct stat stat;	// remember info about file so we can know if it's been rotated
 } out;
 
-struct {
+struct Conf {
   char timestamp;	// { 0 | 1 }
+  char pidfile;	// { 0 | 1 }
   char * time_format;
+  char * pidfile_name;
   int max_len;
   int zap_if_disk_full;
 } conf;
@@ -45,6 +47,7 @@ volatile int gotHUP;
 void growbuf(int size);
 int openfile (struct FileInfo* file);
 int filechanged (struct FileInfo* file);
+void unlinkpidfile (struct Conf* conf);
 void catchHUP (int sig);
 void handleHUP ();
 int dotime (char* prepend_to);
@@ -56,6 +59,7 @@ int main(int argc, char** argv)
   int opt = 0;
   int totalwn = 0;
   int disk_is_full = 0;
+  FILE *pid_fd;
 
   // look for switches
   conf.time_format = NULL;
@@ -70,6 +74,12 @@ int main(int argc, char** argv)
         conf.timestamp = 1;
         conf.time_format = argv[opt];
       }
+    } else if (!strcmp(argv[opt], "-p")) {
+      opt++;
+      if (opt < argc) {
+        conf.pidfile = 1;
+        conf.pidfile_name = argv[opt];
+      }
     }
     else if (!strcmp(argv[opt], "-l")) {
       opt++;
@@ -91,11 +101,23 @@ int main(int argc, char** argv)
 	"	-t	     prepend each line with \"YYYYMMDD;HH:MM:SS: \"\n"
 	"	-T <format>  prepend each line with specified strftime(3) format\n"
 	"	-l <number>  log file length limit (force truncation)\n"
+	"	-p <pidfile> pid file\n"
 	"	-z           zap (truncate) log if disk gets full (default: grow buffer)\n",
 	argv[0]);
     exit(1);
   }
 
+  if (conf.pidfile) {
+    if ((pid_fd = fopen(conf.pidfile_name, "w")) == NULL) {
+      fprintf (stderr, "Could not open pidile: %s", conf.pidfile_name);
+      exit(1);
+    }
+    else {
+      fprintf(pid_fd, "%lu\n", (unsigned long)getpid());
+      fclose(pid_fd);
+    }
+  }
+
   out.name = argv[opt];
   openfile (&out);
 
@@ -146,6 +168,7 @@ int main(int argc, char** argv)
 	    char str[256];
 	    if (ftruncate(out.fd,0)) {
 	      fprintf(stderr, "truncating %s: %d %s\n", out.name, errno, strerror(errno));
+	      unlinkpidfile(&conf);
 	      exit(1);
 	    }
 	    write (out.fd, str, snprintf(str, sizeof(str), "Device full: truncating %s\n\n", out.name));
@@ -157,12 +180,14 @@ int main(int argc, char** argv)
 	}
 	else {
 	  fprintf(stderr, "write %s: %d %s\n", out.name, errno, strerror(errno));
+	  unlinkpidfile(&conf);
 	  exit(1);
 	}
 	if (conf.max_len && (totalwn > conf.max_len)) {
 	  char str[256];
 	  if (ftruncate(out.fd,0)) {
 	    fprintf(stderr, "truncating %s: %d %s\n", out.name, errno, strerror(errno));
+	    unlinkpidfile(&conf);
 	    exit(1);
 	  }
 	  write (out.fd, str, snprintf(str, sizeof(str), "File size limit: truncating %s\n\n", out.name));
@@ -177,8 +202,7 @@ int main(int argc, char** argv)
 
   }
 
-
-
+  unlinkpidfile(&conf);
   exit(0);
 }
 
@@ -293,3 +317,9 @@ int dotime (char* prepend_to)
 
 
 
+void unlinkpidfile (struct Conf* conf)
+{
+  if (conf->pidfile) {
+	  unlink(conf->pidfile_name);
+  }
+}