1 /*
2  * Copyright (C) 2013 Nikos Mavrogiannopoulos
3  *
4  * Author: Nikos Mavrogiannopoulos
5  *
6  * This file is part of ocserv.
7  *
8  * ocserv is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>
20  */
21 #ifndef SCRIPT_LIST_H
22 # define SCRIPT_LIST_H
23 
24 #include <main.h>
25 #include <sys/types.h>
26 #include <signal.h>
27 #include <ev.h>
28 
29 void script_child_watcher_cb(struct ev_loop *loop, ev_child *w, int revents);
30 
31 inline static
add_to_script_list(main_server_st * s,pid_t pid,struct proc_st * proc)32 void add_to_script_list(main_server_st* s, pid_t pid, struct proc_st* proc)
33 {
34 struct script_wait_st *stmp;
35 
36 	stmp = talloc(s, struct script_wait_st);
37 	if (stmp == NULL)
38 		return;
39 
40 	stmp->proc = proc;
41 	stmp->pid = pid;
42 
43 	ev_child_init(&stmp->ev_child, script_child_watcher_cb, pid, 0);
44 	ev_child_start(main_loop, &stmp->ev_child);
45 
46 	list_add(&s->script_list.head, &(stmp->list));
47 }
48 
49 /* Removes the tracked connect script, and kills it. It returns the pid
50  * of the removed script or -1.
51  */
remove_from_script_list(main_server_st * s,struct proc_st * proc)52 inline static pid_t remove_from_script_list(main_server_st* s, struct proc_st* proc)
53 {
54 	struct script_wait_st *stmp = NULL, *spos;
55 	pid_t ret = -1;
56 
57 	list_for_each_safe(&s->script_list.head, stmp, spos, list) {
58 		if (stmp->proc == proc) {
59 			list_del(&stmp->list);
60 			ev_child_stop(main_loop, &stmp->ev_child);
61 			if (stmp->pid > 0) {
62 				kill(stmp->pid, SIGTERM);
63 				ret = stmp->pid;
64 			}
65 			talloc_free(stmp);
66 			break;
67 		}
68 	}
69 
70 	return ret;
71 }
72 
73 #endif
74