From 04f3a2bd22a556b643a13db2417e3b9ddb026577 Mon Sep 17 00:00:00 2001 From: Seungha Yang Date: Sun, 6 Feb 2022 22:54:42 +0900 Subject: [PATCH] gstinfo,ptpclock,libcheck: Use GetCurrentProcessId() instead of getpid() on Windows 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: --- .../gst-libs/gst/pbutils/missing-plugins.c | 29 ++++++++-- subprojects/gstreamer/gst/gstinfo.c | 54 +++++++++++++------ .../gst/check/libcheck/libcompat/libcompat.h | 9 ++-- .../gstreamer/libs/gst/net/gstptpclock.c | 5 ++ 4 files changed, 72 insertions(+), 25 deletions(-) diff --git a/subprojects/gst-plugins-base/gst-libs/gst/pbutils/missing-plugins.c b/subprojects/gst-plugins-base/gst-libs/gst/pbutils/missing-plugins.c index 8c6f222803..4b0f687ce9 100644 --- a/subprojects/gst-plugins-base/gst-libs/gst/pbutils/missing-plugins.c +++ b/subprojects/gst-plugins-base/gst-libs/gst/pbutils/missing-plugins.c @@ -52,9 +52,6 @@ #ifdef HAVE_UNISTD_H # include /* getpid on UNIX */ #endif -#ifdef HAVE_PROCESS_H -# include /* getpid on win32 */ -#endif #include "gst/gst-i18n-plugin.h" @@ -63,6 +60,12 @@ #include +#ifdef G_OS_WIN32 +#define WIN32_LEAN_AND_MEAN +#include +#include +#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) { diff --git a/subprojects/gstreamer/gst/gstinfo.c b/subprojects/gstreamer/gst/gstinfo.c index 8360725961..248d49d5bf 100644 --- a/subprojects/gstreamer/gst/gstinfo.c +++ b/subprojects/gstreamer/gst/gstinfo.c @@ -97,17 +97,7 @@ #include /* fprintf */ #include #include -#ifdef HAVE_UNISTD_H -# include /* getpid on UNIX */ -#endif -#ifdef HAVE_PROCESS_H -# include /* getpid on win32 */ -#endif #include /* G_VA_COPY */ -#ifdef G_OS_WIN32 -# define WIN32_LEAN_AND_MEAN /* prevents from including too many things */ -# include /* 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 #endif /* !GST_DISABLE_GST_DEBUG */ +#ifdef HAVE_UNISTD_H +# include /* getpid on UNIX */ +#endif + +#ifdef G_OS_WIN32 +# define WIN32_LEAN_AND_MEAN /* prevents from including too many things */ +# include /* GetStdHandle, windows console */ +# include /* 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 diff --git a/subprojects/gstreamer/libs/gst/check/libcheck/libcompat/libcompat.h b/subprojects/gstreamer/libs/gst/check/libcheck/libcompat/libcompat.h index 876a0fb4f3..081d48db0b 100644 --- a/subprojects/gstreamer/libs/gst/check/libcheck/libcompat/libcompat.h +++ b/subprojects/gstreamer/libs/gst/check/libcheck/libcompat/libcompat.h @@ -54,11 +54,14 @@ #define CK_DLL_EXP extern #endif -#if _MSC_VER +#ifdef _MSC_VER #include /* struct timeval, API used in gettimeofday implementation */ #include /* read, write */ -#include /* getpid */ #include /* for ssize_t */ +#include /* 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 */ diff --git a/subprojects/gstreamer/libs/gst/net/gstptpclock.c b/subprojects/gstreamer/libs/gst/net/gstptpclock.c index 19b90e5969..eb7e5bd70a 100644 --- a/subprojects/gstreamer/libs/gst/net/gstptpclock.c +++ b/subprojects/gstreamer/libs/gst/net/gstptpclock.c @@ -63,6 +63,7 @@ #ifdef G_OS_WIN32 #define WIN32_LEAN_AND_MEAN #include +#include /* GetCurrentProcessId */ #endif #include @@ -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);