tests: Fix memory leaks in test programs.
[sliver-openvswitch.git] / tests / test-timeval.c
1 /*
2  * Copyright (c) 2009, 2010 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include "timeval.h"
20
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <sys/time.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27
28 #include "command-line.h"
29 #include "daemon.h"
30 #include "util.h"
31
32 #undef NDEBUG
33 #include <assert.h>
34
35 static long long int
36 gettimeofday_in_msec(void)
37 {
38     struct timeval tv;
39
40     assert(!gettimeofday(&tv, NULL));
41     return timeval_to_msec(&tv);
42 }
43
44 static void
45 do_test(void)
46 {
47     /* Wait until we are awakened by a signal (typically EINTR due to the
48      * setitimer()).  Then ensure that, if time has really advanced by
49      * TIME_UPDATE_INTERVAL, then time_msec() reports that it advanced.
50      */
51     long long int start_time_msec;
52     long long int start_gtod;
53
54     start_time_msec = time_msec();
55     start_gtod = gettimeofday_in_msec();
56     for (;;) {
57         /* Wait up to 1 second.  Using select() to do the timeout avoids
58          * interfering with the interval timer. */
59         struct timeval timeout;
60         int retval;
61
62         timeout.tv_sec = 1;
63         timeout.tv_usec = 0;
64         retval = select(0, NULL, NULL, NULL, &timeout);
65         if (retval != -1) {
66             ovs_fatal(0, "select returned %d", retval);
67         } else if (errno != EINTR) {
68             ovs_fatal(errno, "select reported unexpected error");
69         }
70
71         if (gettimeofday_in_msec() - start_gtod >= TIME_UPDATE_INTERVAL) {
72             assert(time_msec() - start_time_msec >= TIME_UPDATE_INTERVAL);
73             break;
74         }
75     }
76 }
77
78 static void
79 usage(void)
80 {
81     ovs_fatal(0, "usage: %s TEST, where TEST is \"plain\" or \"daemon\"",
82               program_name);
83 }
84
85 int
86 main(int argc, char *argv[])
87 {
88     proctitle_init(argc, argv);
89     set_program_name(argv[0]);
90     time_init();
91
92     if (argc != 2) {
93         usage();
94     } else if (!strcmp(argv[1], "plain")) {
95         do_test();
96     } else if (!strcmp(argv[1], "daemon")) {
97         /* Test that time still advances even in a daemon.  This is an
98          * interesting test because fork() cancels the interval timer. */
99         char cwd[1024], *pidfile;
100         FILE *success;
101
102         assert(getcwd(cwd, sizeof cwd) == cwd);
103
104         unlink("test-timeval.success");
105
106         /* Daemonize, with a pidfile in the current directory. */
107         set_detach();
108         pidfile = xasprintf("%s/test-timeval.pid", cwd);
109         set_pidfile(pidfile);
110         free(pidfile);
111         set_no_chdir();
112         daemonize();
113
114         /* Run the test. */
115         do_test();
116
117         /* Report success by writing out a file, since the ultimate invoker of
118          * test-timeval can't wait on the daemonized process. */
119         success = fopen("test-timeval.success", "w");
120         if (!success) {
121             ovs_fatal(errno, "test-timeval.success: create failed");
122         }
123         fprintf(success, "success\n");
124         fclose(success);
125     } else {
126         usage();
127     }
128
129     return 0;
130 }