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, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: Colin Walters <walters@verbum.org>
26 #include "glib-unix.h"
27 #include "gmain-internal.h"
33 * @title: UNIX-specific utilities and integration
34 * @short_description: pipes, signal handling
35 * @include: glib-unix.h
37 * Most of GLib is intended to be portable; in contrast, this set of
38 * functions is designed for programs which explicitly target UNIX,
39 * or are using it to build higher level abstractions which would be
40 * conditionally compiled if the platform matches G_OS_UNIX.
42 * To use these functions, you must explicitly include the
43 * "glib-unix.h" header.
47 g_unix_error_quark (void)
49 return g_quark_from_static_string ("g-unix-error-quark");
53 g_unix_set_error_from_errno (GError **error,
56 g_set_error_literal (error,
59 g_strerror (saved_errno));
66 * @fds: Array of two integers
67 * @flags: Bitfield of file descriptor flags, see "man 2 fcntl"
70 * Similar to the UNIX pipe() call, but on modern systems like Linux
71 * uses the pipe2() system call, which atomically creates a pipe with
72 * the configured flags. The only supported flag currently is
73 * %FD_CLOEXEC. If for example you want to configure %O_NONBLOCK,
74 * that must still be done separately with fcntl().
76 * <note>This function does *not* take %O_CLOEXEC, it takes
77 * %FD_CLOEXEC as if for fcntl(); these are different on
80 * Returns: %TRUE on success, %FALSE if not (and errno will be set).
85 g_unix_open_pipe (int *fds,
91 /* We only support FD_CLOEXEC */
92 g_return_val_if_fail ((flags & (FD_CLOEXEC)) == flags, FALSE);
97 if (flags & FD_CLOEXEC)
98 pipe2_flags |= O_CLOEXEC;
100 ecode = pipe2 (fds, pipe2_flags);
101 if (ecode == -1 && errno != ENOSYS)
102 return g_unix_set_error_from_errno (error, errno);
105 /* Fall through on -ENOSYS, we must be running on an old kernel */
110 return g_unix_set_error_from_errno (error, errno);
111 ecode = fcntl (fds[0], flags);
114 int saved_errno = errno;
116 return g_unix_set_error_from_errno (error, saved_errno);
118 ecode = fcntl (fds[0], flags);
121 int saved_errno = errno;
124 return g_unix_set_error_from_errno (error, saved_errno);
130 * g_unix_set_fd_nonblocking:
131 * @fd: A file descriptor
132 * @nonblock: If %TRUE, set the descriptor to be non-blocking
135 * Control the non-blocking state of the given file descriptor,
136 * according to @nonblock. On most systems this uses %O_NONBLOCK, but
137 * on some older ones may use %O_NDELAY.
139 * Returns: %TRUE if successful
144 g_unix_set_fd_nonblocking (gint fd,
150 fcntl_flags = fcntl (fd, F_GETFL);
152 if (fcntl_flags == -1)
153 return g_unix_set_error_from_errno (error, errno);
158 fcntl_flags |= O_NONBLOCK;
160 fcntl_flags |= O_NDELAY;
166 fcntl_flags &= ~O_NONBLOCK;
168 fcntl_flags &= ~O_NDELAY;
172 if (fcntl (fd, F_SETFL, fcntl_flags) == -1)
173 return g_unix_set_error_from_errno (error, errno);
176 return g_unix_set_error_from_errno (error, EINVAL);
182 * g_unix_signal_source_new:
183 * @signum: A signal number
185 * Create a #GSource that will be dispatched upon delivery of the UNIX
186 * signal @signum. Currently only %SIGHUP, %SIGINT, and %SIGTERM can
187 * be monitored. Note that unlike the UNIX default, all sources which
188 * have created a watch will be dispatched, regardless of which
189 * underlying thread invoked g_unix_signal_source_new().
191 * For example, an effective use of this function is to handle SIGTERM
192 * cleanly; flushing any outstanding files, and then calling
193 * g_main_loop_quit (). It is not safe to do any of this a regular
194 * UNIX signal handler; your handler may be invoked while malloc() or
195 * another library function is running, causing reentrancy if you
196 * attempt to use it from the handler. None of the GLib/GObject API
197 * is safe against this kind of reentrancy.
199 * The interaction of this source when combined with native UNIX
200 * functions like sigprocmask() is not defined.
202 * <note>For reliable behavior, if your program links to gthread
203 * (either directly or indirectly via GObject, GIO, or a higher level
204 * library), you should ensure g_thread_init() is called before using
205 * this function. For example, if your program uses GObject, call
206 * g_type_init().</note>
208 * The source will not initially be associated with any #GMainContext
209 * and must be added to one with g_source_attach() before it will be
212 * Returns: A newly created #GSource
217 g_unix_signal_source_new (int signum)
219 g_return_val_if_fail (signum == SIGHUP || signum == SIGINT || signum == SIGTERM, NULL);
221 return _g_main_create_unix_signal_watch (signum);
225 * g_unix_signal_add_watch_full:
226 * @signum: Signal number
227 * @priority: the priority of the signal source. Typically this will be in
228 * the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
230 * @user_data: Data for @handler
231 * @notify: #GDestroyNotify for @handler
233 * A convenience function for g_unix_signal_source_new(), which
234 * attaches to the default #GMainContext. You can remove the watch
235 * using g_source_remove().
237 * Returns: An ID (greater than 0) for the event source
242 g_unix_signal_add_watch_full (int signum,
246 GDestroyNotify notify)
251 source = g_unix_signal_source_new (signum);
253 if (priority != G_PRIORITY_DEFAULT)
254 g_source_set_priority (source, priority);
256 g_source_set_callback (source, handler, user_data, notify);
257 id = g_source_attach (source, NULL);
258 g_source_unref (source);