1/*
2 * (C) 2009 by Pablo Neira Ayuso <pablo@netfilter.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19#include <signal.h>
20#include "conntrackd.h"
21#include "process.h"
22
23static LIST_HEAD(process_list);
24
25int fork_process_new(int type, int flags, void (*cb)(void *data), void *data)
26{
27	struct child_process *c, *this;
28	int pid;
29
30	/* block SIGCHLD to avoid the access of the list concurrently */
31	sigprocmask(SIG_BLOCK, &STATE(block), NULL);
32
33	/* We only want one process of this type at the same time. This is
34	 * useful if you want to prevent two child processes from accessing
35	 * a shared descriptor at the same time. */
36	if (flags & CTD_PROC_F_EXCL) {
37		list_for_each_entry(this, &process_list, head) {
38			if (this->type == type) {
39				sigprocmask(SIG_UNBLOCK, &STATE(block), NULL);
40				return -1;
41			}
42		}
43	}
44	c = calloc(sizeof(struct child_process), 1);
45	if (c == NULL) {
46		sigprocmask(SIG_UNBLOCK, &STATE(block), NULL);
47		return -1;
48	}
49
50	c->type = type;
51	c->cb = cb;
52	c->data = data;
53	c->pid = pid = fork();
54
55	if (c->pid > 0)
56		list_add(&c->head, &process_list);
57
58	sigprocmask(SIG_UNBLOCK, &STATE(block), NULL);
59
60	return pid;
61}
62
63int fork_process_delete(int pid)
64{
65	struct child_process *this, *tmp;
66
67	list_for_each_entry_safe(this, tmp, &process_list, head) {
68		if (this->pid == pid) {
69			list_del(&this->head);
70			if (this->cb) {
71				this->cb(this->data);
72			}
73			free(this);
74			return 1;
75		}
76	}
77	return 0;
78}
79
80static const char *process_type_to_name[CTD_PROC_MAX] = {
81	[CTD_PROC_ANY]		= "any",
82	[CTD_PROC_FLUSH]	= "flush",
83	[CTD_PROC_COMMIT]	= "commit",
84};
85
86void fork_process_dump(int fd)
87{
88	struct child_process *this;
89	char buf[4096];
90	int size = 0;
91
92	sigprocmask(SIG_BLOCK, &STATE(block), NULL);
93	list_for_each_entry(this, &process_list, head) {
94		size += snprintf(buf+size, sizeof(buf),
95				 "PID=%u type=%s\n",
96				 this->pid,
97				 this->type < CTD_PROC_MAX ?
98				 process_type_to_name[this->type] : "unknown");
99	}
100	sigprocmask(SIG_UNBLOCK, &STATE(block), NULL);
101
102	send(fd, buf, size, 0);
103}
104