On 2016-04-15, Rainer Weikusat <
rwei...@talktalk.net> wrote:
> In a certain program, I need to determine the file descriptor number
> which will be used to log syslog messages.The obvious idea for this
> would be (in a single-threaded program)
(Single threaded, and no asynchronous signal handlers going off
that could open/close descriptors.)
Is it required that syslog goes through a file descriptor at all,
or that openlog acquires that descriptor?
Ubuntu:
Nothing upon just opening the logger:
$ strace txr -e '(openlog "foo")'
[ ... ]
readlink("/proc/self/exe", "/usr/local/bin/txr", 4096) = 18
brk(0x8643000) = 0x8643000
ioctl(0, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig icanon echo ...}) = 0
ioctl(0, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig icanon echo ...}) = 0
exit_group(0) = ?
Logging an actual message lazily creates a socket, whether or not
you call openlog first. Before that, it calls time, and accesses time zone
info.
$ strace txr -e '(syslog log-emerg "foo")'
[ ... ]
readlink("/proc/self/exe", "/usr/local/bin/txr", 4096) = 18
brk(0x9cc2000) = 0x9cc2000
ioctl(0, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig icanon echo ...}) = 0
ioctl(0, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig icanon echo ...}) = 0
time(NULL) = 1460748667
open("/etc/localtime", O_RDONLY|O_CLOEXEC) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=2875, ...}) = 0
fstat64(3, {st_mode=S_IFREG|0644, st_size=2875, ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x285000
read(3, "TZif2\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\4\0\0\0\4\0\0\0\0"..., 4096) = 2875
_llseek(3, -24, [2851], SEEK_CUR) = 0
read(3, "\nPST8PDT,M3.2.0,M11.1.0\n", 4096) = 24
close(3) = 0
munmap(0x285000, 4096) = 0
socket(PF_FILE, SOCK_DGRAM|SOCK_CLOEXEC, 0) = 3
connect(3, {sa_family=AF_FILE, path="/dev/log"}, 110) = 0
send(3, "<8>Apr 15 12:31:07 txr: foo", 27, MSG_NOSIGNAL) = 27
exit_group(0) = ?
By the way, AF_FILE??? Never heard of it. Ah, synonym for AF_LOCAL, which I
also never heard of, which is some POSIX alias for AF_UNIX. (For
trademark-avoiding paranoics?)
> static int log_fd = -1;
>
> static void open_log(void)
> {
> log_fd = open("/dev/null", O_RDWR, 0);
> close(log_fd);
>
> openlog("clfds", LOG_PID | LOG_PERROR | LOG_NDELAY, LOG_USER);
Here we should probably interrogate log_fd to see whether it is now a
valid descriptor, from which we can infer that openlog opened it for
some purpose, and left it open:
if (fcntl(log_fd, F_GETFD) == -1) && errno == EBADF)
log_fd = -1;
(Of course, we already know that this isn't enough on one
widespread system.)