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