[glib/wip/steal-fd] Add g_steal_fd() to API
- From: Simon McVittie <smcv src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib/wip/steal-fd] Add g_steal_fd() to API
- Date: Thu, 25 Feb 2021 12:26:07 +0000 (UTC)
commit 31038ed7e41dceba8310842b1a7ac7867845b242
Author: Simon McVittie <smcv collabora com>
Date: Thu Feb 25 10:33:09 2021 +0000
Add g_steal_fd() to API
This is basically glnx_steal_fd() from libglnx. We already had two
private implementations of it in GLib.
Signed-off-by: Simon McVittie <smcv collabora com>
docs/reference/glib/glib-sections.txt | 3 +++
glib/gfileutils.c | 12 ++----------
glib/gmain.c | 14 ++++++++++++++
glib/gmain.h | 8 ++++++++
glib/gspawn.c | 16 +++-------------
5 files changed, 30 insertions(+), 23 deletions(-)
---
diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index 75994e889..c9697b45d 100644
--- a/docs/reference/glib/glib-sections.txt
+++ b/docs/reference/glib/glib-sections.txt
@@ -924,6 +924,9 @@ g_source_remove_by_user_data
GClearHandleFunc
g_clear_handle_id
+<SUBSECTION>
+g_steal_fd
+
<SUBSECTION Private>
GLIB_HAVE_ALLOCA_H
alloca
diff --git a/glib/gfileutils.c b/glib/gfileutils.c
index a4e90265e..e0a2a65d0 100644
--- a/glib/gfileutils.c
+++ b/glib/gfileutils.c
@@ -1208,14 +1208,6 @@ write_to_file (const gchar *contents,
return TRUE;
}
-static inline int
-steal_fd (int *fd_ptr)
-{
- int fd = *fd_ptr;
- *fd_ptr = -1;
- return fd;
-}
-
/**
* g_file_set_contents:
* @filename: (type filename): name of a file to write @contents to, in the GLib file name
@@ -1369,7 +1361,7 @@ g_file_set_contents_full (const gchar *filename,
}
do_fsync = fd_should_be_fsynced (fd, filename, flags);
- if (!write_to_file (contents, length, steal_fd (&fd), tmp_filename, do_fsync, error))
+ if (!write_to_file (contents, length, g_steal_fd (&fd), tmp_filename, do_fsync, error))
{
g_unlink (tmp_filename);
retval = FALSE;
@@ -1479,7 +1471,7 @@ consistent_out:
}
do_fsync = fd_should_be_fsynced (direct_fd, filename, flags);
- if (!write_to_file (contents, length, steal_fd (&direct_fd), filename,
+ if (!write_to_file (contents, length, g_steal_fd (&direct_fd), filename,
do_fsync, error))
return FALSE;
}
diff --git a/glib/gmain.c b/glib/gmain.c
index 15581ee7a..2a51ee516 100644
--- a/glib/gmain.c
+++ b/glib/gmain.c
@@ -6122,3 +6122,17 @@ g_get_worker_context (void)
return glib_worker_context;
}
+
+/**
+ * g_steal_fd:
+ * @fd_ptr: (not nullable): A pointer to a file descriptor
+ *
+ * Sets @fd_ptr to -1, returning the value that was there before.
+ *
+ * Conceptually, this transfers the ownership of the file descriptor
+ * from the referenced variable to the caller of the function (i.e.
+ * "steals" the reference). This is very similar to g_steal_pointer(),
+ * but for file descriptors.
+ *
+ * Since: 2.70
+ */
diff --git a/glib/gmain.h b/glib/gmain.h
index 5c0e524cc..5566df6f5 100644
--- a/glib/gmain.h
+++ b/glib/gmain.h
@@ -781,6 +781,14 @@ void g_main_context_invoke (GMainContext *context,
GSourceFunc function,
gpointer data);
+GLIB_AVAILABLE_STATIC_INLINE_IN_2_70
+static inline int g_steal_fd (int *fd_ptr)
+{
+ int fd = *fd_ptr;
+ *fd_ptr = -1;
+ return fd;
+}
+
/* Hook for GClosure / GSource integration. Don't touch */
GLIB_VAR GSourceFuncs g_timeout_funcs;
GLIB_VAR GSourceFuncs g_child_watch_funcs;
diff --git a/glib/gspawn.c b/glib/gspawn.c
index 95f5b868e..1acf24058 100644
--- a/glib/gspawn.c
+++ b/glib/gspawn.c
@@ -249,16 +249,6 @@ g_spawn_async (const gchar *working_directory,
error);
}
-/* This function is called between fork() and exec() and hence must be
- * async-signal-safe (see signal-safety(7)). */
-static gint
-steal_fd (gint *fd)
-{
- gint fd_out = *fd;
- *fd = -1;
- return fd_out;
-}
-
/* Avoids a danger in threaded situations (calling close()
* on a file descriptor twice, and another thread has
* re-opened it since the first close)
@@ -2436,13 +2426,13 @@ success:
close_and_invalidate (&stderr_pipe[1]);
if (stdin_pipe_out != NULL)
- *stdin_pipe_out = steal_fd (&stdin_pipe[1]);
+ *stdin_pipe_out = g_steal_fd (&stdin_pipe[1]);
if (stdout_pipe_out != NULL)
- *stdout_pipe_out = steal_fd (&stdout_pipe[0]);
+ *stdout_pipe_out = g_steal_fd (&stdout_pipe[0]);
if (stderr_pipe_out != NULL)
- *stderr_pipe_out = steal_fd (&stderr_pipe[0]);
+ *stderr_pipe_out = g_steal_fd (&stderr_pipe[0]);
return TRUE;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]