Add G_STATIC_ASSERT_EXPR macro
[platform/upstream/glib.git] / glib / glib-unix.c
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  * 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.
10  *
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.
15  *
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.
20  *
21  * Authors: Colin Walters <walters@verbum.org>
22  */
23
24 #include "config.h"
25
26 #include "glib-unix.h"
27 #include "gmain-internal.h"
28
29 #include <string.h>
30
31 /**
32  * SECTION:gunix
33  * @title: UNIX-specific utilities and integration
34  * @short_description: pipes, signal handling
35  * @include: glib-unix.h
36  *
37  * Most of GLib is intended to be portable; in constrast, this set of
38  * functions is designed for programs which explicitly target UNIX, or
39  * are using it to build higher level abstractions which would be
40  * conditionally compiled if the platform matches G_OS_UNIX.
41  *
42  * To use these functions, you must explicitly include the
43  * "glib-unix.h" header.
44  */
45
46 GQuark
47 g_unix_error_quark (void)
48 {
49   return g_quark_from_static_string ("g-unix-error-quark");
50 }
51
52 static gboolean
53 g_unix_set_error_from_errno (GError **error)
54 {
55   int saved_errno = errno;
56   g_set_error_literal (error,
57                        G_UNIX_ERROR,
58                        0,
59                        g_strerror (errno));
60   errno = saved_errno;
61   return FALSE;
62 }
63
64 static gboolean
65 g_unix_set_error_from_errno_saved (GError **error,
66                                     int      saved_errno)
67 {
68   g_set_error_literal (error,
69                        G_UNIX_ERROR,
70                        0,
71                        g_strerror (saved_errno));
72   errno = saved_errno;
73   return FALSE;
74 }
75
76 /**
77  * g_unix_open_pipe:
78  * @fds: Array of two integers
79  * @flags: Bitfield of file descriptor flags, see "man 2 fcntl"
80  * @error: a #GError
81  *
82  * Similar to the UNIX pipe() call, but on modern systems like Linux
83  * uses the pipe2() system call, which atomically creates a pipe with
84  * the configured flags.  The only supported flag currently is
85  * %FD_CLOEXEC.  If for example you want to configure %O_NONBLOCK, that
86  * must still be done separately with fcntl().
87  *
88  * <note>This function does *not* take %O_CLOEXEC, it takes %FD_CLOEXEC as if
89  * for fcntl(); these are different on Linux/glibc.</note>
90  *
91  * Returns: %TRUE on success, %FALSE if not (and errno will be set).
92  *
93  * Since: 2.30
94  */
95 gboolean
96 g_unix_open_pipe (int     *fds,
97                   int      flags,
98                   GError **error)
99 {
100   int ecode;
101
102   /* We only support FD_CLOEXEC */
103   g_return_val_if_fail ((flags & (FD_CLOEXEC)) == flags, FALSE);
104
105 #ifdef HAVE_PIPE2
106   {
107     int pipe2_flags = 0;
108     if (flags & FD_CLOEXEC)
109       pipe2_flags |= O_CLOEXEC;
110     /* Atomic */
111     ecode = pipe2 (fds, pipe2_flags);
112     if (ecode == -1 && errno != ENOSYS)
113       return g_unix_set_error_from_errno (error);
114     else if (ecode == 0)
115       return TRUE;
116     /* Fall through on -ENOSYS, we must be running on an old kernel */
117   }
118 #endif
119   ecode = pipe (fds);
120   if (ecode == -1)
121     return g_unix_set_error_from_errno (error);
122   ecode = fcntl (fds[0], flags);
123   if (ecode == -1)
124     {
125       int saved_errno = errno;
126       close (fds[0]);
127       return g_unix_set_error_from_errno_saved (error, saved_errno);
128     }
129   ecode = fcntl (fds[0], flags);
130   if (ecode == -1)
131     {
132       int saved_errno = errno;
133       close (fds[0]);
134       close (fds[1]);
135       return g_unix_set_error_from_errno_saved (error, saved_errno);
136     }
137   return TRUE;
138 }
139
140 /**
141  * g_unix_set_fd_nonblocking:
142  * @fd: A file descriptor
143  * @nonblock: If %TRUE, set the descriptor to be non-blocking
144  * @error: a #GError
145  *
146  * Control the non-blocking state of the given file descriptor,
147  * according to @nonblock.  On most systems this uses %O_NONBLOCK, but
148  * on some older ones may use %O_NDELAY.
149  *
150  * Returns: %TRUE if successful
151  */
152 gboolean
153 g_unix_set_fd_nonblocking (gint       fd, 
154                            gboolean   nonblock,
155                            GError   **error)
156 {
157 #ifdef F_GETFL
158   glong fcntl_flags;
159   fcntl_flags = fcntl (fd, F_GETFL);
160
161   if (fcntl_flags == -1)
162     return g_unix_set_error_from_errno (error);
163
164   if (nonblock)
165     {
166 #ifdef O_NONBLOCK
167       fcntl_flags |= O_NONBLOCK;
168 #else
169       fcntl_flags |= O_NDELAY;
170 #endif
171     }
172   else
173     {
174 #ifdef O_NONBLOCK
175       fcntl_flags &= ~O_NONBLOCK;
176 #else
177       fcntl_flags &= ~O_NDELAY;
178 #endif
179     }
180
181   if (fcntl (fd, F_SETFL, fcntl_flags) == -1)
182     return g_unix_set_error_from_errno (error);
183   return TRUE;
184 #else
185   return g_unix_set_error_from_errno_saved (error, EINVAL);
186 #endif
187 }
188
189
190 /**
191  * g_unix_signal_source_new:
192  * @signum: A signal number
193  *
194  * Create a #GSource that will be dispatched upon delivery of the UNIX
195  * signal @signum.  Currently only %SIGHUP, %SIGINT, and %SIGTERM can
196  * be monitored.  Note that unlike the UNIX default, all sources which
197  * have created a watch will be dispatched, regardless of which
198  * underlying thread invoked g_unix_signal_create_watch().
199  * 
200  * For example, an effective use of this function is to handle SIGTERM
201  * cleanly; flushing any outstanding files, and then calling
202  * g_main_loop_quit ().  It is not safe to do any of this a regular
203  * UNIX signal handler; your handler may be invoked while malloc() or
204  * another library function is running, causing reentrancy if you
205  * attempt to use it from the handler.  None of the GLib/GObject API
206  * is safe against this kind of reentrancy.
207  *
208  * The interaction of this source when combined with native UNIX
209  * functions like sigprocmask() is not defined.
210  *
211  * <note>For reliable behavior, if your program links to gthread
212  * (either directly or indirectly via GObject, GIO, or a higher level
213  * library), you should ensure g_thread_init() is called before using
214  * this function.  For example, if your program uses GObject, call
215  * g_type_init().</note>
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 GSource *
224 g_unix_signal_source_new (int signum)
225 {
226   g_return_val_if_fail (signum == SIGHUP || signum == SIGINT || signum == SIGTERM, NULL);
227
228   return _g_main_create_unix_signal_watch (signum);
229 }
230
231 /**
232  * g_unix_signal_add_watch_full:
233  * @signum: Signal number
234  * @priority: the priority of the signal source. Typically this will be in
235  *            the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
236  * @handler: Callback
237  * @user_data: Data for @handler
238  * @notify: #GDestroyNotify for @handler
239  *
240  * A convenience function for g_unix_signal_source_new(), which
241  * attaches to the default #GMainContext.  You can remove the watch
242  * using g_source_remove().
243  *
244  * Returns: An ID (greater than 0) for the event source
245  */
246 guint
247 g_unix_signal_add_watch_full (int            signum,
248                               int            priority,
249                               GSourceFunc    handler,
250                               gpointer       user_data,
251                               GDestroyNotify notify)
252 {
253   guint id;
254   GSource *source;
255
256   source = g_unix_signal_source_new (signum);
257
258   if (priority != G_PRIORITY_DEFAULT)
259     g_source_set_priority (source, priority);
260
261   g_source_set_callback (source, handler, user_data, notify);
262   id = g_source_attach (source, NULL);
263   g_source_unref (source);
264
265   return id;
266 }