ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/util.C
(Generate patch)

Comparing gvpe/src/util.C (file contents):
Revision 1.12 by pcg, Thu Jan 29 18:55:10 2004 UTC vs.
Revision 1.19 by pcg, Wed Mar 23 21:55:39 2005 UTC

1/* 1/*
2 util.C -- process management and other utility functions 2 util.C -- process management and other utility functions
3 Copyright (C) 2003 Marc Lehmann <pcg@goof.com> 3 Copyright (C) 2003-2005 Marc Lehmann <gvpe@schmorp.de>
4 4
5 Some of these are taken from tinc, see the AUTHORS file. 5 Some of these are taken from tinc, see the AUTHORS file.
6 6
7 This file is part of GVPE.
8
7 This program is free software; you can redistribute it and/or modify 9 GVPE is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by 10 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or 11 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version. 12 (at your option) any later version.
11 13
12 This program is distributed in the hope that it will be useful, 14 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details. 17 GNU General Public License for more details.
16 18
17 You should have received a copy of the GNU General Public License 19 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software 20 along with gvpe; if not, write to the Free Software
19 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20*/ 22*/
21 23
22#include "config.h" 24#include "config.h"
23 25
46int 48int
47write_pidfile (void) 49write_pidfile (void)
48{ 50{
49 int pid; 51 int pid;
50 52
51 pid = check_pid (pidfilename); 53 pid = check_pid (conf.pidfilename);
52 54
53 if (pid) 55 if (pid)
54 { 56 {
55 fprintf (stderr, _("A vped is already running with pid %d.\n"), pid); 57 fprintf (stderr, _("A gvpe daemon is already running with pid %d.\n"), pid);
56 return 1; 58 return 1;
57 } 59 }
58 60
59 /* if it's locked, write-protected, or whatever */ 61 /* if it's locked, write-protected, or whatever */
60 if (!write_pid (pidfilename)) 62 if (!write_pid (conf.pidfilename))
61 return 1; 63 return 1;
62 64
63 return 0; 65 return 0;
64} 66}
65 67
66int 68int
67kill_other (int signal) 69kill_other (int signal)
68{ 70{
69 int pid; 71 int pid;
70 72
71 pid = read_pid (pidfilename); 73 pid = read_pid (conf.pidfilename);
72 74
73 if (!pid) 75 if (!pid)
74 { 76 {
75 fprintf (stderr, _("No other vped is running.\n")); 77 fprintf (stderr, _("No other gvpe daemon is running.\n"));
76 return 1; 78 return 1;
77 } 79 }
78 80
79 errno = 0; /* No error, sometimes errno is only changed on error */ 81 errno = 0; /* No error, sometimes errno is only changed on error */
80 82
81 /* ESRCH is returned when no process with that pid is found */ 83 /* ESRCH is returned when no process with that pid is found */
82 if (kill (pid, signal) && errno == ESRCH) 84 if (kill (pid, signal) && errno == ESRCH)
83 { 85 {
84 fprintf (stderr, _("The vped is no longer running. ")); 86 fprintf (stderr, _("The gvpe daemon is no longer running. "));
85 87
86 fprintf (stderr, _("Removing stale lock file.\n")); 88 fprintf (stderr, _("Removing stale lock file.\n"));
87 remove_pid (pidfilename); 89 remove_pid (conf.pidfilename);
88 } 90 }
89 91
90 return 0; 92 return 0;
91} 93}
92 94
112 return -1; 114 return -1;
113 } 115 }
114 116
115 /* Now UPDATE the pid in the pidfile, because we changed it... */ 117 /* Now UPDATE the pid in the pidfile, because we changed it... */
116 118
117 if (!write_pid (pidfilename)) 119 if (!write_pid (conf.pidfilename))
118 return -1; 120 return -1;
119 121
120 log_to (LOGTO_SYSLOG); 122 log_to (LOGTO_SYSLOG);
121 } 123 }
122 else 124 else
123 log_to (LOGTO_SYSLOG | LOGTO_STDERR); 125 log_to (LOGTO_SYSLOG | LOGTO_STDERR);
124 126
125 slog (L_INFO, _("vped %s (%s %s) starting"), VERSION, __DATE__, __TIME__); 127 slog (L_INFO, _("gvpe daemon %s (%s %s) starting"), VERSION, __DATE__, __TIME__);
126 128
127 return 0; 129 return 0;
128} 130}
129 131
130void
131make_names (void)
132{
133 if (!pidfilename)
134 pidfilename = LOCALSTATEDIR "/run/vped.pid";
135
136 if (!confbase)
137 asprintf (&confbase, "%s/vpe", CONFDIR);
138}
139
140void run_script (const run_script_cb &cb, bool wait) 132bool run_script (const run_script_cb &cb, bool wait)
141{ 133{
142 int pid; 134 int pid;
143 135
144 if ((pid = fork ()) == 0) 136 if ((pid = fork ()) == 0)
145 { 137 {
146 char *filename; 138 execl ("/bin/sh", "/bin/sh", "-c", cb (), (char *) 0);
147 asprintf (&filename, "%s/%s", confbase, cb()); 139 exit (EXIT_FAILURE);
148 execl (filename, filename, (char *) 0);
149 exit (126);
150 } 140 }
151 else if (pid > 0) 141 else if (pid > 0)
152 { 142 {
153 if (wait) 143 if (wait)
154 { 144 {
145 int status;
146
155 waitpid (pid, 0, 0); 147 if (waitpid (pid, &status, 0) < 0)
156 /* TODO: check status */ 148 {
149 slog (L_WARN, _("waiting for an external command failed: %s."),
150 strerror (errno));
151 return false;
152 }
153 else if (!WIFEXITED (status) || WEXITSTATUS (status) != EXIT_SUCCESS)
154 {
155 slog (L_WARN, _("external command returned with exit status %d (%04x)."),
156 WEXITSTATUS (status), status);
157 return false;
158 }
157 } 159 }
158 } 160 }
161
162 return true;
159} 163}
160 164
161#if ENABLE_HTTP_PROXY 165#if ENABLE_HTTP_PROXY
162// works like strdup 166// works like strdup
163u8 * 167u8 *

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines