1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 2011 Red Hat, Inc.
4 * glib-unix.c: UNIX specific API wrappers and convenience functions
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Authors: Colin Walters <walters@verbum.org>
24 /* To make bionic export pipe2() */
29 #include "glib-unix.h"
30 #include "gmain-internal.h"
35 #include <sys/syscall.h>
40 /* We want to support these features of Linux even when building GLib
41 * against older versions of system headers. This will allow us to
42 * 'automatically' start supporting a particular feature when GLib is
43 * used with a newer kernel, without recompile.
45 * This means that we're not changing functionality of GLib simply based
46 * on the set of headers we happen to compile against...
49 #ifndef F_LINUX_SPECIFIC_BASE
50 #define F_LINUX_SPECIFIC_BASE 1024
54 #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
55 #define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
57 #define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */
58 #define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */
59 #define F_SEAL_GROW 0x0004 /* prevent file from growing */
60 #define F_SEAL_WRITE 0x0008 /* prevent writes */
67 * @title: UNIX-specific utilities and integration
68 * @short_description: pipes, signal handling
69 * @include: glib-unix.h
71 * Most of GLib is intended to be portable; in contrast, this set of
72 * functions is designed for programs which explicitly target UNIX,
73 * or are using it to build higher level abstractions which would be
74 * conditionally compiled if the platform matches G_OS_UNIX.
76 * To use these functions, you must explicitly include the
77 * "glib-unix.h" header.
80 G_DEFINE_QUARK (g-unix-error-quark, g_unix_error)
83 g_unix_set_error_from_errno (GError **error,
86 g_set_error_literal (error,
89 g_strerror (saved_errno));
96 * @fds: Array of two integers
97 * @flags: Bitfield of file descriptor flags, as for fcntl()
100 * Similar to the UNIX pipe() call, but on modern systems like Linux
101 * uses the pipe2() system call, which atomically creates a pipe with
102 * the configured flags. The only supported flag currently is
103 * %FD_CLOEXEC. If for example you want to configure %O_NONBLOCK, that
104 * must still be done separately with fcntl().
106 * This function does not take %O_CLOEXEC, it takes %FD_CLOEXEC as if
107 * for fcntl(); these are different on Linux/glibc.
109 * Returns: %TRUE on success, %FALSE if not (and errno will be set).
114 g_unix_open_pipe (int *fds,
120 /* We only support FD_CLOEXEC */
121 g_return_val_if_fail ((flags & (FD_CLOEXEC)) == flags, FALSE);
126 if (flags & FD_CLOEXEC)
127 pipe2_flags |= O_CLOEXEC;
129 ecode = pipe2 (fds, pipe2_flags);
130 if (ecode == -1 && errno != ENOSYS)
131 return g_unix_set_error_from_errno (error, errno);
134 /* Fall through on -ENOSYS, we must be running on an old kernel */
139 return g_unix_set_error_from_errno (error, errno);
144 ecode = fcntl (fds[0], F_SETFD, flags);
147 int saved_errno = errno;
150 return g_unix_set_error_from_errno (error, saved_errno);
152 ecode = fcntl (fds[1], F_SETFD, flags);
155 int saved_errno = errno;
158 return g_unix_set_error_from_errno (error, saved_errno);
164 * g_unix_set_fd_nonblocking:
165 * @fd: A file descriptor
166 * @nonblock: If %TRUE, set the descriptor to be non-blocking
169 * Control the non-blocking state of the given file descriptor,
170 * according to @nonblock. On most systems this uses %O_NONBLOCK, but
171 * on some older ones may use %O_NDELAY.
173 * Returns: %TRUE if successful
178 g_unix_set_fd_nonblocking (gint fd,
184 fcntl_flags = fcntl (fd, F_GETFL);
186 if (fcntl_flags == -1)
187 return g_unix_set_error_from_errno (error, errno);
192 fcntl_flags |= O_NONBLOCK;
194 fcntl_flags |= O_NDELAY;
200 fcntl_flags &= ~O_NONBLOCK;
202 fcntl_flags &= ~O_NDELAY;
206 if (fcntl (fd, F_SETFL, fcntl_flags) == -1)
207 return g_unix_set_error_from_errno (error, errno);
210 return g_unix_set_error_from_errno (error, EINVAL);
215 * g_unix_signal_source_new:
216 * @signum: A signal number
218 * Create a #GSource that will be dispatched upon delivery of the UNIX
219 * signal @signum. In GLib versions before 2.36, only `SIGHUP`, `SIGINT`,
220 * `SIGTERM` can be monitored. In GLib 2.36, `SIGUSR1` and `SIGUSR2`
223 * Note that unlike the UNIX default, all sources which have created a
224 * watch will be dispatched, regardless of which underlying thread
225 * invoked g_unix_signal_source_new().
227 * For example, an effective use of this function is to handle `SIGTERM`
228 * cleanly; flushing any outstanding files, and then calling
229 * g_main_loop_quit (). It is not safe to do any of this a regular
230 * UNIX signal handler; your handler may be invoked while malloc() or
231 * another library function is running, causing reentrancy if you
232 * attempt to use it from the handler. None of the GLib/GObject API
233 * is safe against this kind of reentrancy.
235 * The interaction of this source when combined with native UNIX
236 * functions like sigprocmask() is not defined.
238 * The source will not initially be associated with any #GMainContext
239 * and must be added to one with g_source_attach() before it will be
242 * Returns: A newly created #GSource
247 g_unix_signal_source_new (int signum)
249 g_return_val_if_fail (signum == SIGHUP || signum == SIGINT || signum == SIGTERM ||
250 signum == SIGUSR1 || signum == SIGUSR2, NULL);
252 return _g_main_create_unix_signal_watch (signum);
256 * g_unix_signal_add_full:
257 * @priority: the priority of the signal source. Typically this will be in
258 * the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
259 * @signum: Signal number
261 * @user_data: Data for @handler
262 * @notify: #GDestroyNotify for @handler
264 * A convenience function for g_unix_signal_source_new(), which
265 * attaches to the default #GMainContext. You can remove the watch
266 * using g_source_remove().
268 * Returns: An ID (greater than 0) for the event source
270 * Rename to: g_unix_signal_add
274 g_unix_signal_add_full (int priority,
278 GDestroyNotify notify)
283 source = g_unix_signal_source_new (signum);
285 if (priority != G_PRIORITY_DEFAULT)
286 g_source_set_priority (source, priority);
288 g_source_set_callback (source, handler, user_data, notify);
289 id = g_source_attach (source, NULL);
290 g_source_unref (source);
297 * @signum: Signal number
299 * @user_data: Data for @handler
301 * A convenience function for g_unix_signal_source_new(), which
302 * attaches to the default #GMainContext. You can remove the watch
303 * using g_source_remove().
305 * Returns: An ID (greater than 0) for the event source
310 g_unix_signal_add (int signum,
314 return g_unix_signal_add_full (G_PRIORITY_DEFAULT, signum, handler, user_data, NULL);
326 g_unix_fd_source_dispatch (GSource *source,
327 GSourceFunc callback,
330 GUnixFDSource *fd_source = (GUnixFDSource *) source;
331 GUnixFDSourceFunc func = (GUnixFDSourceFunc) callback;
335 g_warning ("GUnixFDSource dispatched without callback\n"
336 "You must call g_source_set_callback().");
340 return (* func) (fd_source->fd, g_source_query_unix_fd (source, fd_source->tag), user_data);
343 GSourceFuncs g_unix_fd_source_funcs = {
344 NULL, NULL, g_unix_fd_source_dispatch, NULL
348 * g_unix_fd_source_new:
349 * @fd: a file descriptor
350 * @condition: IO conditions to watch for on @fd
352 * Creates a #GSource to watch for a particular IO condition on a file
355 * The source will never close the fd -- you must do it yourself.
357 * Returns: the newly created #GSource
362 g_unix_fd_source_new (gint fd,
363 GIOCondition condition)
365 GUnixFDSource *fd_source;
368 source = g_source_new (&g_unix_fd_source_funcs, sizeof (GUnixFDSource));
369 fd_source = (GUnixFDSource *) source;
372 fd_source->tag = g_source_add_unix_fd (source, fd, condition);
378 * g_unix_fd_add_full:
379 * @priority: the priority of the source
380 * @fd: a file descriptor
381 * @condition: IO conditions to watch for on @fd
382 * @function: a #GUnixFDSourceFunc
383 * @user_data: data to pass to @function
384 * @notify: function to call when the idle is removed, or %NULL
386 * Sets a function to be called when the IO condition, as specified by
387 * @condition becomes true for @fd.
389 * This is the same as g_unix_fd_add(), except that it allows you to
390 * specify a non-default priority and a provide a #GDestroyNotify for
393 * Returns: the ID (greater than 0) of the event source
398 g_unix_fd_add_full (gint priority,
400 GIOCondition condition,
401 GUnixFDSourceFunc function,
403 GDestroyNotify notify)
408 g_return_val_if_fail (function != NULL, 0);
410 source = g_unix_fd_source_new (fd, condition);
412 if (priority != G_PRIORITY_DEFAULT)
413 g_source_set_priority (source, priority);
415 g_source_set_callback (source, (GSourceFunc) function, user_data, notify);
416 id = g_source_attach (source, NULL);
417 g_source_unref (source);
424 * @fd: a file descriptor
425 * @condition: IO conditions to watch for on @fd
426 * @function: a #GPollFDFunc
427 * @user_data: data to pass to @function
429 * Sets a function to be called when the IO condition, as specified by
430 * @condition becomes true for @fd.
432 * @function will be called when the specified IO condition becomes
433 * %TRUE. The function is expected to clear whatever event caused the
434 * IO condition to become true and return %TRUE in order to be notified
435 * when it happens again. If @function returns %FALSE then the watch
438 * The return value of this function can be passed to g_source_remove()
439 * to cancel the watch at any time that it exists.
441 * The source will never close the fd -- you must do it yourself.
443 * Returns: the ID (greater than 0) of the event source
448 g_unix_fd_add (gint fd,
449 GIOCondition condition,
450 GUnixFDSourceFunc function,
453 return g_unix_fd_add_full (G_PRIORITY_DEFAULT, fd, condition, function, user_data, NULL);
457 * g_unix_fd_ensure_zero_copy_safe:
458 * @fd: a file descriptor
460 * Checks whether @fd can be use in zero-copy mode. On Linux, this checks that
461 * the descriptor is a memfd, created with memfd_create(), and tries to apply
462 * seals to it so that it can be safely consumed by another process. On other
463 * Unix systems, this function will fail.
465 * Returns: whether the fd is safe to use in zero-copy mode. Sealing of memfds
466 * is required for the @fd to be considered safe. If sealing fails, or
467 * memfds are not available, or the @fd is not a memfd, returns %FALSE
472 g_unix_fd_ensure_zero_copy_safe (gint fd)
476 const gint IMMUTABLE_SEALS = F_SEAL_WRITE |
481 g_return_val_if_fail (fd >= 0, FALSE);
483 /* Seal the fd if possible (only on Linux 3.17+, and if the fd was created
484 * with memfd_create()). */
485 seals = fcntl (fd, F_GET_SEALS);
488 g_debug ("Retrieving fd seals failed: %s", g_strerror (errno));
492 /* Seal the fd, if it is not already. */
493 if ((seals & (IMMUTABLE_SEALS)) >= IMMUTABLE_SEALS)
495 g_debug ("%s", "fd already sealed");
501 error = fcntl (fd, F_ADD_SEALS, IMMUTABLE_SEALS);
504 g_debug ("fd sealing failed: %s", g_strerror (errno));