/src/irssi/subprojects/glib-2.74.3/glib/glib-unix.c
Line | Count | Source |
1 | | /* GLIB - Library of useful routines for C programming |
2 | | * Copyright (C) 2011 Red Hat, Inc. |
3 | | * |
4 | | * glib-unix.c: UNIX specific API wrappers and convenience functions |
5 | | * |
6 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
7 | | * |
8 | | * This library is free software; you can redistribute it and/or |
9 | | * modify it under the terms of the GNU Lesser General Public |
10 | | * License as published by the Free Software Foundation; either |
11 | | * version 2.1 of the License, or (at your option) any later version. |
12 | | * |
13 | | * This library is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | | * Lesser General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public |
19 | | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
20 | | * |
21 | | * Authors: Colin Walters <walters@verbum.org> |
22 | | */ |
23 | | |
24 | | #include "config.h" |
25 | | |
26 | | /* To make bionic export pipe2() */ |
27 | | #ifndef _GNU_SOURCE |
28 | | #define _GNU_SOURCE 1 |
29 | | #endif |
30 | | |
31 | | #include "glib-unix.h" |
32 | | #include "gmain-internal.h" |
33 | | |
34 | | #include <string.h> |
35 | | #include <sys/types.h> |
36 | | #include <pwd.h> |
37 | | |
38 | | G_STATIC_ASSERT (sizeof (ssize_t) == GLIB_SIZEOF_SSIZE_T); |
39 | | G_STATIC_ASSERT (G_ALIGNOF (gssize) == G_ALIGNOF (ssize_t)); |
40 | | |
41 | | G_STATIC_ASSERT (sizeof (GPid) == sizeof (pid_t)); |
42 | | G_STATIC_ASSERT (G_ALIGNOF (GPid) == G_ALIGNOF (pid_t)); |
43 | | |
44 | | /** |
45 | | * SECTION:gunix |
46 | | * @title: UNIX-specific utilities and integration |
47 | | * @short_description: pipes, signal handling |
48 | | * @include: glib-unix.h |
49 | | * |
50 | | * Most of GLib is intended to be portable; in contrast, this set of |
51 | | * functions is designed for programs which explicitly target UNIX, |
52 | | * or are using it to build higher level abstractions which would be |
53 | | * conditionally compiled if the platform matches %G_OS_UNIX. |
54 | | * |
55 | | * To use these functions, you must explicitly include the |
56 | | * "glib-unix.h" header. |
57 | | */ |
58 | | |
59 | | G_DEFINE_QUARK (g-unix-error-quark, g_unix_error) |
60 | | |
61 | | static gboolean |
62 | | g_unix_set_error_from_errno (GError **error, |
63 | | gint saved_errno) |
64 | 0 | { |
65 | 0 | g_set_error_literal (error, |
66 | 0 | G_UNIX_ERROR, |
67 | 0 | 0, |
68 | 0 | g_strerror (saved_errno)); |
69 | 0 | errno = saved_errno; |
70 | 0 | return FALSE; |
71 | 0 | } |
72 | | |
73 | | /** |
74 | | * g_unix_open_pipe: |
75 | | * @fds: (array fixed-size=2): Array of two integers |
76 | | * @flags: Bitfield of file descriptor flags, as for fcntl() |
77 | | * @error: a #GError |
78 | | * |
79 | | * Similar to the UNIX pipe() call, but on modern systems like Linux |
80 | | * uses the pipe2() system call, which atomically creates a pipe with |
81 | | * the configured flags. The only supported flag currently is |
82 | | * %FD_CLOEXEC. If for example you want to configure %O_NONBLOCK, that |
83 | | * must still be done separately with fcntl(). |
84 | | * |
85 | | * This function does not take %O_CLOEXEC, it takes %FD_CLOEXEC as if |
86 | | * for fcntl(); these are different on Linux/glibc. |
87 | | * |
88 | | * Returns: %TRUE on success, %FALSE if not (and errno will be set). |
89 | | * |
90 | | * Since: 2.30 |
91 | | */ |
92 | | gboolean |
93 | | g_unix_open_pipe (int *fds, |
94 | | int flags, |
95 | | GError **error) |
96 | 0 | { |
97 | 0 | int ecode; |
98 | | |
99 | | /* We only support FD_CLOEXEC */ |
100 | 0 | g_return_val_if_fail ((flags & (FD_CLOEXEC)) == flags, FALSE); |
101 | | |
102 | 0 | #ifdef HAVE_PIPE2 |
103 | 0 | { |
104 | 0 | int pipe2_flags = 0; |
105 | 0 | if (flags & FD_CLOEXEC) |
106 | 0 | pipe2_flags |= O_CLOEXEC; |
107 | | /* Atomic */ |
108 | 0 | ecode = pipe2 (fds, pipe2_flags); |
109 | 0 | if (ecode == -1 && errno != ENOSYS) |
110 | 0 | return g_unix_set_error_from_errno (error, errno); |
111 | 0 | else if (ecode == 0) |
112 | 0 | return TRUE; |
113 | | /* Fall through on -ENOSYS, we must be running on an old kernel */ |
114 | 0 | } |
115 | 0 | #endif |
116 | 0 | ecode = pipe (fds); |
117 | 0 | if (ecode == -1) |
118 | 0 | return g_unix_set_error_from_errno (error, errno); |
119 | | |
120 | 0 | if (flags == 0) |
121 | 0 | return TRUE; |
122 | | |
123 | 0 | ecode = fcntl (fds[0], F_SETFD, flags); |
124 | 0 | if (ecode == -1) |
125 | 0 | { |
126 | 0 | int saved_errno = errno; |
127 | 0 | close (fds[0]); |
128 | 0 | close (fds[1]); |
129 | 0 | return g_unix_set_error_from_errno (error, saved_errno); |
130 | 0 | } |
131 | 0 | ecode = fcntl (fds[1], F_SETFD, flags); |
132 | 0 | if (ecode == -1) |
133 | 0 | { |
134 | 0 | int saved_errno = errno; |
135 | 0 | close (fds[0]); |
136 | 0 | close (fds[1]); |
137 | 0 | return g_unix_set_error_from_errno (error, saved_errno); |
138 | 0 | } |
139 | 0 | return TRUE; |
140 | 0 | } |
141 | | |
142 | | /** |
143 | | * g_unix_set_fd_nonblocking: |
144 | | * @fd: A file descriptor |
145 | | * @nonblock: If %TRUE, set the descriptor to be non-blocking |
146 | | * @error: a #GError |
147 | | * |
148 | | * Control the non-blocking state of the given file descriptor, |
149 | | * according to @nonblock. On most systems this uses %O_NONBLOCK, but |
150 | | * on some older ones may use %O_NDELAY. |
151 | | * |
152 | | * Returns: %TRUE if successful |
153 | | * |
154 | | * Since: 2.30 |
155 | | */ |
156 | | gboolean |
157 | | g_unix_set_fd_nonblocking (gint fd, |
158 | | gboolean nonblock, |
159 | | GError **error) |
160 | 0 | { |
161 | 0 | #ifdef F_GETFL |
162 | 0 | glong fcntl_flags; |
163 | 0 | fcntl_flags = fcntl (fd, F_GETFL); |
164 | |
|
165 | 0 | if (fcntl_flags == -1) |
166 | 0 | return g_unix_set_error_from_errno (error, errno); |
167 | | |
168 | 0 | if (nonblock) |
169 | 0 | { |
170 | 0 | #ifdef O_NONBLOCK |
171 | 0 | fcntl_flags |= O_NONBLOCK; |
172 | | #else |
173 | | fcntl_flags |= O_NDELAY; |
174 | | #endif |
175 | 0 | } |
176 | 0 | else |
177 | 0 | { |
178 | 0 | #ifdef O_NONBLOCK |
179 | 0 | fcntl_flags &= ~O_NONBLOCK; |
180 | | #else |
181 | | fcntl_flags &= ~O_NDELAY; |
182 | | #endif |
183 | 0 | } |
184 | |
|
185 | 0 | if (fcntl (fd, F_SETFL, fcntl_flags) == -1) |
186 | 0 | return g_unix_set_error_from_errno (error, errno); |
187 | 0 | return TRUE; |
188 | | #else |
189 | | return g_unix_set_error_from_errno (error, EINVAL); |
190 | | #endif |
191 | 0 | } |
192 | | |
193 | | /** |
194 | | * g_unix_signal_source_new: |
195 | | * @signum: A signal number |
196 | | * |
197 | | * Create a #GSource that will be dispatched upon delivery of the UNIX |
198 | | * signal @signum. In GLib versions before 2.36, only `SIGHUP`, `SIGINT`, |
199 | | * `SIGTERM` can be monitored. In GLib 2.36, `SIGUSR1` and `SIGUSR2` |
200 | | * were added. In GLib 2.54, `SIGWINCH` was added. |
201 | | * |
202 | | * Note that unlike the UNIX default, all sources which have created a |
203 | | * watch will be dispatched, regardless of which underlying thread |
204 | | * invoked g_unix_signal_source_new(). |
205 | | * |
206 | | * For example, an effective use of this function is to handle `SIGTERM` |
207 | | * cleanly; flushing any outstanding files, and then calling |
208 | | * g_main_loop_quit(). It is not safe to do any of this from a regular |
209 | | * UNIX signal handler; such a handler may be invoked while malloc() or |
210 | | * another library function is running, causing reentrancy issues if the |
211 | | * handler attempts to use those functions. None of the GLib/GObject |
212 | | * API is safe against this kind of reentrancy. |
213 | | * |
214 | | * The interaction of this source when combined with native UNIX |
215 | | * functions like sigprocmask() is not defined. |
216 | | * |
217 | | * The source will not initially be associated with any #GMainContext |
218 | | * and must be added to one with g_source_attach() before it will be |
219 | | * executed. |
220 | | * |
221 | | * Returns: A newly created #GSource |
222 | | * |
223 | | * Since: 2.30 |
224 | | */ |
225 | | GSource * |
226 | | g_unix_signal_source_new (int signum) |
227 | 0 | { |
228 | 0 | g_return_val_if_fail (signum == SIGHUP || signum == SIGINT || signum == SIGTERM || |
229 | 0 | signum == SIGUSR1 || signum == SIGUSR2 || signum == SIGWINCH, |
230 | 0 | NULL); |
231 | | |
232 | 0 | return _g_main_create_unix_signal_watch (signum); |
233 | 0 | } |
234 | | |
235 | | /** |
236 | | * g_unix_signal_add_full: (rename-to g_unix_signal_add) |
237 | | * @priority: the priority of the signal source. Typically this will be in |
238 | | * the range between %G_PRIORITY_DEFAULT and %G_PRIORITY_HIGH. |
239 | | * @signum: Signal number |
240 | | * @handler: Callback |
241 | | * @user_data: Data for @handler |
242 | | * @notify: #GDestroyNotify for @handler |
243 | | * |
244 | | * A convenience function for g_unix_signal_source_new(), which |
245 | | * attaches to the default #GMainContext. You can remove the watch |
246 | | * using g_source_remove(). |
247 | | * |
248 | | * Returns: An ID (greater than 0) for the event source |
249 | | * |
250 | | * Since: 2.30 |
251 | | */ |
252 | | guint |
253 | | g_unix_signal_add_full (int priority, |
254 | | int signum, |
255 | | GSourceFunc handler, |
256 | | gpointer user_data, |
257 | | GDestroyNotify notify) |
258 | 0 | { |
259 | 0 | guint id; |
260 | 0 | GSource *source; |
261 | |
|
262 | 0 | source = g_unix_signal_source_new (signum); |
263 | |
|
264 | 0 | if (priority != G_PRIORITY_DEFAULT) |
265 | 0 | g_source_set_priority (source, priority); |
266 | |
|
267 | 0 | g_source_set_callback (source, handler, user_data, notify); |
268 | 0 | id = g_source_attach (source, NULL); |
269 | 0 | g_source_unref (source); |
270 | |
|
271 | 0 | return id; |
272 | 0 | } |
273 | | |
274 | | /** |
275 | | * g_unix_signal_add: |
276 | | * @signum: Signal number |
277 | | * @handler: Callback |
278 | | * @user_data: Data for @handler |
279 | | * |
280 | | * A convenience function for g_unix_signal_source_new(), which |
281 | | * attaches to the default #GMainContext. You can remove the watch |
282 | | * using g_source_remove(). |
283 | | * |
284 | | * Returns: An ID (greater than 0) for the event source |
285 | | * |
286 | | * Since: 2.30 |
287 | | */ |
288 | | guint |
289 | | g_unix_signal_add (int signum, |
290 | | GSourceFunc handler, |
291 | | gpointer user_data) |
292 | 0 | { |
293 | 0 | return g_unix_signal_add_full (G_PRIORITY_DEFAULT, signum, handler, user_data, NULL); |
294 | 0 | } |
295 | | |
296 | | typedef struct |
297 | | { |
298 | | GSource source; |
299 | | |
300 | | gint fd; |
301 | | gpointer tag; |
302 | | } GUnixFDSource; |
303 | | |
304 | | static gboolean |
305 | | g_unix_fd_source_dispatch (GSource *source, |
306 | | GSourceFunc callback, |
307 | | gpointer user_data) |
308 | 0 | { |
309 | 0 | GUnixFDSource *fd_source = (GUnixFDSource *) source; |
310 | 0 | GUnixFDSourceFunc func = (GUnixFDSourceFunc) callback; |
311 | |
|
312 | 0 | if (!callback) |
313 | 0 | { |
314 | 0 | g_warning ("GUnixFDSource dispatched without callback. " |
315 | 0 | "You must call g_source_set_callback()."); |
316 | 0 | return FALSE; |
317 | 0 | } |
318 | | |
319 | 0 | return (* func) (fd_source->fd, g_source_query_unix_fd (source, fd_source->tag), user_data); |
320 | 0 | } |
321 | | |
322 | | GSourceFuncs g_unix_fd_source_funcs = { |
323 | | NULL, NULL, g_unix_fd_source_dispatch, NULL, NULL, NULL |
324 | | }; |
325 | | |
326 | | /** |
327 | | * g_unix_fd_source_new: |
328 | | * @fd: a file descriptor |
329 | | * @condition: IO conditions to watch for on @fd |
330 | | * |
331 | | * Creates a #GSource to watch for a particular IO condition on a file |
332 | | * descriptor. |
333 | | * |
334 | | * The source will never close the fd -- you must do it yourself. |
335 | | * |
336 | | * Returns: the newly created #GSource |
337 | | * |
338 | | * Since: 2.36 |
339 | | **/ |
340 | | GSource * |
341 | | g_unix_fd_source_new (gint fd, |
342 | | GIOCondition condition) |
343 | 0 | { |
344 | 0 | GUnixFDSource *fd_source; |
345 | 0 | GSource *source; |
346 | |
|
347 | 0 | source = g_source_new (&g_unix_fd_source_funcs, sizeof (GUnixFDSource)); |
348 | 0 | fd_source = (GUnixFDSource *) source; |
349 | |
|
350 | 0 | fd_source->fd = fd; |
351 | 0 | fd_source->tag = g_source_add_unix_fd (source, fd, condition); |
352 | |
|
353 | 0 | return source; |
354 | 0 | } |
355 | | |
356 | | /** |
357 | | * g_unix_fd_add_full: |
358 | | * @priority: the priority of the source |
359 | | * @fd: a file descriptor |
360 | | * @condition: IO conditions to watch for on @fd |
361 | | * @function: a #GUnixFDSourceFunc |
362 | | * @user_data: data to pass to @function |
363 | | * @notify: function to call when the idle is removed, or %NULL |
364 | | * |
365 | | * Sets a function to be called when the IO condition, as specified by |
366 | | * @condition becomes true for @fd. |
367 | | * |
368 | | * This is the same as g_unix_fd_add(), except that it allows you to |
369 | | * specify a non-default priority and a provide a #GDestroyNotify for |
370 | | * @user_data. |
371 | | * |
372 | | * Returns: the ID (greater than 0) of the event source |
373 | | * |
374 | | * Since: 2.36 |
375 | | **/ |
376 | | guint |
377 | | g_unix_fd_add_full (gint priority, |
378 | | gint fd, |
379 | | GIOCondition condition, |
380 | | GUnixFDSourceFunc function, |
381 | | gpointer user_data, |
382 | | GDestroyNotify notify) |
383 | 0 | { |
384 | 0 | GSource *source; |
385 | 0 | guint id; |
386 | |
|
387 | 0 | g_return_val_if_fail (function != NULL, 0); |
388 | | |
389 | 0 | source = g_unix_fd_source_new (fd, condition); |
390 | |
|
391 | 0 | if (priority != G_PRIORITY_DEFAULT) |
392 | 0 | g_source_set_priority (source, priority); |
393 | |
|
394 | 0 | g_source_set_callback (source, (GSourceFunc) function, user_data, notify); |
395 | 0 | id = g_source_attach (source, NULL); |
396 | 0 | g_source_unref (source); |
397 | |
|
398 | 0 | return id; |
399 | 0 | } |
400 | | |
401 | | /** |
402 | | * g_unix_fd_add: |
403 | | * @fd: a file descriptor |
404 | | * @condition: IO conditions to watch for on @fd |
405 | | * @function: a #GUnixFDSourceFunc |
406 | | * @user_data: data to pass to @function |
407 | | * |
408 | | * Sets a function to be called when the IO condition, as specified by |
409 | | * @condition becomes true for @fd. |
410 | | * |
411 | | * @function will be called when the specified IO condition becomes |
412 | | * %TRUE. The function is expected to clear whatever event caused the |
413 | | * IO condition to become true and return %TRUE in order to be notified |
414 | | * when it happens again. If @function returns %FALSE then the watch |
415 | | * will be cancelled. |
416 | | * |
417 | | * The return value of this function can be passed to g_source_remove() |
418 | | * to cancel the watch at any time that it exists. |
419 | | * |
420 | | * The source will never close the fd -- you must do it yourself. |
421 | | * |
422 | | * Returns: the ID (greater than 0) of the event source |
423 | | * |
424 | | * Since: 2.36 |
425 | | **/ |
426 | | guint |
427 | | g_unix_fd_add (gint fd, |
428 | | GIOCondition condition, |
429 | | GUnixFDSourceFunc function, |
430 | | gpointer user_data) |
431 | 0 | { |
432 | 0 | return g_unix_fd_add_full (G_PRIORITY_DEFAULT, fd, condition, function, user_data, NULL); |
433 | 0 | } |
434 | | |
435 | | /** |
436 | | * g_unix_get_passwd_entry: |
437 | | * @user_name: the username to get the passwd file entry for |
438 | | * @error: return location for a #GError, or %NULL |
439 | | * |
440 | | * Get the `passwd` file entry for the given @user_name using `getpwnam_r()`. |
441 | | * This can fail if the given @user_name doesn’t exist. |
442 | | * |
443 | | * The returned `struct passwd` has been allocated using g_malloc() and should |
444 | | * be freed using g_free(). The strings referenced by the returned struct are |
445 | | * included in the same allocation, so are valid until the `struct passwd` is |
446 | | * freed. |
447 | | * |
448 | | * This function is safe to call from multiple threads concurrently. |
449 | | * |
450 | | * You will need to include `pwd.h` to get the definition of `struct passwd`. |
451 | | * |
452 | | * Returns: (transfer full): passwd entry, or %NULL on error; free the returned |
453 | | * value with g_free() |
454 | | * Since: 2.64 |
455 | | */ |
456 | | struct passwd * |
457 | | g_unix_get_passwd_entry (const gchar *user_name, |
458 | | GError **error) |
459 | 0 | { |
460 | 0 | struct passwd *passwd_file_entry; |
461 | 0 | struct |
462 | 0 | { |
463 | 0 | struct passwd pwd; |
464 | 0 | char string_buffer[]; |
465 | 0 | } *buffer = NULL; |
466 | 0 | gsize string_buffer_size = 0; |
467 | 0 | GError *local_error = NULL; |
468 | |
|
469 | 0 | g_return_val_if_fail (user_name != NULL, NULL); |
470 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
471 | | |
472 | 0 | #ifdef _SC_GETPW_R_SIZE_MAX |
473 | 0 | { |
474 | | /* Get the recommended buffer size */ |
475 | 0 | glong string_buffer_size_long = sysconf (_SC_GETPW_R_SIZE_MAX); |
476 | 0 | if (string_buffer_size_long > 0) |
477 | 0 | string_buffer_size = string_buffer_size_long; |
478 | 0 | } |
479 | 0 | #endif /* _SC_GETPW_R_SIZE_MAX */ |
480 | | |
481 | | /* Default starting size. */ |
482 | 0 | if (string_buffer_size == 0) |
483 | 0 | string_buffer_size = 64; |
484 | |
|
485 | 0 | do |
486 | 0 | { |
487 | 0 | int retval; |
488 | |
|
489 | 0 | g_free (buffer); |
490 | | /* Allocate space for the `struct passwd`, and then a buffer for all its |
491 | | * strings (whose size is @string_buffer_size, which increases in this |
492 | | * loop until it’s big enough). Add 6 extra bytes to work around a bug in |
493 | | * macOS < 10.3. See #156446. |
494 | | */ |
495 | 0 | buffer = g_malloc0 (sizeof (*buffer) + string_buffer_size + 6); |
496 | |
|
497 | 0 | retval = getpwnam_r (user_name, &buffer->pwd, buffer->string_buffer, |
498 | 0 | string_buffer_size, &passwd_file_entry); |
499 | | |
500 | | /* Bail out if: the lookup was successful, or if the user id can't be |
501 | | * found (should be pretty rare case actually), or if the buffer should be |
502 | | * big enough and yet lookups are still not successful. |
503 | | */ |
504 | 0 | if (passwd_file_entry != NULL) |
505 | 0 | { |
506 | | /* Success. */ |
507 | 0 | break; |
508 | 0 | } |
509 | 0 | else if (retval == 0 || |
510 | 0 | retval == ENOENT || retval == ESRCH || |
511 | 0 | retval == EBADF || retval == EPERM) |
512 | 0 | { |
513 | | /* Username not found. */ |
514 | 0 | g_unix_set_error_from_errno (&local_error, retval); |
515 | 0 | break; |
516 | 0 | } |
517 | 0 | else if (retval == ERANGE) |
518 | 0 | { |
519 | | /* Can’t allocate enough string buffer space. */ |
520 | 0 | if (string_buffer_size > 32 * 1024) |
521 | 0 | { |
522 | 0 | g_unix_set_error_from_errno (&local_error, retval); |
523 | 0 | break; |
524 | 0 | } |
525 | | |
526 | 0 | string_buffer_size *= 2; |
527 | 0 | continue; |
528 | 0 | } |
529 | 0 | else |
530 | 0 | { |
531 | 0 | g_unix_set_error_from_errno (&local_error, retval); |
532 | 0 | break; |
533 | 0 | } |
534 | 0 | } |
535 | 0 | while (passwd_file_entry == NULL); |
536 | |
|
537 | 0 | g_assert (passwd_file_entry == NULL || |
538 | 0 | (gpointer) passwd_file_entry == (gpointer) buffer); |
539 | | |
540 | | /* Success or error. */ |
541 | 0 | if (local_error != NULL) |
542 | 0 | { |
543 | 0 | g_clear_pointer (&buffer, g_free); |
544 | 0 | g_propagate_error (error, g_steal_pointer (&local_error)); |
545 | 0 | } |
546 | |
|
547 | 0 | return (struct passwd *) g_steal_pointer (&buffer); |
548 | 0 | } |