1 --- busybox-1.00/networking/udhcp/pidfile.h-dist 2004-04-15 03:51:26.000000000 +1000
2 +++ busybox-1.00/networking/udhcp/pidfile.h 2004-10-27 15:46:38.000000000 +1000
6 int pidfile_acquire(const char *pidfile);
7 -void pidfile_write_release(int pid_fd);
8 +int pidfile_reassign(const char *pidfile, int newpid);
10 --- busybox-1.00/networking/udhcp/pidfile.c-dist 2004-04-15 03:51:25.000000000 +1000
11 +++ busybox-1.00/networking/udhcp/pidfile.c 2004-10-27 19:43:40.000000000 +1000
24 -int pidfile_acquire(const char *pidfile)
25 +static int pidfile_open(const char *pidfile)
28 - if (!pidfile) return -1;
31 - pid_fd = open(pidfile, O_CREAT | O_WRONLY, 0644);
33 - LOG(LOG_ERR, "Unable to open pidfile %s: %m\n", pidfile);
35 - lockf(pid_fd, F_LOCK, 0);
37 - atexit(pidfile_delete);
38 - saved_pidfile = (char *) pidfile;
39 + if ((fd = open(pidfile, O_CREAT | O_RDWR, 0644)) < 0) {
40 + LOG(LOG_ERR, "pidfile_open: open %s failed: %m\n", pidfile);
44 + /* NOTE: lockf is not inherited by child after fork */
45 + if (lockf(fd, F_LOCK, 0) < 0) {
46 + LOG(LOG_ERR, "pidfile_open: lock %s failed: %m\n", pidfile);
55 +static int pidfile_check(int fd, const char *pidfile)
60 + if (lseek(fd, 0L, SEEK_SET) < 0) {
61 + LOG(LOG_ERR, "pidfile_check: lseek %s failed: %m\n", pidfile);
65 + if ((len = read(fd, buf, sizeof buf - 1)) < 0) {
66 + LOG(LOG_ERR, "pidfile_check: read %s failed: %m\n", pidfile);
75 + if ((pid = atoi(buf)) <= 1) {
77 + "pidfile_check: ignoring bogus pid (%s) in %s\n",
82 + if (kill((pid_t)pid, 0) == 0) {
83 + LOG(LOG_ERR, "pidfile_check: process %d exists (%s)\n",
88 + if (errno != ESRCH) {
89 + LOG(LOG_ERR, "pidfile_check: kill %d failed (%s): %m\n",
98 +static int pidfile_store(int fd, const char *pidfile, int pid)
103 + if (lseek(fd, 0L, SEEK_SET) < 0) {
104 + LOG(LOG_ERR, "pidfile_store: lseek %s failed: %m\n", pidfile);
108 + len = snprintf(buf, sizeof buf - 1, "%d\n", pid);
111 + if (write(fd, buf, len) < 0) {
112 + LOG(LOG_ERR, "pidfile_store: write %s failed: %m\n",
117 + if (ftruncate(fd, len) < 0) {
118 + LOG(LOG_ERR, "pidfile_store: ftruncate %d failed (%s): %m\n",
128 -void pidfile_write_release(int pid_fd)
129 +static void pidfile_close(int fd)
132 + (void)lseek(fd, 0L, SEEK_SET);
133 + (void)lockf(fd, F_ULOCK, 0);
137 - if (pid_fd < 0) return;
139 - if ((out = fdopen(pid_fd, "w")) != NULL) {
140 - fprintf(out, "%d\n", getpid());
142 +int pidfile_acquire(const char *pidfile)
145 + if (!pidfile) return (-1);
147 + if ((fd = pidfile_open(pidfile)) < 0)
150 + if ((result = pidfile_check(fd, pidfile)) == 0)
151 + result = pidfile_store(fd, pidfile, getpid());
156 + saved_pidfile = (char *) pidfile;
157 + atexit(pidfile_delete);
159 - lockf(pid_fd, F_UNLCK, 0);
167 + * reassign the pid in a pidfile - used just after a fork so a parent
168 + * can store the pid of its child into the file without any window
169 + * where the pid in the file is a dead process (which might let another
170 + * instance of the program start). Note the parent must use _exit() to
171 + * avoid triggering the unlink scheduled above in pidfile_acquire()
173 +int pidfile_reassign(const char *pidfile, int pid)
176 + if (!pidfile) return (-1);
178 + if ((fd = pidfile_open(pidfile)) < 0)
181 + result = pidfile_store(fd, pidfile, pid);
187 --- busybox-1.00/networking/udhcp/common.c-dist 2004-05-19 19:18:04.000000000 +1000
188 +++ busybox-1.00/networking/udhcp/common.c 2004-10-27 19:58:10.000000000 +1000
191 LOG(LOG_ERR, "Cannot background in uclinux (yet)");
192 #else /* __uClinux__ */
196 - /* hold lock during fork. */
197 - pid_fd = pidfile_acquire(pidfile);
198 - if (daemon(0, 0) == -1) {
199 + /* NOTE: lockf is not inherited by the child after fork */
200 + if ((pid = fork()) < 0) {
207 + if (pidfile_reassign(pidfile, pid) < 0) {
208 + (void)kill(pid, SIGKILL);
216 + if ((fd = open("/dev/null", O_RDWR)) >= 0) {
225 - pidfile_write_release(pid_fd);
226 #endif /* __uClinux__ */
231 void start_log_and_pid(const char *client_server, const char *pidfile)
235 /* Make sure our syslog fd isn't overwritten */
238 /* do some other misc startup stuff while we are here to save bytes */
239 - pid_fd = pidfile_acquire(pidfile);
240 - pidfile_write_release(pid_fd);
241 + if (pidfile_acquire(pidfile) < 0)
244 /* equivelent of doing a fflush after every \n */
249 /* do some other misc startup stuff while we are here to save bytes */
250 - pid_fd = pidfile_acquire(pidfile);
251 - pidfile_write_release(pid_fd);
252 + if (pidfile_acquire(pidfile) < 0)
255 /* equivelent of doing a fflush after every \n */
257 --- busybox-1.00/networking/udhcp/common.h-dist 2004-05-19 18:29:05.000000000 +1000
258 +++ busybox-1.00/networking/udhcp/common.h 2004-10-27 15:10:16.000000000 +1000
261 void background(const char *pidfile);
262 void start_log_and_pid(const char *client_server, const char *pidfile);
263 -void background(const char *pidfile);
264 void udhcp_logging(int level, const char *fmt, ...);
266 #define LOG(level, str, args...) udhcp_logging(level, str, ## args)
267 --- busybox-1.00/networking/udhcp/script.c-dist 2004-05-19 17:45:47.000000000 +1000
268 +++ busybox-1.00/networking/udhcp/script.c 2004-10-27 15:54:04.000000000 +1000
270 execle(client_config.script, client_config.script,
272 LOG(LOG_ERR, "script %s failed: %m", client_config.script);