Skip to content

libs/libc/syslog: Implement openlog() and closelog(). - #21128

Draft
casaroli wants to merge 2 commits into
apache:masterfrom
casaroli:fb-openlog-functions-up
Draft

casaroli wants to merge 2 commits into
apache:masterfrom
casaroli:fb-openlog-functions-up

Conversation

@casaroli

@casaroli casaroli commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

<syslog.h> defined openlog() as a macro that is a block of statements and closelog() as an empty macro. Programs that write (void) openlog(...); or (void) closelog(); did not compile (tcp-wrappers' tcpd.c, for example), and a program that declares them itself, takes their address, or a configure link test did not find them.

The first commit makes them functions of libc, as in musl: openlog() keeps a copy of the ident (up to 31 characters), the options and the facility in the task information of the process (task_info_s.ta_openlog, one per task group, in every build mode), and the messages of the process get ident: before them, ident[pid]: with LOG_PID. With LOG_PERROR, syslog() writes the message to stderr too. The other options and the facility have no effect (NuttX has no system logger to connect to); closelog() has no descriptor to close, and the settings stay, as in musl. The OS adds the ident in nx_vsyslog(), in the stream that already writes the header, so syslog() needs no buffer and no lock: vsyslog() of the programs sets the internal flag SYSLOG_OPENLOG in the priority, and only then nx_vsyslog() reads the settings, during a syslog() call of that process. Messages of the OS and from interrupt handlers have no ident. The second commit describes them in Documentation/standards/posix.rst and on the logging page.

Impact

Two new libc functions; the two macros are gone. The same in flat, protected and kernel builds; nothing per architecture. A file that defines its own openlog() or closelog() macro before <syslog.h> does not compile any more, because the macro now expands inside the declaration. nuttx-apps' interpreters/luamodules/luasyslog did that (on the command line); apache/nuttx-apps#3939 removes those macros and works with both the old and the new <syslog.h>, so it must merge first (sim:lua builds luasyslog).

Testing

Test program openlog.c (expr: (void) openlog(...) and (void) closelog() compile; decl: the program declares both itself and takes the address of openlog(); log: syslog() after openlog() and after closelog()), compiled against the export of qemu-armv8a:knsh (kernel build) and run under QEMU (HVF):

master aeab6fb this PR
openlog does not compile (macro "closelog" passed 1 arguments, expected identifier before '{') expr PASS, decl PASS, log PASS

Test program openlog2.c (stderr is a pipe, the test reads what syslog() wrote), qemu-armv8a:knsh (kernel) and qemu-armv8a:nsh (flat, builtin app), both with CONFIG_PIPES: ident olt: one 1, LOG_PID olt[5]: two, a message with its own newline, an ident of 40 characters cut to 31, the settings after closelog(), no ident: PASS; flat only: a new task group has its own settings (its openlog() does not change the ident of the parent): PASS. The console shows the same lines: the prefix that nx_vsyslog() adds.

ostest exits with status 0 on qemu-armv8a:knsh (kernel) and qemu-armv8a:nsh (flat).

Builds, each without new warnings: stm32f4discovery:nsh (flat, Cortex-M4), lm3s6965-ek:qemu-protected and mps2-an500:knsh (protected, Cortex-M3), rv-virt:knsh64 (kernel, RISC-V), qemu-armv8a:nsh with CONFIG_SYSLOG_RFC5424 (flat, arm64) and sim:lua (macOS host, with luasyslog and the nuttx-apps change). A file with the old luasyslog macros and openlog()/closelog() calls, compiled against the knsh export: master compiles it, this PR fails ("macro "closelog" passed 1 arguments"); without the macros both compile.

tools/checkpatch.sh -c -u -m -g passes.

depends-on: apache/nuttx-apps#3939

@cederom

cederom commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Not sure if this is okay to state that functions are "SUPPORTED" but DOES NOTHING as per proposition, over stating that functions are not implemented. Sounds like cheating :D

Either we support them and their functionality is operational or not at all :-)

@casaroli
casaroli force-pushed the fb-openlog-functions-up branch from be2f335 to 4f77204 Compare October 29, 2026 15:57
@casaroli casaroli changed the title libs/libc/syslog: Make openlog() and closelog() functions. libs/libc/syslog: Implement openlog() and closelog(). Oct 29, 2026
acassis
acassis previously approved these changes Oct 29, 2026
Comment thread libs/libc/syslog/lib_openlog.c Outdated
* the other builds each task group has its settings in its task_info_s.
*/

static struct openlog_s g_openlog;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not always put into task tls even for kernel mode

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread libs/libc/syslog/lib_syslog.c Outdated
* such a message goes out without the settings.
*/

