ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/util.C
Revision: 1.3
Committed: Fri Mar 28 04:05:10 2003 UTC (21 years, 2 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.2: +1 -11 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 util.C -- process management and other utility functions
3
4 Some of these are taken from tinc, see the AUTHORS file.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include "config.h"
22
23 #include <cstdio>
24 #include <cstring>
25
26 #include <errno.h>
27 #include <signal.h>
28 #include <sys/types.h>
29 #include <unistd.h>
30 #include <time.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34
35 #include <sys/mman.h>
36
37 #include <openssl/rand.h>
38 #include <openssl/rsa.h>
39 #include <openssl/pem.h>
40 #include <openssl/evp.h>
41
42 #include "gettext.h"
43 #include "pidfile.h"
44 #include "dropin.h"
45
46 #include "global.h"
47 #include "conf.h"
48 #include "slog.h"
49 #include "protocol.h"
50
51 int
52 write_pidfile (void)
53 {
54 int pid;
55
56 pid = check_pid (pidfilename);
57
58 if (pid)
59 {
60 fprintf (stderr, _("A vped is already running with pid %d.\n"), pid);
61 return 1;
62 }
63
64 /* if it's locked, write-protected, or whatever */
65 if (!write_pid (pidfilename))
66 return 1;
67
68 return 0;
69 }
70
71 int
72 kill_other (int signal)
73 {
74 int pid;
75
76 pid = read_pid (pidfilename);
77
78 if (!pid)
79 {
80 fprintf (stderr, _("No other vped is running.\n"));
81 return 1;
82 }
83
84 errno = 0; /* No error, sometimes errno is only changed on error */
85
86 /* ESRCH is returned when no process with that pid is found */
87 if (kill (pid, signal) && errno == ESRCH)
88 {
89 fprintf (stderr, _("The vped is no longer running. "));
90
91 fprintf (stderr, _("Removing stale lock file.\n"));
92 remove_pid (pidfilename);
93 }
94
95 return 0;
96 }
97
98 int
99 detach (int do_detach)
100 {
101 /* First check if we can open a fresh new pidfile */
102
103 if (write_pidfile ())
104 return -1;
105
106 /* If we succeeded in doing that, detach */
107
108 log_to (0);
109
110 if (do_detach)
111 {
112 if (daemon (0, 0) < 0)
113 {
114 log_to (LOGTO_SYSLOG | LOGTO_STDERR);
115
116 slog (L_ERR, _("couldn't detach from terminal: %s"), strerror (errno));
117 return -1;
118 }
119
120 /* Now UPDATE the pid in the pidfile, because we changed it... */
121
122 if (!write_pid (pidfilename))
123 return -1;
124
125 log_to (LOGTO_SYSLOG);
126 }
127 else
128 log_to (LOGTO_SYSLOG | LOGTO_STDERR);
129
130 slog (L_INFO, _("vped %s (%s %s) starting"), VERSION, __DATE__, __TIME__);
131
132 return 0;
133 }
134
135 void
136 make_names (void)
137 {
138 if (!pidfilename)
139 pidfilename = LOCALSTATEDIR "/run/vped.pid";
140
141 if (!confbase)
142 asprintf (&confbase, "%s/vpe", CONFDIR);
143 }
144