gstinfo,ptpclock,libcheck: Use GetCurrentProcessId() instead of getpid() on Windows
authorSeungha Yang <seungha@centricular.com>
Sun, 6 Feb 2022 13:54:42 +0000 (22:54 +0900)
committerGStreamer Marge Bot <gitlab-merge-bot@gstreamer-foundation.org>
Wed, 16 Feb 2022 17:03:29 +0000 (17:03 +0000)
getpid() shouldn't be used in case of UWP. Use GetCurrentProcessId()
instead which provides exactly the same functionality and can be
used with UWP as well.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/1006>

subprojects/gst-plugins-base/gst-libs/gst/pbutils/missing-plugins.c
subprojects/gstreamer/gst/gstinfo.c
subprojects/gstreamer/libs/gst/check/libcheck/libcompat/libcompat.h
subprojects/gstreamer/libs/gst/net/gstptpclock.c

index 8c6f222..4b0f687 100644 (file)
@@ -52,9 +52,6 @@
 #ifdef HAVE_UNISTD_H
 # include <unistd.h>            /* getpid on UNIX */
 #endif
-#ifdef HAVE_PROCESS_H
-# include <process.h>           /* getpid on win32 */
-#endif
 
 #include "gst/gst-i18n-plugin.h"
 
 
 #include <string.h>
 
+#ifdef G_OS_WIN32
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+#include <processthreadsapi.h>
+#endif
+
 #ifndef GST_DISABLE_GST_DEBUG
 #define GST_CAT_DEFAULT gst_pb_utils_missing_plugins_ensure_debug_category()
 
@@ -84,6 +87,22 @@ gst_pb_utils_missing_plugins_ensure_debug_category (void)
 }
 #endif /* GST_DISABLE_GST_DEBUG */
 
+/* use glib's abstraction once it's landed
+ * https://gitlab.gnome.org/GNOME/glib/-/merge_requests/2475 */
+#ifdef G_OS_WIN32
+static inline DWORD
+_gst_getpid (void)
+{
+  return GetCurrentProcessId ();
+}
+#else
+static inline pid_t
+_gst_getpid (void)
+{
+  return getpid ();
+}
+#endif
+
 #define GST_DETAIL_STRING_MARKER "gstreamer"
 
 typedef enum
@@ -450,7 +469,7 @@ gst_missing_plugin_message_get_installer_detail (GstMessage * msg)
   if (progname) {
     g_string_append_printf (str, "%s|", progname);
   } else {
-    g_string_append_printf (str, "pid/%lu|", (gulong) getpid ());
+    g_string_append_printf (str, "pid/%lu|", (gulong) _gst_getpid ());
   }
 
   desc = gst_missing_plugin_message_get_description (msg);
@@ -642,7 +661,7 @@ gst_installer_detail_new (gchar * description, const gchar * type,
   if (progname) {
     g_string_append_printf (s, "%s|", progname);
   } else {
-    g_string_append_printf (s, "pid/%lu|", (gulong) getpid ());
+    g_string_append_printf (s, "pid/%lu|", (gulong) _gst_getpid ());
   }
 
   if (description) {
index 8360725..248d49d 100644 (file)
 #include <stdio.h>              /* fprintf */
 #include <glib/gstdio.h>
 #include <errno.h>
-#ifdef HAVE_UNISTD_H
-#  include <unistd.h>           /* getpid on UNIX */
-#endif
-#ifdef HAVE_PROCESS_H
-#  include <process.h>          /* getpid on win32 */
-#endif
 #include <string.h>             /* G_VA_COPY */
-#ifdef G_OS_WIN32
-#  define WIN32_LEAN_AND_MEAN   /* prevents from including too many things */
-#  include <windows.h>          /* GetStdHandle, windows console */
-#endif
 
 #include "gst_private.h"
 #include "gstutils.h"
@@ -132,6 +122,34 @@ static char *gst_info_printf_pointer_extension_func (const char *format,
 #include <glib/gprintf.h>
 #endif /* !GST_DISABLE_GST_DEBUG */
 
+#ifdef HAVE_UNISTD_H
+#  include <unistd.h>           /* getpid on UNIX */
+#endif
+
+#ifdef G_OS_WIN32
+#  define WIN32_LEAN_AND_MEAN   /* prevents from including too many things */
+#  include <windows.h>          /* GetStdHandle, windows console */
+#  include <processthreadsapi.h>        /* GetCurrentProcessId */
+/* getpid() is not allowed in case of UWP, use GetCurrentProcessId() instead
+ * which can be used on both desktop and UWP */
+#endif
+
+/* use glib's abstraction once it's landed
+ * https://gitlab.gnome.org/GNOME/glib/-/merge_requests/2475 */
+#ifdef G_OS_WIN32
+static inline DWORD
+_gst_getpid (void)
+{
+  return GetCurrentProcessId ();
+}
+#else
+static inline pid_t
+_gst_getpid (void)
+{
+  return getpid ();
+}
+#endif
+
 #ifdef HAVE_UNWIND
 /* No need for remote debugging so turn on the 'local only' optimizations in
  * libunwind */
@@ -349,7 +367,7 @@ _priv_gst_debug_file_name (const gchar * env)
   gchar *name;
 
   name = g_strdup (env);
-  name = _replace_pattern_in_gst_debug_file_name (name, "%p", getpid ());
+  name = _replace_pattern_in_gst_debug_file_name (name, "%p", _gst_getpid ());
   name = _replace_pattern_in_gst_debug_file_name (name, "%r", g_random_int ());
 
   return name;
@@ -1129,7 +1147,11 @@ gst_debug_construct_win_color (guint colorinfo)
 #else
 #define PTR_FMT "%10p"
 #endif
+#ifdef G_OS_WIN32
+#define PID_FMT "%5lu"
+#else
 #define PID_FMT "%5d"
+#endif
 #define CAT_FMT "%20s %s:%d:%s:%s"
 #define NOCOLOR_PRINT_FMT " "PID_FMT" "PTR_FMT" %s "CAT_FMT" %s\n"
 
@@ -1246,7 +1268,7 @@ gst_debug_log_get_line (GstDebugCategory * category, GstDebugLevel level,
       &elapsed);
 
   ret = g_strdup_printf ("%" GST_TIME_FORMAT NOCOLOR_PRINT_FMT,
-      GST_TIME_ARGS (elapsed), getpid (), g_thread_self (),
+      GST_TIME_ARGS (elapsed), _gst_getpid (), g_thread_self (),
       gst_debug_level_get_name (level), gst_debug_category_get_name
       (category), file, line, function, obj_str, message_str);
 
@@ -1343,7 +1365,7 @@ gst_debug_log_default (GstDebugCategory * category, GstDebugLevel level,
   _gst_debug_log_preamble (message, object, &file, &message_str, &obj,
       &elapsed);
 
-  pid = getpid ();
+  pid = _gst_getpid ();
   color_mode = gst_debug_get_color_mode ();
 
   if (color_mode != GST_DEBUG_COLOR_MODE_OFF) {
@@ -2944,7 +2966,7 @@ generate_unwind_trace (GstStackTraceFlags flags)
 #ifdef HAVE_DW
   /* Due to plugins being loaded, mapping of process might have changed,
    * so always scan it. */
-  if (dwfl_linux_proc_report (dwfl, getpid ()) != 0)
+  if (dwfl_linux_proc_report (dwfl, _gst_getpid ()) != 0)
     goto done;
 #endif
 
@@ -3278,7 +3300,6 @@ gst_ring_buffer_logger_log (GstDebugCategory * category,
     gint line, GObject * object, GstDebugMessage * message, gpointer user_data)
 {
   GstRingBufferLogger *logger = user_data;
-  gint pid;
   GThread *thread;
   GstClockTime elapsed;
   gchar *obj = NULL;
@@ -3305,14 +3326,13 @@ gst_ring_buffer_logger_log (GstDebugCategory * category,
   }
 
   elapsed = GST_CLOCK_DIFF (_priv_gst_start_time, gst_util_get_timestamp ());
-  pid = getpid ();
   thread = g_thread_self ();
 
   /* no color, all platforms */
 #define PRINT_FMT " "PID_FMT" "PTR_FMT" %s "CAT_FMT" %s\n"
   output =
       g_strdup_printf ("%" GST_TIME_FORMAT PRINT_FMT, GST_TIME_ARGS (elapsed),
-      pid, thread, gst_debug_level_get_name (level),
+      _gst_getpid (), thread, gst_debug_level_get_name (level),
       gst_debug_category_get_name (category), file, line, function, obj,
       message_str);
 #undef PRINT_FMT
index 876a0fb..081d48d 100644 (file)
 #define CK_DLL_EXP extern
 #endif
 
-#if _MSC_VER
+#ifdef _MSC_VER
 #include <WinSock2.h>           /* struct timeval, API used in gettimeofday implementation */
 #include <io.h>                 /* read, write */
-#include <process.h>            /* getpid */
 #include <BaseTsd.h>            /* for ssize_t */
+#include <processthreadsapi.h> /* GetCurrentProcessId */
+/* getpid() is not allowed in case of UWP, use GetCurrentProcessId() instead
+ * which can be used on both desktop and UWP */
+#define getpid GetCurrentProcessId
 typedef SSIZE_T ssize_t;
 #endif /* _MSC_VER */
 
@@ -114,7 +117,7 @@ CK_DLL_EXP void *rpl_malloc (size_t n);
 CK_DLL_EXP void *rpl_realloc (void *p, size_t n);
 #endif /* !HAVE_REALLOC */
 
-#if !HAVE_GETPID && HAVE__GETPID
+#if !HAVE_GETPID && HAVE__GETPID && !defined(_MSC_VER)
 #define getpid _getpid
 #endif /* !HAVE_GETPID && HAVE__GETPID */
 
index 19b90e5..eb7e5bd 100644 (file)
@@ -63,6 +63,7 @@
 #ifdef G_OS_WIN32
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
+#include <processthreadsapi.h>  /* GetCurrentProcessId */
 #endif
 #include <sys/types.h>
 
@@ -1862,7 +1863,11 @@ have_stdin_data_cb (GIOChannel * channel, GIOCondition condition,
       }
       g_mutex_lock (&ptp_lock);
       ptp_clock_id.clock_identity = GST_READ_UINT64_BE (buffer);
+#ifdef G_OS_WIN32
+      ptp_clock_id.port_number = (guint16) GetCurrentProcessId ();
+#else
       ptp_clock_id.port_number = getpid ();
+#endif
       GST_DEBUG ("Got clock id 0x%016" G_GINT64_MODIFIER "x %u",
           ptp_clock_id.clock_identity, ptp_clock_id.port_number);
       g_cond_signal (&ptp_cond);