1 /* Process handling
2  *
3  * Copyright © 2013, Benoît Knecht <benoit.knecht@fsfe.org>
4  *
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *     * Redistributions of source code must retain the above copyright
10  *       notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above copyright
12  *       notice, this list of conditions and the following disclaimer in the
13  *       documentation and/or other materials provided with the distribution.
14  *     * The name of the author may not be used to endorse or promote products
15  *       derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 #ifndef __PROCESS_H__
30 #define __PROCESS_H__
31 
32 #include <unistd.h>
33 #include "clients.h"
34 
35 struct child {
36 	pid_t pid;
37 	time_t age;
38 	struct client_cache_s *client;
39 };
40 
41 extern struct child *children;
42 extern int number_of_children;
43 
44 /**
45  * Fork a new child (just like fork()) but keep track of how many childs are
46  * already running, and refuse fo fork if there are too many.
47  * @return -1 if it couldn't fork, 0 in the child process, the pid of the
48  *         child process in the parent process.
49  */
50 pid_t process_fork(struct client_cache_s *client);
51 
52 /**
53  * Handler to be called upon receiving SIGCHLD. This signal is received by the
54  * parent process when a child terminates, and this handler updates the number
55  * of running childs accordingly.
56  * @param signal The signal number.
57  */
58 void process_handle_child_termination(int signal);
59 
60 /**
61  * Daemonize the current process by forking itself and redirecting standard
62  * input, standard output and standard error to /dev/null.
63  * @return The pid of the process.
64  */
65 int process_daemonize(void);
66 
67 /**
68  * Check if the process corresponding to the pid found in the pid file is
69  * running.
70  * @param fname The path to the pid file.
71  * @return 0 if no other instance is running, -1 if the file name is invalid,
72  *         -2 if another instance is running.
73  */
74 int process_check_if_running(const char *fname);
75 
76 /**
77  * Kill all child processes
78  */
79 void process_reap_children(void);
80 
81 #endif // __PROCESS_H__
82