source: web/old/remctl-2.14/tests/portable/daemon-t.c @ f6f3e91

web
Last change on this file since f6f3e91 was f6f3e91, checked in by Jessica B. Hamrick <jhamrick@…>, 15 years ago

Preserve directory hierarchy (not sure what happened to it)

  • Property mode set to 100644
File size: 3.1 KB
Line 
1/*
2 * daemon test suite.
3 *
4 * Written by Russ Allbery <rra@stanford.edu>
5 * Copyright 2008, 2009 Board of Trustees, Leland Stanford Jr. University
6 *
7 * See LICENSE for licensing terms.
8 */
9
10#include <config.h>
11#include <portable/system.h>
12
13#include <errno.h>
14#include <fcntl.h>
15#ifdef HAVE_SYS_SELECT_H
16# include <sys/select.h>
17#endif
18#include <sys/stat.h>
19#include <sys/time.h>
20#include <sys/wait.h>
21
22#include <tests/tap/basic.h>
23#include <util/util.h>
24
25int test_daemon(int, int);
26
27
28/*
29 * Create the sentinel file, used by the child to indicate when it's done.
30 */
31static void
32create_sentinel(void)
33{
34    int fd;
35
36    fd = open("daemon-sentinel", O_RDWR | O_CREAT, 0666);
37    close(fd);
38}
39
40
41/*
42 * Wait for a sentinel file to be created.  Returns true if we saw it within
43 * the expected length of time, and false otherwise.
44 */
45static int
46wait_sentinel(void)
47{
48    int count = 20;
49    int i;
50    struct timeval tv;
51
52    for (i = 0; i < count; i++) {
53        if (access("daemon-sentinel", F_OK) == 0) {
54            unlink("daemon-sentinel");
55            return 1;
56        }
57        tv.tv_sec = 0;
58        tv.tv_usec = 100000;
59        select(0, NULL, NULL, NULL, &tv);
60    }
61    if (access("daemon-sentinel", F_OK) == 0) {
62        unlink("daemon-sentinel");
63        return 1;
64    }
65    return 0;
66}
67
68
69int
70main(void)
71{
72    int fd, status;
73    pid_t child;
74    char start[BUFSIZ], dir[BUFSIZ];
75
76    plan(9);
77
78    /* Get the current working directory. */
79    if (getcwd(start, sizeof(start)) == NULL)
80        bail("cannot get current working directory");
81
82    /* First, some basic tests. */
83    child = fork();
84    if (child < 0)
85        sysbail("cannot fork");
86    else if (child == 0) {
87        is_int(0, daemon(1, 1), "daemon(1, 1)");
88        fd = open("/dev/tty", O_RDONLY);
89        ok(fd < 0, "...no tty");
90        is_string(start, getcwd(dir, sizeof(dir)), "...in same directory");
91        create_sentinel();
92        exit(42);
93    } else {
94        if (waitpid(child, &status, 0) < 0)
95            bail("cannot wait for child: %s", strerror(errno));
96        testnum += 3;
97        ok(wait_sentinel(), "...child exited");
98        is_int(0, status, "...successfully");
99    }
100
101    /* Test chdir. */
102    child = fork();
103    if (child < 0)
104        sysbail("cannot fork");
105    else if (child == 0) {
106        is_int(0, daemon(0, 1), "daemon(0, 1)");
107        is_string("/", getcwd(dir, sizeof(dir)), "...now in /");
108        if (chdir(start) != 0)
109            sysbail("cannot chdir to %s", start);
110        create_sentinel();
111        exit(0);
112    } else {
113        if (waitpid(child, &status, 0) < 0)
114            sysbail("cannot wait for child");
115        testnum += 2;
116        ok(wait_sentinel(), "...child exited");
117    }
118
119    /* Test close. */
120    child = fork();
121    if (child < 0)
122        sysbail("cannot fork");
123    else if (child == 0) {
124        daemon(0, 0);
125        if (chdir(start) != 0)
126            sysbail("cannot chdir to %s", start);
127        ok(0, "output from child that should be hidden");
128        create_sentinel();
129        exit(0);
130    } else {
131        if (waitpid(child, &status, 0) < 0)
132            sysbail("cannot wait for child");
133        ok(wait_sentinel(), "daemon(0, 0)");
134    }
135
136    return 0;
137}
Note: See TracBrowser for help on using the repository browser.