Timeline for Proper way to write C code that injects message into /var/log/messages?
Current License: CC BY-SA 4.0
18 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jun 26, 2024 at 23:29 | comment | added | ilkkachu | @ron, Except that there aren't a hundred other processes that might write into that file. Usually just the one, the logging daemon. Was your intention to ask how to send a log message to the usual logging mechanisms (possibly ending up in that file)? | |
| Jun 26, 2024 at 21:01 | vote | accept | ron | ||
| Jun 26, 2024 at 15:32 | comment | added | Chris Davies | @ron that's fine. Just didn't want you reinventing a standard system tool needlessly :) | |
| Jun 26, 2024 at 15:22 | comment | added | ron | their last paragraph... issue with this question is that they're not really saying ... I was not aware other people were involved in writing my C code :) it just doesn't feel proper doing a fprintf from left field into /var/log/message when I know any of a hundred other processes write into this one file at any given moment and there is a standardized line format, and a daemon surrounding this file. | |
| Jun 26, 2024 at 15:08 | comment | added | ron | @Chris Davies - for what I want to ultimately do and for flexibility I am more proficient at writing C than bash, and can incorporate some logic to interpret data as to whether or not issue a system shutdown, rather than doing just a if x>y shutdown in a shell script. | |
| Jun 26, 2024 at 11:01 | comment | added | grawity | @ilkkachu: No, their last paragraph very strongly implies that that's the opposite of what they're saying and that you're taking the title of the post much too literally. The fact that they specifically rule out direct fopen() means they already realize that there is some kind of infrastructure that they would be bypassing, and that they would prefer to use it rather than bypass it, but they don't know what the exact infrastructure is – nor the precise terminology for it – so they describe it in terms of the effect of "make messages appear in /var/log/messages" that they see. | |
| Jun 26, 2024 at 9:20 | comment | added | ilkkachu | @AndrewHenle, I would expect a single a fprintf() to result in just one write(), actually. But you're right, it might not, so manually doing sprintf() and write() might be better. Remembering to open in append mode is important, though, but they did have "a" there already. The issue with this question is that they're not really saying they want to send a message to the regular system logging infrastructure, but that they just want to write to a particular file. Those are two different questions, with two different answers, and syslog() is only an answer to one of them. | |
| S Jun 26, 2024 at 8:53 | history | suggested | genpfault | CC BY-SA 4.0 | Fix captialization/punctuation |
| Jun 25, 2024 at 23:24 | comment | added | Andrew Henle | @ilkkachu Why does fopen() + fprintf() "feel like a hack"? It's not multithread-safe. Parallel writers will likely write intermingled messages. Any single fprintf() call can translate to multiple actual write() calls. open() and write() can get around that, but at that point the amount of work necessary compared to a simple syslog() call usually makes such work a wasted effort. | |
| Jun 25, 2024 at 15:42 | review | Suggested edits | |||
| S Jun 26, 2024 at 8:53 | |||||
| Jun 25, 2024 at 1:43 | comment | added | Austin Hemmelgarn | Nitpick: Never call init 0 on a modern Linux system, it’s a legacy interface if you’re using systemd (and is thus liable to go away whenever the systemd developers feel like it shouldn’t exist anymore) and it almost always disconnects any other login sessions without warning. Always prefer shutdown -hP now, or on systemd systems possibly systemctl poweroff. They’re not legacy interfaces and will ensure that logged in users actually get notified that the system is shutting down (and will also log the shutdown, though not to the degree you seem to want to). | |
| Jun 24, 2024 at 21:10 | comment | added | Stephen Kitt | @ilkkachu for /var/log/messages it’s not ideal though — in particular, the process might not have permission to write to it, whereas the system logger would. | |
| Jun 24, 2024 at 20:53 | comment | added | ilkkachu | Why does fopen() + fprintf() "feel like a hack"? It's pretty much the standard C way to write to a a particular file, if that's what you want to do. (You could also use the lower level POSIX interfaces open() and write().) | |
| Jun 24, 2024 at 14:02 | history | edited | ron | CC BY-SA 4.0 | deleted 8 characters in body |
| Jun 24, 2024 at 11:18 | comment | added | Chris Davies | Do you need to write your own C code or would using the standard logger utility from a shell script match your requirements better? | |
| Jun 24, 2024 at 9:25 | history | became hot network question | |||
| Jun 24, 2024 at 4:37 | answer | added | Stephen Kitt | timeline score: 47 | |
| Jun 24, 2024 at 1:24 | history | asked | ron | CC BY-SA 4.0 |