This way, errors are reported properly.
static void
daemonize(void)
{
static void
daemonize(void)
{
+ pid_t pid = fork();
+ if (pid < 0)
+ die("Fork failed: %m");
+ if (pid)
+ exit(0);
+
close(0);
close(1);
close(2);
close(0);
close(1);
close(2);
int
main(int argc, char **argv)
{
int
main(int argc, char **argv)
{
- int pid;
-
- if (argc != 1 && argc != 2)
- {
- fprintf(stderr, "Usage: nwhod [<server>]\n");
- return 1;
- }
- pid = fork();
- if (pid < 0)
- {
- perror("fork");
- return 1;
- }
- if (pid)
- return 0;
if (argc == 2)
client(argv[1]);
else if (argc == 1)
server();
if (argc == 2)
client(argv[1]);
else if (argc == 1)
server();
+ else
+ {
+ fprintf(stderr, "Usage: nwhod [<server>]\n");
+ return 1;
+ }
+ return 0;