$ cat t.c #include <unistd.h> #include <syslog.h> int main() { openlog("test", LOG_PERROR, LOG_USER); syslog(LOG_INFO, "before fork: %d\n", getpid()); fork(); syslog(LOG_INFO, "after fork: %d\n", getpid()); return 0; } $ cc -o t t.c On 12.4-RELEASE (note last line): $ ./t test 55317 - - before fork: 55317 test 55317 - - after fork: 55317 test 55317 - - after fork: 55318 On 13.1-RELEASE-p3: $ ./t test 16590 - - before fork: 16590 test 16590 - - after fork: 16590 test 16591 - - after fork: 16591 Suggested fix for 12.4: Make LogPid a local variable inside vsyslog1(), let parse_tag() return either the parsed pid or -1, and assign LogPid either that return value or getpid().
Created attachment 239223 [details] proposed fix
Please test attached patch. You need to rebuild and reinstall libc only. Full buildworld not required. fetch -o /tmp/syslog.c.diff https://bz-attachments.freebsd.org/attachment.cgi?id=239223 cd /usr/src/lib/libc make obj depend && make all install Running processes need restart to use fix.
(In reply to Eugene Grosbein from comment #2) Apply the patch after fetching it: cd /usr/src patch < /tmp/syslog.c.diff
Thanks, your patch resolved this: root@fbsd124:~ # freebsd-version -kru 12.4-RELEASE 12.4-RELEASE 12.4-RELEASE root@fbsd124:~ # ./t test 18410 - - before fork: 18410 test 18410 - - after fork: 18410 test 18411 - - after fork: 18411 root@fbsd124:~ # Had to build lib/libnetbsd before lib/libc, btw.
A commit in branch main references this bug: URL: https://cgit.FreeBSD.org/src/commit/?id=2ce3ef55035093cac7839e71e9ff91f5562ebc29 commit 2ce3ef55035093cac7839e71e9ff91f5562ebc29 Author: Eugene Grosbein <eugen@FreeBSD.org> AuthorDate: 2023-01-03 08:53:47 +0000 Commit: Eugene Grosbein <eugen@FreeBSD.org> CommitDate: 2023-01-03 08:58:36 +0000 syslog: fix PID of forking process Do not cache PID for a process that does not fabricate it, calls openlog() before forking and does not call exec() thereafter. PR: 268666 Fixes: e9ae9fa93745669b7dd0341d333257ad6cfe8e37 Tested by: kp MFC after: 3 days lib/libc/gen/syslog.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
A commit in branch stable/13 references this bug: URL: https://cgit.FreeBSD.org/src/commit/?id=77c0992af4e3b013915ae84ef3d831f470ea5dd5 commit 77c0992af4e3b013915ae84ef3d831f470ea5dd5 Author: Eugene Grosbein <eugen@FreeBSD.org> AuthorDate: 2023-01-03 08:53:47 +0000 Commit: Eugene Grosbein <eugen@FreeBSD.org> CommitDate: 2023-01-06 04:40:42 +0000 syslog: fix PID of forking process Do not cache PID for a process that does not fabricate it, calls openlog() before forking and does not call exec() thereafter. PR: 268666 Fixes: e9ae9fa93745669b7dd0341d333257ad6cfe8e37 Tested by: kp (cherry picked from commit 2ce3ef55035093cac7839e71e9ff91f5562ebc29) lib/libc/gen/syslog.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
A commit in branch stable/12 references this bug: URL: https://cgit.FreeBSD.org/src/commit/?id=14814ff45bb4f8e9accd0b45cc1f3bcf4fd9d32f commit 14814ff45bb4f8e9accd0b45cc1f3bcf4fd9d32f Author: Eugene Grosbein <eugen@FreeBSD.org> AuthorDate: 2023-01-03 08:53:47 +0000 Commit: Eugene Grosbein <eugen@FreeBSD.org> CommitDate: 2023-01-06 04:46:33 +0000 syslog: fix PID of forking process Do not cache PID for a process that does not fabricate it, calls openlog() before forking and does not call exec() thereafter. PR: 268666 Fixes: e9ae9fa93745669b7dd0341d333257ad6cfe8e37 Tested by: kp (cherry picked from commit 2ce3ef55035093cac7839e71e9ff91f5562ebc29) lib/libc/gen/syslog.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Thank you very much for the report. The issue is fixed in head and stable branches.