LCOV - code coverage report
Current view: top level - glib - glib-unix.h (source / functions) Coverage Total Hit
Test: unnamed Lines: 100.0 % 15 15
Test Date: 2025-03-04 05:20:05 Functions: 100.0 % 6 6
Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* glib-unix.h - Unix specific integration
       2                 :             :  * Copyright (C) 2011 Red Hat, Inc.
       3                 :             :  * Copyright 2023 Collabora Ltd.
       4                 :             :  *
       5                 :             :  * SPDX-License-Identifier: LGPL-2.1-or-later
       6                 :             :  *
       7                 :             :  * This library is free software; you can redistribute it and/or
       8                 :             :  * modify it under the terms of the GNU Lesser General Public
       9                 :             :  * License as published by the Free Software Foundation; either
      10                 :             :  * version 2.1 of the License, or (at your option) any later version.
      11                 :             :  *
      12                 :             :  * This library is distributed in the hope that it will be useful,
      13                 :             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15                 :             :  * Lesser General Public License for more details.
      16                 :             :  *
      17                 :             :  * You should have received a copy of the GNU Lesser General Public License
      18                 :             :  * along with this library; if not, see <http://www.gnu.org/licenses/>.
      19                 :             :  */
      20                 :             : 
      21                 :             : #ifndef __G_UNIX_H__
      22                 :             : #define __G_UNIX_H__
      23                 :             : 
      24                 :             : /* We need to include the UNIX headers needed to use the APIs below,
      25                 :             :  * but we also take this opportunity to include a wide selection of
      26                 :             :  * other UNIX headers.  If one of the headers below is broken on some
      27                 :             :  * system, work around it here (or better, fix the system or tell
      28                 :             :  * people to use a better one).
      29                 :             :  */
      30                 :             : #include <unistd.h>
      31                 :             : #include <errno.h>
      32                 :             : #include <sys/wait.h>
      33                 :             : #include <stdlib.h>
      34                 :             : #include <fcntl.h>
      35                 :             : 
      36                 :             : #include <glib.h>
      37                 :             : #include <glib/gstdio.h>
      38                 :             : 
      39                 :             : #ifndef G_OS_UNIX
      40                 :             : #error "This header may only be used on UNIX"
      41                 :             : #endif
      42                 :             : 
      43                 :             : G_BEGIN_DECLS
      44                 :             : 
      45                 :             : /**
      46                 :             :  * G_UNIX_ERROR:
      47                 :             :  *
      48                 :             :  * Error domain for API in the g_unix_ namespace. Note that there is no
      49                 :             :  * exported enumeration mapping %errno. Instead, all functions ensure that
      50                 :             :  * %errno is relevant. The code for all %G_UNIX_ERROR is always 0, and the
      51                 :             :  * error message is always generated via g_strerror().
      52                 :             :  *
      53                 :             :  * It is expected that most code will not look at %errno from these APIs.
      54                 :             :  * Important cases where one would want to differentiate between errors are
      55                 :             :  * already covered by existing cross-platform GLib API, such as e.g. #GFile
      56                 :             :  * wrapping `ENOENT`. However, it is provided for completeness, at least.
      57                 :             :  */
      58                 :             : #define G_UNIX_ERROR (g_unix_error_quark())
      59                 :             : 
      60                 :             : GLIB_AVAILABLE_IN_2_30
      61                 :             : GQuark g_unix_error_quark (void);
      62                 :             : 
      63                 :             : GLIB_AVAILABLE_IN_2_30
      64                 :             : gboolean g_unix_open_pipe (gint    *fds,
      65                 :             :                            gint     flags,
      66                 :             :                            GError **error);
      67                 :             : 
      68                 :             : GLIB_AVAILABLE_IN_2_30
      69                 :             : gboolean g_unix_set_fd_nonblocking (gint       fd,
      70                 :             :                                     gboolean   nonblock,
      71                 :             :                                     GError   **error);
      72                 :             : 
      73                 :             : GLIB_AVAILABLE_IN_2_30
      74                 :             : GSource *g_unix_signal_source_new  (gint signum);
      75                 :             : 
      76                 :             : GLIB_AVAILABLE_IN_2_30
      77                 :             : guint    g_unix_signal_add_full    (gint           priority,
      78                 :             :                                     gint           signum,
      79                 :             :                                     GSourceFunc    handler,
      80                 :             :                                     gpointer       user_data,
      81                 :             :                                     GDestroyNotify notify);
      82                 :             : 
      83                 :             : GLIB_AVAILABLE_IN_2_30
      84                 :             : guint    g_unix_signal_add         (gint        signum,
      85                 :             :                                     GSourceFunc handler,
      86                 :             :                                     gpointer    user_data);
      87                 :             : 
      88                 :             : /**
      89                 :             :  * GUnixFDSourceFunc:
      90                 :             :  * @fd: the fd that triggered the event
      91                 :             :  * @condition: the IO conditions reported on @fd
      92                 :             :  * @user_data: user data passed to g_unix_fd_add()
      93                 :             :  *
      94                 :             :  * The type of functions to be called when a UNIX fd watch source
      95                 :             :  * triggers.
      96                 :             :  *
      97                 :             :  * Returns: %FALSE if the source should be removed
      98                 :             :  **/
      99                 :             : typedef gboolean (*GUnixFDSourceFunc) (gint         fd,
     100                 :             :                                        GIOCondition condition,
     101                 :             :                                        gpointer     user_data);
     102                 :             : 
     103                 :             : GLIB_AVAILABLE_IN_2_36
     104                 :             : GSource *g_unix_fd_source_new      (gint         fd,
     105                 :             :                                     GIOCondition condition);
     106                 :             : 
     107                 :             : GLIB_AVAILABLE_IN_2_36
     108                 :             : guint    g_unix_fd_add_full        (gint              priority,
     109                 :             :                                     gint              fd,
     110                 :             :                                     GIOCondition      condition,
     111                 :             :                                     GUnixFDSourceFunc function,
     112                 :             :                                     gpointer          user_data,
     113                 :             :                                     GDestroyNotify    notify);
     114                 :             : 
     115                 :             : GLIB_AVAILABLE_IN_2_36
     116                 :             : guint    g_unix_fd_add             (gint              fd,
     117                 :             :                                     GIOCondition      condition,
     118                 :             :                                     GUnixFDSourceFunc function,
     119                 :             :                                     gpointer          user_data);
     120                 :             : 
     121                 :             : GLIB_AVAILABLE_IN_2_64
     122                 :             : struct passwd *g_unix_get_passwd_entry (const gchar  *user_name,
     123                 :             :                                         GError      **error);
     124                 :             : 
     125                 :             : /**
     126                 :             :  * GUnixPipe:
     127                 :             :  * @fds: A pair of file descriptors, each negative if closed or not yet opened.
     128                 :             :  *  The file descriptor with index %G_UNIX_PIPE_END_READ is readable.
     129                 :             :  *  The file descriptor with index %G_UNIX_PIPE_END_WRITE is writable.
     130                 :             :  *
     131                 :             :  * A Unix pipe. The advantage of this type over `int[2]` is that it can
     132                 :             :  * be closed automatically when it goes out of scope, using `g_auto(GUnixPipe)`,
     133                 :             :  * on compilers that support that feature.
     134                 :             :  *
     135                 :             :  * Since: 2.80
     136                 :             :  */
     137                 :             : GLIB_AVAILABLE_TYPE_IN_2_80
     138                 :             : typedef struct {
     139                 :             :   int fds[2];
     140                 :             : } GUnixPipe;
     141                 :             : 
     142                 :             : /**
     143                 :             :  * GUnixPipeEnd:
     144                 :             :  * @G_UNIX_PIPE_END_READ: The readable file descriptor 0
     145                 :             :  * @G_UNIX_PIPE_END_WRITE: The writable file descriptor 1
     146                 :             :  *
     147                 :             :  * Mnemonic constants for the ends of a Unix pipe.
     148                 :             :  *
     149                 :             :  * Since: 2.80
     150                 :             :  */
     151                 :             : GLIB_AVAILABLE_TYPE_IN_2_80
     152                 :             : typedef enum
     153                 :             : {
     154                 :             :   G_UNIX_PIPE_END_READ = 0,
     155                 :             :   G_UNIX_PIPE_END_WRITE = 1
     156                 :             : } GUnixPipeEnd;
     157                 :             : 
     158                 :             : /**
     159                 :             :  * G_UNIX_PIPE_INIT:
     160                 :             :  *
     161                 :             :  * Initializer for a #GUnixPipe that has not yet been opened.
     162                 :             :  * Both of its file descriptors are initialized to `-1` (invalid),
     163                 :             :  * the same as if they had been closed.
     164                 :             :  *
     165                 :             :  * Since: 2.80
     166                 :             :  */
     167                 :             : #define G_UNIX_PIPE_INIT { { -1, -1 } } GLIB_AVAILABLE_MACRO_IN_2_80
     168                 :             : 
     169                 :             : /* Suppress "Not available before" warnings when declaring the
     170                 :             :  * implementations */
     171                 :             : G_GNUC_BEGIN_IGNORE_DEPRECATIONS
     172                 :             : 
     173                 :             : /**
     174                 :             :  * g_unix_pipe_open:
     175                 :             :  * @self: A pair of file descriptors
     176                 :             :  * @flags: Flags to pass to g_unix_open_pipe(), typically `O_CLOEXEC`
     177                 :             :  * @error: Used to report an error on failure
     178                 :             :  *
     179                 :             :  * Open a pipe. This is the same as g_unix_open_pipe(), but uses the
     180                 :             :  * #GUnixPipe data structure.
     181                 :             :  *
     182                 :             :  * Returns: %TRUE on success
     183                 :             :  *
     184                 :             :  * Since: 2.80
     185                 :             :  */
     186                 :             : GLIB_AVAILABLE_STATIC_INLINE_IN_2_80
     187                 :             : static inline gboolean g_unix_pipe_open (GUnixPipe  *self,
     188                 :             :                                          int         flags,
     189                 :             :                                          GError    **error);
     190                 :             : 
     191                 :             : GLIB_AVAILABLE_STATIC_INLINE_IN_2_80
     192                 :             : static inline gboolean
     193                 :        2284 : g_unix_pipe_open (GUnixPipe *self,
     194                 :             :                   int flags,
     195                 :             :                   GError **error)
     196                 :             : {
     197                 :        2284 :   return g_unix_open_pipe (self->fds, flags, error);
     198                 :             : }
     199                 :             : 
     200                 :             : /**
     201                 :             :  * g_unix_pipe_get:
     202                 :             :  * @self: A pair of file descriptors
     203                 :             :  * @end: One of the ends of the pipe
     204                 :             :  *
     205                 :             :  * Return one of the ends of the pipe. It remains owned by @self.
     206                 :             :  *
     207                 :             :  * This function is async-signal safe (see [`signal(7)`](man:signal(7)) and
     208                 :             :  * [`signal-safety(7)`](man:signal-safety(7))), making it safe to call from a
     209                 :             :  * signal handler or a #GSpawnChildSetupFunc.
     210                 :             :  *
     211                 :             :  * This function preserves the value of `errno`.
     212                 :             :  *
     213                 :             :  * Returns: a non-negative file descriptor owned by @self, which must not
     214                 :             :  *  be closed by the caller, or a negative number if the corresponding
     215                 :             :  *  end of the pipe was already closed or stolen
     216                 :             :  *
     217                 :             :  * Since: 2.80
     218                 :             :  */
     219                 :             : GLIB_AVAILABLE_STATIC_INLINE_IN_2_80
     220                 :             : static inline int g_unix_pipe_get (GUnixPipe    *self,
     221                 :             :                                    GUnixPipeEnd  end);
     222                 :             : 
     223                 :             : GLIB_AVAILABLE_STATIC_INLINE_IN_2_80
     224                 :             : static inline int
     225                 :        4448 : g_unix_pipe_get (GUnixPipe *self,
     226                 :             :                  GUnixPipeEnd end)
     227                 :             : {
     228                 :        4448 :   return self->fds[end];
     229                 :             : }
     230                 :             : 
     231                 :             : /**
     232                 :             :  * g_unix_pipe_steal:
     233                 :             :  * @self: A pair of file descriptors
     234                 :             :  * @end: One of the ends of the pipe
     235                 :             :  *
     236                 :             :  * Return one of the ends of the pipe. It becomes owned by the caller,
     237                 :             :  * and the file descriptor in the data structure is set to `-1`,
     238                 :             :  * similar to g_steal_fd().
     239                 :             :  *
     240                 :             :  * This function is async-signal safe (see [`signal(7)`](man:signal(7)) and
     241                 :             :  * [`signal-safety(7)`](man:signal-safety(7))), making it safe to call from a
     242                 :             :  * signal handler or a #GSpawnChildSetupFunc.
     243                 :             :  *
     244                 :             :  * This function preserves the value of `errno`.
     245                 :             :  *
     246                 :             :  * Returns: a non-negative file descriptor, which becomes owned by the
     247                 :             :  *  caller and must be closed by the caller if required, or a negative
     248                 :             :  *  number if the corresponding end of the pipe was already closed or stolen
     249                 :             :  *
     250                 :             :  * Since: 2.80
     251                 :             :  */
     252                 :             : GLIB_AVAILABLE_STATIC_INLINE_IN_2_80
     253                 :             : static inline int g_unix_pipe_steal (GUnixPipe    *self,
     254                 :             :                                      GUnixPipeEnd  end);
     255                 :             : 
     256                 :             : GLIB_AVAILABLE_STATIC_INLINE_IN_2_80
     257                 :             : static inline int
     258                 :        1206 : g_unix_pipe_steal (GUnixPipe *self,
     259                 :             :                    GUnixPipeEnd end)
     260                 :             : {
     261                 :        1206 :   return g_steal_fd (&self->fds[end]);
     262                 :             : }
     263                 :             : 
     264                 :             : /**
     265                 :             :  * g_unix_pipe_close:
     266                 :             :  * @self: A pair of file descriptors
     267                 :             :  * @end: One of the ends of the pipe
     268                 :             :  * @error: Optionally used to report an error on failure
     269                 :             :  *
     270                 :             :  * Close one of the ends of the pipe and set the relevant member of @fds
     271                 :             :  * to `-1` before returning, equivalent to g_clear_fd().
     272                 :             :  *
     273                 :             :  * Like g_close(), if closing the file descriptor fails, the error is
     274                 :             :  * stored in both %errno and @error. If this function succeeds,
     275                 :             :  * %errno is undefined.
     276                 :             :  *
     277                 :             :  * This function is async-signal safe if @error is %NULL and the relevant
     278                 :             :  * member of @fds is either negative or a valid open file descriptor.
     279                 :             :  * This makes it safe to call from a signal handler or a #GSpawnChildSetupFunc
     280                 :             :  * under those conditions.
     281                 :             :  * See [`signal(7)`](man:signal(7)) and
     282                 :             :  * [`signal-safety(7)`](man:signal-safety(7)) for more details.
     283                 :             :  *
     284                 :             :  * To close both file descriptors and ignore any errors, use
     285                 :             :  * g_unix_pipe_clear() instead.
     286                 :             :  *
     287                 :             :  * Returns: %TRUE on success
     288                 :             :  *
     289                 :             :  * Since: 2.80
     290                 :             :  */
     291                 :             : GLIB_AVAILABLE_STATIC_INLINE_IN_2_80
     292                 :             : static inline gboolean g_unix_pipe_close (GUnixPipe     *self,
     293                 :             :                                           GUnixPipeEnd   end,
     294                 :             :                                           GError       **error);
     295                 :             : 
     296                 :             : GLIB_AVAILABLE_STATIC_INLINE_IN_2_80
     297                 :             : static inline gboolean
     298                 :        8936 : g_unix_pipe_close (GUnixPipe *self,
     299                 :             :                    GUnixPipeEnd end,
     300                 :             :                    GError **error)
     301                 :             : {
     302                 :        8936 :   return g_clear_fd (&self->fds[end], error);
     303                 :             : }
     304                 :             : 
     305                 :             : /**
     306                 :             :  * g_unix_pipe_clear:
     307                 :             :  * @self: a #GUnixPipe
     308                 :             :  *
     309                 :             :  * Close both ends of the pipe, unless they have already been closed or
     310                 :             :  * stolen. Any errors are ignored: use g_unix_pipe_close() or g_clear_fd()
     311                 :             :  * if error-handling is required.
     312                 :             :  *
     313                 :             :  * This function is async-signal safe if @error is %NULL and each member
     314                 :             :  * of @fds are either negative or a valid open file descriptor.
     315                 :             :  * As a result, it is safe to call this function or use `g_auto(GUnixPipe)`
     316                 :             :  * (on compilers that support it) in a signal handler or a
     317                 :             :  * #GSpawnChildSetupFunc, as long as those conditions are ensured to be true.
     318                 :             :  * See [`signal(7)`](man:signal(7)) and
     319                 :             :  * [`signal-safety(7)`](man:signal-safety(7)) for more details.
     320                 :             :  *
     321                 :             :  * This function preserves the value of `errno`.
     322                 :             :  *
     323                 :             :  * Since: 2.80
     324                 :             :  */
     325                 :             : GLIB_AVAILABLE_STATIC_INLINE_IN_2_80
     326                 :             : static inline void g_unix_pipe_clear (GUnixPipe *self);
     327                 :             : 
     328                 :             : GLIB_AVAILABLE_STATIC_INLINE_IN_2_80
     329                 :             : static inline void
     330                 :          60 : g_unix_pipe_clear (GUnixPipe *self)
     331                 :             : {
     332                 :             :   /* Don't overwrite thread-local errno if closing the fd fails */
     333                 :          60 :   int errsv = errno;
     334                 :             : 
     335                 :          60 :   if (!g_unix_pipe_close (self, G_UNIX_PIPE_END_READ, NULL))
     336                 :             :     {
     337                 :             :       /* ignore */
     338                 :             :     }
     339                 :             : 
     340                 :          60 :   if (!g_unix_pipe_close (self, G_UNIX_PIPE_END_WRITE, NULL))
     341                 :             :     {
     342                 :             :       /* ignore */
     343                 :             :     }
     344                 :             : 
     345                 :          60 :   errno = errsv;
     346                 :          60 : }
     347                 :             : 
     348                 :           4 : G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC (GUnixPipe, g_unix_pipe_clear)
     349                 :             : 
     350                 :             : GLIB_AVAILABLE_IN_2_80
     351                 :             : int g_closefrom (int lowfd);
     352                 :             : 
     353                 :             : GLIB_AVAILABLE_IN_2_80
     354                 :             : int g_fdwalk_set_cloexec (int lowfd);
     355                 :             : 
     356                 :             : G_GNUC_END_IGNORE_DEPRECATIONS
     357                 :             : 
     358                 :             : G_END_DECLS
     359                 :             : 
     360                 :             : #endif  /* __G_UNIX_H__ */
        

Generated by: LCOV version 2.0-1