Revert unintential IAPI break for g_key_file_load_from_data()
[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 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.
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                              gint     saved_errno)
55 {
56   g_set_error_literal (error,
57                        G_UNIX_ERROR,
58                        0,
59                        g_strerror (saved_errno));
60   errno = saved_errno;
61   return FALSE;
62 }
63
64 /**
65  * g_unix_open_pipe:
66  * @fds: Array of two integers
67  * @flags: Bitfield of file descriptor flags, see "man 2 fcntl"
68  * @error: a #GError
69  *
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  * <literal>FD_CLOEXEC</literal>.  If for example you want to configure
74  * <literal>O_NONBLOCK</literal>, that must still be done separately with
75  * fcntl().
76  *
77  * <note>This function does *not* take <literal>O_CLOEXEC</literal>, it takes
78  * <literal>FD_CLOEXEC</literal> as if for fcntl(); these are
79  * different on Linux/glibc.</note>
80  *
81  * Returns: %TRUE on success, %FALSE if not (and errno will be set).
82  *
83  * Since: 2.30
84  */
85 gboolean
86 g_unix_open_pipe (int     *fds,
87                   int      flags,
88                   GError **error)
89 {
90   int ecode;
91
92   /* We only support FD_CLOEXEC */
93   g_return_val_if_fail ((flags & (FD_CLOEXEC)) == flags, FALSE);
94
95 #ifdef HAVE_PIPE2
96   {
97     int pipe2_flags = 0;
98     if (flags & FD_CLOEXEC)
99       pipe2_flags |= O_CLOEXEC;
100     /* Atomic */
101     ecode = pipe2 (fds, pipe2_flags);
102     if (ecode == -1 && errno != ENOSYS)
103       return g_unix_set_error_from_errno (error, errno);
104     else if (ecode == 0)
105       return TRUE;
106     /* Fall through on -ENOSYS, we must be running on an old kernel */
107   }
108 #endif
109   ecode = pipe (fds);
110   if (ecode == -1)
111     return g_unix_set_error_from_errno (error, errno);
112   ecode = fcntl (fds[0], flags);
113   if (ecode == -1)
114     {
115       int saved_errno = errno;
116       close (fds[0]);
117       close (fds[1]);
118       return g_unix_set_error_from_errno (error, saved_errno);
119     }
120   ecode = fcntl (fds[1], flags);
121   if (ecode == -1)
122     {
123       int saved_errno = errno;
124       close (fds[0]);
125       close (fds[1]);
126       return g_unix_set_error_from_errno (error, saved_errno);
127     }
128   return TRUE;
129 }
130
131 /**
132  * g_unix_set_fd_nonblocking:
133  * @fd: A file descriptor
134  * @nonblock: If %TRUE, set the descriptor to be non-blocking
135  * @error: a #GError
136  *
137  * Control the non-blocking state of the given file descriptor,
138  * according to @nonblock.  On most systems this uses <literal>O_NONBLOCK</literal>, but
139  * on some older ones may use <literal>O_NDELAY</literal>.
140  *
141  * Returns: %TRUE if successful
142  *
143  * Since: 2.30
144  */
145 gboolean
146 g_unix_set_fd_nonblocking (gint       fd,
147                            gboolean   nonblock,
148                            GError   **error)
149 {
150 #ifdef F_GETFL
151   glong fcntl_flags;
152   fcntl_flags = fcntl (fd, F_GETFL);
153
154   if (fcntl_flags == -1)
155     return g_unix_set_error_from_errno (error, errno);
156
157   if (nonblock)
158     {
159 #ifdef O_NONBLOCK
160       fcntl_flags |= O_NONBLOCK;
161 #else
162       fcntl_flags |= O_NDELAY;
163 #endif
164     }
165   else
166     {
167 #ifdef O_NONBLOCK
168       fcntl_flags &= ~O_NONBLOCK;
169 #else
170       fcntl_flags &= ~O_NDELAY;
171 #endif
172     }
173
174   if (fcntl (fd, F_SETFL, fcntl_flags) == -1)
175     return g_unix_set_error_from_errno (error, errno);
176   return TRUE;
177 #else
178   return g_unix_set_error_from_errno (error, EINVAL);
179 #endif
180 }
181
182
183 /**
184  * g_unix_signal_source_new:
185  * @signum: A signal number
186  *
187  * Create a #GSource that will be dispatched upon delivery of the UNIX
188  * signal @signum.  Currently only <literal>SIGHUP</literal>,
189  * <literal>SIGINT</literal>, and <literal>SIGTERM</literal> can
190  * be monitored.  Note that unlike the UNIX default, all sources which
191  * have created a watch will be dispatched, regardless of which
192  * underlying thread invoked g_unix_signal_source_new().
193  *
194  * For example, an effective use of this function is to handle <literal>SIGTERM</literal>
195  * cleanly; flushing any outstanding files, and then calling
196  * g_main_loop_quit ().  It is not safe to do any of this a regular
197  * UNIX signal handler; your handler may be invoked while malloc() or
198  * another library function is running, causing reentrancy if you
199  * attempt to use it from the handler.  None of the GLib/GObject API
200  * is safe against this kind of reentrancy.
201  *
202  * The interaction of this source when combined with native UNIX
203  * functions like sigprocmask() is not defined.
204  *
205  * The source will not initially be associated with any #GMainContext
206  * and must be added to one with g_source_attach() before it will be
207  * executed.
208  *
209  * Returns: A newly created #GSource
210  *
211  * Since: 2.30
212  */
213 GSource *
214 g_unix_signal_source_new (int signum)
215 {
216   g_return_val_if_fail (signum == SIGHUP || signum == SIGINT || signum == SIGTERM, NULL);
217
218   return _g_main_create_unix_signal_watch (signum);
219 }
220
221 /**
222  * g_unix_signal_add_full:
223  * @priority: the priority of the signal source. Typically this will be in
224  *            the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
225  * @signum: Signal number
226  * @handler: Callback
227  * @user_data: Data for @handler
228  * @notify: #GDestroyNotify for @handler
229  *
230  * A convenience function for g_unix_signal_source_new(), which
231  * attaches to the default #GMainContext.  You can remove the watch
232  * using g_source_remove().
233  *
234  * Returns: An ID (greater than 0) for the event source
235  *
236  * Since: 2.30
237  */
238 guint
239 g_unix_signal_add_full (int            priority,
240                         int            signum,
241                         GSourceFunc    handler,
242                         gpointer       user_data,
243                         GDestroyNotify notify)
244 {
245   guint id;
246   GSource *source;
247
248   source = g_unix_signal_source_new (signum);
249
250   if (priority != G_PRIORITY_DEFAULT)
251     g_source_set_priority (source, priority);
252
253   g_source_set_callback (source, handler, user_data, notify);
254   id = g_source_attach (source, NULL);
255   g_source_unref (source);
256
257   return id;
258 }
259
260 /**
261  * g_unix_signal_add:
262  * @signum: Signal number
263  * @handler: Callback
264  * @user_data: Data for @handler
265  *
266  * A convenience function for g_unix_signal_source_new(), which
267  * attaches to the default #GMainContext.  You can remove the watch
268  * using g_source_remove().
269  *
270  * Returns: An ID (greater than 0) for the event source
271  *
272  * Since: 2.30
273  */
274 guint
275 g_unix_signal_add (int         signum,
276                    GSourceFunc handler,
277                    gpointer    user_data)
278 {
279   return g_unix_signal_add_full (G_PRIORITY_DEFAULT, signum, handler, user_data, NULL);
280 }