fork-safe rt_print, rt_syslog extension
cover fork-safe rt_print, rt_syslog extension
Please pull Oliver's rt_print patches from
git://xenomai.org/xenomai-jki.git for-upstream
Oliver Schlenker (2):
Fork-safe rt_print
Syslog extension of rt_print
include/rtdk.h | 2 + src/rtdk/rt_print.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 67 insertions(+), 7 deletions(-)
PS: Trying to manage patches by hand is doomed to fail. I can only recommend anyone investing the time to get familiar with basic git commands. It quickly pays off, even for managing only private changes.
PATCH 1/2 Fork-safe rt_print
From: Oliver Schlenker <[email protected]>
Fork-safe initialisation of rt_print library, necessary child initialisation are done via pthread_atfork() when child process is started.
---
src/rtdk/rt_print.c | 33 +++++++++++++++++++++++++++++---- 1 files changed, 29 insertions(+), 4 deletions(-)
src/rtdk/rt_print.c +29 -4
@@ -417,9 +417,35 @@ static void *printer_loop(void *arg) } } -void __rt_print_init (void ) +static void spawn_printer_thread (void ) {pthread_attr_t thattr; + +pthread_attr_init (&thattr); +pthread_attr_setstacksize (&thattr,PTHREAD_STACK_MIN ); +pthread_create (&printer_thread, &thattr, printer_loop,NULL ); +} + +static void forked_child_init (void ) +{ +struct print_buffer *my_buffer =pthread_getspecific (buffer_key); +struct print_buffer **pbuffer = &first_buffer; + +/* re-init to avoid finding it locked by some parent thread */ +pthread_mutex_init (&buffer_lock,NULL ); + +while (*pbuffer) { +if (*pbuffer == my_buffer) + pbuffer = &(*pbuffer)->next; +else +cleanup_buffer (*pbuffer); + } + +spawn_printer_thread (); +} + +void __rt_print_init (void ) +{const char *value_str;unsigned long long period;
@@ -456,7 +482,6 @@ void __rt_print_init(void)pthread_cond_init (&printer_wakeup,NULL ); -pthread_attr_init (&thattr); -pthread_attr_setstacksize (&thattr,PTHREAD_STACK_MIN ); -pthread_create (&printer_thread, &thattr, printer_loop,NULL ); +spawn_printer_thread (); +pthread_atfork (NULL ,NULL , forked_child_init); }
PATCH 2/2 Syslog extension of rt_print
In reply to Jan KiszkaFrom: Oliver Schlenker <[email protected]>
Extension of rt_print library with rt_syslog() and rt_vsyslog() to print messages rt-safe directly to syslog.
---
include/rtdk.h | 2 ++ src/rtdk/rt_print.c | 39 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 3 deletions(-)
include/rtdk.h +2 -0
@@ -52,6 +52,8 @@ int rt_vfprintf(FILE *stream, const char *format, va_list args);int rt_vprintf (const char *format,va_list args);int rt_fprintf (FILE *stream,const char *format, ...);int rt_printf (const char *format, ...); +void rt_syslog (int priority,char *format, ...); +void rt_vsyslog (int priority,char *format,va_list args);int rt_print_init (size_t buffer_size,const char *name);void rt_print_cleanup (void );
src/rtdk/rt_print.c +36 -3
@@ -25,6 +25,7 @@#include <stdlib.h> #include <string.h> #include <unistd.h> +#include <syslog.h> #include <rtdk.h> #include <asm/xenomai/system.h>
@@ -37,9 +38,12 @@#define RT_PRINT_LINE_BREAK 256 +#define RT_PRINT_SYSLOG_STREAM NULL +struct entry_head {FILE *dest;uint32_t seq_no; +int priority;char text[1 ]; } __attribute__((packed));
@@ -76,7 +80,8 @@ static void print_buffers(void);/* *** rt_print API *** */ -int rt_vfprintf (FILE *stream,const char *format,va_list args) +static int print_to_buffer (FILE *stream,int priority,const char *format, +va_list args) {struct print_buffer *buffer =pthread_getspecific (buffer_key);off_t write_pos, read_pos;
@@ -115,6 +120,7 @@ int rt_vfprintf(FILE *stream, const char *format, va_list args)/* Write out empty entry */ head = buffer->ring + write_pos; head->seq_no = seq_no; + head->priority =0 ; head->text[0 ] =0 ;/* Forward to the ring buffer start */
@@ -148,6 +154,7 @@ int rt_vfprintf(FILE *stream, const char *format, va_list args)/* If we were able to write some text, finalise the entry */ if (len >0 ) { head->seq_no = ++seq_no; + head->priority = priority; head->dest = stream;/* Move forward by text and head length */
@@ -160,6 +167,7 @@ int rt_vfprintf(FILE *stream, const char *format, va_list args)/* An empty entry marks the wrap-around */ head = buffer->ring + write_pos; head->seq_no = seq_no; + head->priority = priority; head->text[0 ] =0 ; write_pos =0 ;
@@ -173,6 +181,11 @@ int rt_vfprintf(FILE *stream, const char *format, va_list args)return res; } +int rt_vfprintf (FILE *stream,const char *format,va_list args) +{ +return print_to_buffer (stream,0 , format, args); +} +int rt_vprintf (const char *format,va_list args) {return rt_vfprintf (stdout, format, args);
@@ -202,6 +215,20 @@ int rt_printf(const char *format, ...)return n; } +void rt_syslog (int priority,char *format, ...) +{ +va_list args; + +va_start (args, format); +print_to_buffer (RT_PRINT_SYSLOG_STREAM , priority, format, args); +va_end (args); +} + +void rt_vsyslog (int priority,char *format,va_list args) +{ +print_to_buffer (RT_PRINT_SYSLOG_STREAM , priority, format, args); +} +static void set_buffer_name (struct print_buffer *buffer,const char *name) {int n;
@@ -311,7 +338,6 @@ const char *rt_print_buffer_name(void)return buffer->name; } -/* *** Deferred Output Management *** */ static void cleanup_buffer (struct print_buffer *buffer)
@@ -384,7 +410,14 @@ static void print_buffers(void)if (len) {/* Print out non-empty entry and proceed */ -fprintf (head->dest,"%s" , head->text); +/* Check if output goes to syslog */ +if (head->dest ==RT_PRINT_SYSLOG_STREAM ) { +syslog (head->priority,"%s" , head->text); + }else { +/* Output goes to specified stream */ +fprintf (head->dest,"%s" , head->text); + } + read_pos +=sizeof (*head) + len; }else {/* Emptry entries mark the wrap-around */
Quoted 9 lines · Jan Kiszka
Please pull Oliver's
rt_printpatches from
git://xenomai.org/xenomai-jki.gitfor-upstreamOliver Schlenker (2):
Fork-safert_print
Syslog extension ofrt_print
Quoted 7 lines
include/rtdk.h | 2 + src/rtdk/rt_print.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 67 insertions(+), 7 deletions(-)PS: Trying to manage patches by hand is doomed to fail. I can only recommend anyone investing the time to get familiar with basic git commands. It quickly pays off, even for managing only private changes.
Please, make sure to provide a prefix in your short log; this makes sorting the short logs for changes by topic a bit easier. E.g.
rtdk: fork-safe rt_print
rtdk: syslog extension of rt_print
Signature · 1 line
Philippe.
Quoted 18 lines · Philippe Gerum
Quoted 9 lines · Jan Kiszka
Please pull Oliver's
rt_printpatches from
git://xenomai.org/xenomai-jki.gitfor-upstreamOliver Schlenker (2):
Fork-safert_print
Syslog extension ofrt_printQuoted 7 lines
include/rtdk.h | 2 + src/rtdk/rt_print.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 67 insertions(+), 7 deletions(-)PS: Trying to manage patches by hand is doomed to fail. I can only recommend anyone investing the time to get familiar with basic git commands. It quickly pays off, even for managing only private changes.
Quoted 6 lines
Please, make sure to provide a prefix in your short log; this makes sorting the short logs for changes by topic a bit easier. E.g.
rtdk: fork-safe
rt_print
rtdk: syslog extension ofrt_print
As you already merged it - too late to fix. Sorry, will try to avoid forgetting this in the future.
Jan