Merge Irssi 0.8.16-rc1
[silc.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 along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "module.h"
22 #include "signals.h"
23 #include "modules.h"
24
25 static GHashTable *child_pids;
26 static GSList *pids;
27
28 static int signal_pidwait;
29
30 static void sig_child(GPid pid, gint status, gpointer data)
31 {
32         signal_emit_id(signal_pidwait, 2, GINT_TO_POINTER(pid),
33                        GINT_TO_POINTER(status));
34         g_hash_table_remove(child_pids, GINT_TO_POINTER(pid));
35         pids = g_slist_remove(pids, GINT_TO_POINTER(pid));
36 }
37
38 /* add a pid to wait list */
39 void pidwait_add(int pid)
40 {
41         if (g_hash_table_lookup(child_pids, GINT_TO_POINTER(pid)) == NULL) {
42                 int id = g_child_watch_add_full(10, pid, sig_child, NULL, NULL);
43                 g_hash_table_insert(child_pids, GINT_TO_POINTER(pid), GINT_TO_POINTER(id));
44                 pids = g_slist_append(pids, GINT_TO_POINTER(pid));
45         }
46 }
47
48 /* remove pid from wait list */
49 void pidwait_remove(int pid)
50 {
51         gpointer id = g_hash_table_lookup(child_pids, GINT_TO_POINTER(pid));
52         if (id != NULL) {
53                 g_source_remove(GPOINTER_TO_INT(id));
54                 g_hash_table_remove(child_pids, GINT_TO_POINTER(pid));
55                 pids = g_slist_remove(pids, GINT_TO_POINTER(pid));
56         }
57 }
58
59 /* return list of pids that are being waited.
60    don't free the return value. */
61 GSList *pidwait_get_pids(void)
62 {
63         return pids;
64 }
65
66 void pidwait_init(void)
67 {
68         child_pids = g_hash_table_new(g_direct_hash, g_direct_equal);
69         pids = NULL;
70
71         signal_pidwait = signal_get_uniq_id("pidwait");
72 }
73
74 void pidwait_deinit(void)
75 {
76         g_hash_table_destroy(child_pids);
77         g_slist_free(pids);
78 }