Added SILC Thread Queue API
[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 /* return list of pids that are being waited.
45    don't free the return value. */
46 GSList *pidwait_get_pids(void)
47 {
48         return pids;
49 }
50
51 static int child_check(void)
52 {
53         GSList *tmp, *next;
54         int status;
55
56         /* wait for each pid.. */
57         for (tmp = pids; tmp != NULL; tmp = next) {
58                 int pid = GPOINTER_TO_INT(tmp->data);
59
60                 next = tmp->next;
61                 if (waitpid(pid, &status, WNOHANG) > 0) {
62                         /* process terminated, remove from list */
63                         signal_emit_id(signal_pidwait, 2, tmp->data,
64                                        GINT_TO_POINTER(status));
65                         pids = g_slist_remove(pids, tmp->data);
66                 }
67         }
68         return 1;
69 }
70
71 void pidwait_init(void)
72 {
73         pids = NULL;
74         childcheck_tag = g_timeout_add(1000, (GSourceFunc) child_check, NULL);
75
76         signal_pidwait = signal_get_uniq_id("pidwait");
77 }
78
79 void pidwait_deinit(void)
80 {
81         g_slist_free(pids);
82
83         g_source_remove(childcheck_tag);
84 }