addition of silc.css
[runtime.git] / apps / irssi / src / core / pidwait.c
1 /*
2  pidwait.c :
3
4     Copyright (C) 1999-2000 Timo Sirainen
5
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 2 of the License, or
9     (at your option) any later version.
10
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.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "module.h"
22 #include "signals.h"
23 #include "modules.h"
24
25 #include <sys/wait.h>
26
27 static GSList *pids;
28
29 static unsigned int childcheck_tag;
30 static int signal_pidwait;
31
32 /* add a pid to wait list */
33 void pidwait_add(int pid)
34 {
35         pids = g_slist_append(pids, GINT_TO_POINTER(pid));
36 }
37
38 /* remove pid from wait list */
39 void pidwait_remove(int pid)
40 {
41         pids = g_slist_remove(pids, GINT_TO_POINTER(pid));
42 }
43
44 static int child_check(void)
45 {
46         GSList *tmp, *next;
47         int status;
48
49         /* wait for each pid.. */
50         for (tmp = pids; tmp != NULL; tmp = next) {
51                 int pid = GPOINTER_TO_INT(tmp->data);
52
53                 next = tmp->next;
54                 if (waitpid(pid, &status, WNOHANG) > 0) {
55                         /* process terminated, remove from list */
56                         signal_emit_id(signal_pidwait, 2, tmp->data,
57                                        GINT_TO_POINTER(status));
58                         pids = g_slist_remove(pids, tmp->data);
59                 }
60         }
61         return 1;
62 }
63
64 void pidwait_init(void)
65 {
66         pids = NULL;
67         childcheck_tag = g_timeout_add(1000, (GSourceFunc) child_check, NULL);
68
69         signal_pidwait = signal_get_uniq_id("pidwait");
70 }
71
72 void pidwait_deinit(void)
73 {
74         g_slist_free(pids);
75
76         g_source_remove(childcheck_tag);
77 }