Patch from Kent Robotti to being fdisk in sync with v2.12 final.
[oweals/busybox.git] / init / pidfilehack.c
1 /*
2  *  minit version 0.9.1 by Felix von Leitner
3  *  ported to busybox by Glenn McGrath <bug1@optushome.com.au>
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include <unistd.h>
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <sys/fcntl.h>
24
25 #include "busybox.h"
26 /* purpose: argv[1] is the full path to a PID file,
27  *          argv+2 is the daemon to run.
28  * the daemon is expected to fork in the background and write its PID in
29  * the pid file.
30  */
31
32 extern int pidfilehack_main(int argc, char **argv)
33 {
34         int count = 0;
35
36         if (argc < 3) {
37                 bb_show_usage();
38
39         }
40         if (unlink(argv[2]) && (errno != ENOENT)) {
41                 bb_perror_msg("could not remove pid file");
42         }
43         switch (fork()) {
44         case -1:
45                 bb_perror_msg("could not fork");
46                 return 2;
47         case 0: /* child */
48                 execv(argv[3], argv + 3);
49                 bb_perror_msg("execvp failed");
50                 return 3;
51         }
52         do {
53                 int fd = open(argv[2], O_RDONLY);
54
55                 if (fd >= 0) {
56                         static char buf[100] = "-P";
57                         int len = read(fd, buf + 2, 100);
58
59                         close(fd);
60                         if (len > 0) {
61                                 char *_argv[] = { "msvc", 0, 0, 0 };
62                                 if (buf[len + 1] == '\n') {
63                                         buf[len + 1] = 0;
64                                 } else {
65                                         buf[len + 2] = 0;
66                                 }
67                                 _argv[1] = buf;
68                                 _argv[2] = argv[1];
69                                 execvp(_argv[0], _argv);
70                                 bb_perror_msg("execvp failed");
71                                 return 0;
72                         }
73                 }
74                 sleep(1);
75         } while (++count < 30);
76
77         return(0);
78 }