From 4c961bfb6eba485d5022c0a9a603f6f8f2134b8a Mon Sep 17 00:00:00 2001
From: Ludwig Ortmann <ludwig.ortmann@fu-berlin.de>
Date: Tue, 13 May 2014 16:53:36 +0200
Subject: [PATCH] native: refactor getpid calls

---
 boards/native/board_config.c         |  7 +++----
 boards/native/drivers/native-uart0.c |  2 +-
 cpu/native/crash.c                   |  2 +-
 cpu/native/include/native_internal.h |  1 +
 cpu/native/net/tap.c                 |  4 ++--
 cpu/native/startup.c                 | 19 +++++++++----------
 6 files changed, 17 insertions(+), 18 deletions(-)

diff --git a/boards/native/board_config.c b/boards/native/board_config.c
index 68d3e9bb42..512d4eb172 100644
--- a/boards/native/board_config.c
+++ b/boards/native/board_config.c
@@ -25,6 +25,7 @@
 #include "nativenet.h"
 #include "nativenet_internal.h"
 #endif
+#include "native_internal.h"
 
 #define ENABLE_DEBUG (0)
 #include "debug.h"
@@ -36,12 +37,10 @@ void config_load(void)
 {
     DEBUG("config_load()\n");
 
-    int pid = getpid();
-
-    sysconfig.id = pid;
+    sysconfig.id = _native_pid;
 
 #ifdef MODULE_NATIVENET
-    _native_net_addr = pid;
+    _native_net_addr = _native_pid;
 #endif
 
     return;
diff --git a/boards/native/drivers/native-uart0.c b/boards/native/drivers/native-uart0.c
index 1f2fb3c408..0711c15b8e 100644
--- a/boards/native/drivers/native-uart0.c
+++ b/boards/native/drivers/native-uart0.c
@@ -148,7 +148,7 @@ int init_unix_socket()
     }
 
     sa.sun_family = AF_UNIX;
-    snprintf(sa.sun_path, sizeof(sa.sun_path), "/tmp/riot.tty.%d", getpid());
+    snprintf(sa.sun_path, sizeof(sa.sun_path), "/tmp/riot.tty.%d", _native_pid);
     unlink(sa.sun_path); /* remove stale socket */
     if (bind(s, (struct sockaddr *)&sa, SUN_LEN(&sa)) == -1) {
         err(EXIT_FAILURE, "init_unix_socket: bind");
diff --git a/cpu/native/crash.c b/cpu/native/crash.c
index b6f0532352..08c38621b5 100644
--- a/cpu/native/crash.c
+++ b/cpu/native/crash.c
@@ -53,7 +53,7 @@ NORETURN void core_panic(int crash_code, const char *message)
 #if DEVELHELP
     /* since we're atop an Unix-like platform,
        just use the (developer-)friendly core-dump feature */
-    kill(getpid(), SIGTRAP);
+    kill(_native_pid, SIGTRAP);
 #else
     (void) reboot(RB_AUTOBOOT);
 #endif
diff --git a/cpu/native/include/native_internal.h b/cpu/native/include/native_internal.h
index cf9bf04241..5c4ae5caf9 100644
--- a/cpu/native/include/native_internal.h
+++ b/cpu/native/include/native_internal.h
@@ -77,6 +77,7 @@ extern ucontext_t *_native_cur_ctx, *_native_isr_ctx;
 
 extern const char *_progname;
 extern char **_native_argv;
+extern pid_t _native_pid;
 
 #ifdef MODULE_UART0
 #include <sys/select.h>
diff --git a/cpu/native/net/tap.c b/cpu/native/net/tap.c
index 52ed19da21..185a975af1 100644
--- a/cpu/native/net/tap.c
+++ b/cpu/native/net/tap.c
@@ -151,7 +151,7 @@ void _native_handle_tap_input(void)
 #ifdef __MACH__
 void sigio_child()
 {
-    pid_t parent = getpid();
+    pid_t parent = _native_pid;
 
     if ((sigio_child_pid = fork()) == -1) {
         err(EXIT_FAILURE, "sigio_child: fork");
@@ -315,7 +315,7 @@ int tap_init(char *name)
     sigio_child();
 #else
     /* configure fds to send signals on io */
-    if (fcntl(_native_tap_fd, F_SETOWN, getpid()) == -1) {
+    if (fcntl(_native_tap_fd, F_SETOWN, _native_pid) == -1) {
         err(EXIT_FAILURE, "tap_init(): fcntl(F_SETOWN)");
     }
 
diff --git a/cpu/native/startup.c b/cpu/native/startup.c
index 432760dc23..a45e3688ac 100644
--- a/cpu/native/startup.c
+++ b/cpu/native/startup.c
@@ -42,6 +42,7 @@ int _native_null_in_pipe[2];
 int _native_null_out_file;
 const char *_progname;
 char **_native_argv;
+pid_t _native_pid;
 
 /**
  * initialize _native_null_in_pipe to allow for reading from stdin
@@ -85,7 +86,7 @@ void _native_log_stdout(char *stdouttype)
     }
     else if (strcmp(stdouttype, "file") == 0) {
         char stdout_logname[255];
-        snprintf(stdout_logname, sizeof(stdout_logname), "/tmp/riot.stdout.%d", getpid());
+        snprintf(stdout_logname, sizeof(stdout_logname), "/tmp/riot.stdout.%d", _native_pid);
         if ((stdout_outfile = creat(stdout_logname, 0666)) == -1) {
             err(EXIT_FAILURE, "_native_log_stdout: open");
         }
@@ -120,7 +121,7 @@ void _native_log_stderr(char *stderrtype)
     }
     else if (strcmp(stderrtype, "file") == 0) {
         char stderr_logname[255];
-        snprintf(stderr_logname, sizeof(stderr_logname), "/tmp/riot.stderr.%d", getpid());
+        snprintf(stderr_logname, sizeof(stderr_logname), "/tmp/riot.stderr.%d", _native_pid);
         if ((stderr_outfile = creat(stderr_logname, 0666)) == -1) {
             err(EXIT_FAILURE, "_native_log_stderr: open");
         }
@@ -136,14 +137,12 @@ void _native_log_stderr(char *stderrtype)
 
 void daemonize()
 {
-    pid_t pid;
-
-    if ((pid = fork()) == -1) {
+    if ((_native_pid = fork()) == -1) {
         err(EXIT_FAILURE, "daemonize: fork");
     }
 
-    if (pid > 0) {
-        real_printf("RIOT pid: %d\n", pid);
+    if (_native_pid > 0) {
+        real_printf("RIOT pid: %d\n", _native_pid);
         exit(EXIT_SUCCESS);
     }
 }
@@ -191,11 +190,11 @@ __attribute__((constructor)) static void startup(int argc, char **argv)
     *(void **)(&real_malloc) = dlsym(RTLD_NEXT, "malloc");
     *(void **)(&real_realloc) = dlsym(RTLD_NEXT, "realloc");
     *(void **)(&real_free) = dlsym(RTLD_NEXT, "free");
+    *(void **)(&real_printf) = dlsym(RTLD_NEXT, "printf");
 
-   *(void **)(&real_printf) = dlsym(RTLD_NEXT, "printf");
-
-   _native_argv = argv;
+    _native_argv = argv;
     _progname = argv[0];
+    _native_pid = getpid();
 
     int argp = 1;
     char *stderrtype = "stdio";
-- 
GitLab