nxrmutex_lock(&info->ta_lock);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syslog is called in interrupt context, malloc and mutex need be avoided

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread libs/libc/syslog/lib_syslog.c Outdated
*
****************************************************************************/

static bool vsyslog_openlog(int priority, FAR const IPTR char *fmt,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not do the format in kernel side, so we can avoid malloc and mutex

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@casaroli

Copy link
Copy Markdown
Contributor Author

Either we support them and their functionality is operational or not at all :-)

Done.

@github-actions github-actions Bot added Area: Documentation Improvements or additions to documentation Size: S The size of the change in this PR is small labels Oct 29, 2026
<syslog.h> defined openlog() as a macro that is a block of statements
and closelog() as an empty macro, and syslog() did not use the ident or
the options.  Programs that write "(void) openlog(...);" or
"(void) closelog();" did not compile (tcp-wrappers' tcpd.c), a program
that declares them, or a link test, did not find them, and the messages
of a program had no ident.

Make them functions of libc, as in musl: openlog() keeps a copy of the
ident (up to 31 characters), the options and the facility in the task
information of the process (task_info_s.ta_openlog, one per task
group), and the messages of the process get "ident: " before them,
"ident[pid]: " with LOG_PID.  With LOG_PERROR, syslog() writes the
message to stderr too.  The other options and the facility have no
effect: NuttX has no system logger to connect to.  closelog() has no
descriptor to close; the settings stay, as in musl.

The OS adds the ident in nx_vsyslog(), in the stream that already
writes the header, so syslog() needs no buffer and no lock.  vsyslog()
of the programs sets the internal flag SYSLOG_OPENLOG in the priority,
and only then nx_vsyslog() reads the settings: during a syslog() call of
the process, when its task information is in memory.  The messages of
the OS have no ident, and neither have messages from an interrupt
handler.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
openlog() and closelog() are now functions of libc, and syslog() uses
the ident and the options of openlog().  Mark them as supported in the
POSIX table, and describe them on the logging page.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@casaroli
casaroli force-pushed the fb-openlog-functions-up branch from 4f77204 to b1022d8 Compare October 29, 2026 17:51
@github-actions github-actions Bot added Size: M The size of the change in this PR is medium Area: Drivers Drivers issues and removed Size: S The size of the change in this PR is small Area: Documentation Improvements or additions to documentation labels Oct 29, 2026
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

  • flash: .data +18 B, .text +514 B (+0.8%, 65,664 B / 262,144 B, total: 25% used)
  • sram: .bss +32 B, .data +18 B (+1.9%, 2,740 B / 8,192 B, total: 33% used)

esp32-devkitc

  • ROM: .flash.rodata +16 B, .flash.text +364 B (+0.3%, 128,164 B / 4,194,272 B, total: 3% used)
  • dram0_0_seg: .dram0.bss +32 B (+0.3%, 9,928 B / 180,736 B, total: 5% used)
  • drom0_0_seg: .flash.rodata +16 B (+0.1%, 13,812 B / 4,194,272 B, total: 0% used)
  • irom0_0_seg: .flash.text +364 B (+0.4%, 94,124 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .text +344 B (+0.4%, 84,876 B / 4,194,304 B, total: 2% used)
  • sram: .bss +32 B (+0.8%, 3,924 B / 16,384 B, total: 24% used)

mirtoo

  • kseg0_progmem: .text +596 B (+0.9%, 67,396 B / 131,072 B, total: 51% used)
  • kseg1_datamem: .bss +48 B (+1.9%, 2,568 B / 32,256 B, total: 8% used)

qemu-armv8a

  • Code: .rodata +16 B, .text.nx_vsyslog +132 B, .text.vsyslog +20 B (+0.2%, 354,256 B)
  • Data: .bss.g_kthread_group +40 B (+0.1%, 77,599 B)

qemu-intel64

  • Code: .text +640 B (+0.0%, 8,687,623 B)
  • Data: .bss +64 B, .rodata +32 B (+0.1%, 125,113 B)

rx65n-rsk2mb

  • RAM: .bss +48 B (+0.7%, 6,586 B / 262,144 B, total: 3% used)
  • ROM: .rodata +20 B, .text +288 B (+0.3%, 90,220 B / 2,097,152 B, total: 4% used)

s698pm-dkit

  • Code: .text +1,136 B (+0.3%, 383,728 B)
  • Data: .bss +48 B (+0.4%, 11,867 B)

stm32-nucleo-f103rb

  • flash: .text +312 B (+0.9%, 34,432 B / 131,072 B, total: 26% used)
  • sram: .bss +40 B (+1.7%, 2,428 B / 20,480 B, total: 12% used)

Catch memory regressions in CI with MemBrowse. Free for open source.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Drivers Drivers issues Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants