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