logging: gsmtap: Fill PID field for each message

It was recently discovered that PID field in gsmtap log messages was
always set to 0. Before this patch, the field was never being set.

The approach of this patch is to record the PID of process one, in
order to avoid calling getpid() syscall on each
log line to be sent. The counterpart of this optimization is that
eventual fork() calls would still keep the old incorrect value, but I
think nobody can safely assume that fork() is possible once all this
kind of infrastructure has already been configured (fork() should only
be done really at the start of the program before any osmocom foo is
initialized, or to immediatelly call exec()).

Related: OS#5027
Change-Id: I7db00d1810f0860166bffa0bda8566caa82e06a9
This commit is contained in:
Pau Espin 2021-02-15 12:59:48 +01:00
parent 4f1128fcbd
commit bb149ecda2
1 changed files with 7 additions and 0 deletions

View File

@ -38,6 +38,7 @@
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#ifdef HAVE_STRINGS_H
#include <strings.h>
@ -53,6 +54,8 @@
#define GSMTAP_LOG_MAX_SIZE 4096
static uint32_t logging_gsmtap_pid;
static void _gsmtap_raw_output(struct log_target *target, int subsys,
unsigned int level, const char *file,
int line, int cont, const char *format,
@ -82,6 +85,7 @@ static void _gsmtap_raw_output(struct log_target *target, int subsys,
/* Logging header */
golh = (struct gsmtap_osmocore_log_hdr *) msgb_put(msg, sizeof(*golh));
OSMO_STRLCPY_ARRAY(golh->proc_name, target->tgt_gsmtap.ident);
golh->pid = logging_gsmtap_pid;
if (subsys_name)
OSMO_STRLCPY_ARRAY(golh->subsys, subsys_name + 1);
else
@ -152,6 +156,9 @@ struct log_target *log_target_create_gsmtap(const char *host, uint16_t port,
target->type = LOG_TGT_TYPE_GSMTAP;
target->raw_output = _gsmtap_raw_output;
if (!logging_gsmtap_pid)
logging_gsmtap_pid = (uint32_t)getpid();
return target;
}