Debian Codegraph @

src/dex-unix-signal.c

raw symbols SCIP
1 /*
2 * dex-unix-signal.c
3 *
4 * Copyright 2022-2023 Christian Hergert <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of the
9 * License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser 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, see <http://www.gnu.org/licenses/>.
18 *
19 * SPDX-License-Identifier: LGPL-2.1-or-later
20 */
21
22 #include "config.h"
23
24 #include <glib-unix.h>
25
26 #include "dex-compat-private.h"
27 #include "dex-future-private.h"
28 #include "dex-unix-signal.h"
29
30 /**
31 * DexUnixSignal:
32 *
33 * `DexUnixSignal` is a [[email protected]] that will resolve when a specific
34 * unix signal has been received.
35 *
36 * Use this when you want to handle a signal from your main loop rather than
37 * from a resticted operating signal handler.
38 *
39 * On Linux, this uses a signalfd.
40 */
41
42 struct _DexUnixSignal
43 {
44 DexFuture parent_instance;
45 GSource *source;
46 int signum;
47 };
48
49 typedef struct _DexUnixSignalClass
50 {
51 DexFutureClass parent_class;
52 } DexUnixSignalClass;
53
54 DEX_DEFINE_FINAL_TYPE (DexUnixSignal, dex_unix_signal, DEX_TYPE_FUTURE)
55
56 static void
57 dex_unix_signal_finalize (DexObject *object)
58 {
59 DexUnixSignal *unix_signal = DEX_UNIX_SIGNAL (object);
60
61 if (unix_signal->source != NULL)
62 {
63 g_source_destroy (unix_signal->source);
64 g_clear_pointer (&unix_signal->source, g_source_unref);
65 }
66
67 DEX_OBJECT_CLASS (dex_unix_signal_parent_class)->finalize (object);
68 }
69
70 static void
71 dex_unix_signal_class_init (DexUnixSignalClass *unix_signal_class)
72 {
73 DexObjectClass *object_class = DEX_OBJECT_CLASS (unix_signal_class);
74
75 object_class->finalize = dex_unix_signal_finalize;
76 }
77
78 static void
79 dex_unix_signal_init (DexUnixSignal *unix_signal)
80 {
81 }
82
83 static gboolean
84 dex_unix_signal_source_func (gpointer data)
85 {
86 DexWeakRef *wr = data;
87 DexUnixSignal *unix_signal = dex_weak_ref_get (wr);
88
89 g_assert (!unix_signal || DEX_IS_UNIX_SIGNAL (unix_signal));
90
91 if (unix_signal != NULL)
92 {
93 GValue value = G_VALUE_INIT;
94
95 g_value_init (&value, G_TYPE_INT);
96 g_value_set_int (&value, unix_signal->signum);
97 dex_future_complete (DEX_FUTURE (unix_signal), &value, NULL);
98
99 dex_unref (unix_signal);
100 }
101
102 return G_SOURCE_REMOVE;
103 }
104
105 static void
106 clear_weak_ref (gpointer data)
107 {
108 dex_weak_ref_clear (data);
109 g_free (data);
110 }
111
112 /**
113 * dex_unix_signal_new:
114 * @signum: a unix signal number
115 *
116 * Creates a new [[email protected]] that completes when @signum is delivered
117 * to the process.
118 *
119 * @signum must be one of SIGHUP, SIGINT, SIGTERM, SIGUSR1, SIGUSR2, or
120 * SIGWINCH.
121 *
122 * This API is only supported on UNIX-like systems.
123 *
124 * Returns: (transfer full): a new [[email protected]]
125 */
126 DexFuture *
127 dex_unix_signal_new (int signum)
128 {
129 DexUnixSignal *unix_signal;
130 const char *name = NULL;
131 DexWeakRef *wr;
132
133 g_return_val_if_fail (signum == SIGHUP || signum == SIGINT || signum == SIGTERM ||
134 signum == SIGUSR1 || signum == SIGUSR2 || signum == SIGWINCH,
135 NULL);
136
137 switch (signum)
138 {
139 case SIGHUP: name = "[dex-unix-signal-SIGHUP]"; break;
140 case SIGINT: name = "[dex-unix-signal-SIGINT]"; break;
141 case SIGTERM: name = "[dex-unix-signal-SIGTERM]"; break;
142 case SIGUSR1: name = "[dex-unix-signal-SIGUSR1]"; break;
143 case SIGUSR2: name = "[dex-unix-signal-SIGUSR2]"; break;
144 case SIGWINCH: name = "[dex-unix-signal-SIGWINCH]"; break;
145 default:
146 g_assert_not_reached ();
147 }
148
149 unix_signal = (DexUnixSignal *)dex_object_create_instance (DEX_TYPE_UNIX_SIGNAL);
150 unix_signal->signum = signum;
151 unix_signal->source = g_unix_signal_source_new (signum);
152
153 wr = g_new0 (DexWeakRef, 1);
154 dex_weak_ref_init (wr, unix_signal);
155
156 g_source_set_callback (unix_signal->source,
157 dex_unix_signal_source_func,
158 wr,
159 clear_weak_ref);
160 _g_source_set_static_name (unix_signal->source, name);
161 g_source_attach (unix_signal->source, NULL);
162
163 return DEX_FUTURE (unix_signal);
164 }
165
166 /**
167 * dex_unix_signal_get_signum:
168 * @unix_signal: a [[email protected]]
169 *
170 * Get the signal number that the future represents.
171 *
172 * Since: 1.0
173 */
174 int
175 dex_unix_signal_get_signum (DexUnixSignal *unix_signal)
176 {
177 g_return_val_if_fail (DEX_IS_UNIX_SIGNAL (unix_signal), 0);
178
179 return unix_signal->signum;
180 }