4 Copyright (C) Amitay Isaacs 2016
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "system/wait.h"
25 #include "common/pidfile.c"
28 /* create pid file, check pid file exists, check pid and remove pid file */
29 static void test1(const char *pidfile)
31 struct pidfile_context *pid_ctx;
37 ret = pidfile_context_create(NULL, pidfile, &pid_ctx);
39 assert(pid_ctx != NULL);
41 ret = stat(pidfile, &st);
43 assert(S_ISREG(st.st_mode));
45 fp = fopen(pidfile, "r");
47 ret = fscanf(fp, "%d", &pid);
49 assert(pid == getpid());
54 ret = stat(pidfile, &st);
58 /* create pid file in two processes */
59 static void test2(const char *pidfile)
61 struct pidfile_context *pid_ctx;
80 ret = pidfile_context_create(NULL, pidfile, &pid_ctx);
82 assert(pid_ctx != NULL);
84 nwritten = write(fd[1], &ret, sizeof(ret));
85 assert(nwritten == sizeof(ret));
91 nwritten = write(fd[1], &ret, sizeof(ret));
92 assert(nwritten == sizeof(ret));
99 nread = read(fd[0], &ret, sizeof(ret));
100 assert(nread == sizeof(ret));
103 fp = fopen(pidfile, "r");
105 ret = fscanf(fp, "%d", &pid2);
110 ret = pidfile_context_create(NULL, pidfile, &pid_ctx);
113 nread = read(fd[0], &ret, sizeof(ret));
114 assert(nread == sizeof(ret));
117 ret = pidfile_context_create(NULL, pidfile, &pid_ctx);
119 assert(pid_ctx != NULL);
121 TALLOC_FREE(pid_ctx);
123 ret = stat(pidfile, &st);
127 /* create pid file, fork, try to remove pid file in separate process */
128 static void test3(const char *pidfile)
130 struct pidfile_context *pid_ctx;
137 ret = pidfile_context_create(NULL, pidfile, &pid_ctx);
139 assert(pid_ctx != NULL);
152 TALLOC_FREE(pid_ctx);
154 nwritten = write(fd[1], &ret, sizeof(ret));
155 assert(nwritten == sizeof(ret));
162 nread = read(fd[0], &ret, sizeof(ret));
163 assert(nread == sizeof(ret));
165 ret = stat(pidfile, &st);
168 TALLOC_FREE(pid_ctx);
170 ret = stat(pidfile, &st);
174 /* create pid file, kill process, overwrite pid file in different process */
175 static void test4(const char *pidfile)
177 struct pidfile_context *pid_ctx;
195 ret = pidfile_context_create(NULL, pidfile, &pid_ctx);
197 nwritten = write(fd[1], &ret, sizeof(ret));
198 assert(nwritten == sizeof(ret));
206 nread = read(fd[0], &ret, sizeof(ret));
207 assert(nread == sizeof(ret));
210 ret = stat(pidfile, &st);
213 ret = kill(pid, SIGKILL);
216 pid2 = waitpid(pid, &ret, 0);
219 ret = pidfile_context_create(NULL, pidfile, &pid_ctx);
221 assert(pid_ctx != NULL);
223 ret = stat(pidfile, &st);
226 TALLOC_FREE(pid_ctx);
229 int main(int argc, const char **argv)
232 fprintf(stderr, "Usage: %s <pidfile>\n", argv[0]);