[systemd-devel] [RFC PATCH 2/2] journal: add async-signal-safe mode for sd_journald_sendv

Hans de Goede hdegoede at redhat.com
Mon Jan 27 07:08:48 PST 2014


Hi,

On 01/27/2014 03:19 PM, Lennart Poettering wrote:
> On Sun, 26.01.14 00:21, Zbigniew Jędrzejewski-Szmek (zbyszek at in.waw.pl) wrote:
> 
>>  
>> +static int writev_safe(int fd, const struct iovec *w, int j, bool async_signal_safe) {
>> +        if (!async_signal_safe)
>> +                return writev(fd, w, j);
>> +
>> +        for (int i = 0; i < j; i++) {
>> +                size_t written = 0;
>> +
>> +                while (written < w[i].iov_len) {
>> +                        ssize_t r;
>> +
>> +                        r = write(fd, w[i].iov_base + written, w[i].iov_len - written);
>> +                        if (r < 0 && errno != -EINTR)
>> +                                return -errno;
>> +
>> +                        written += r;
>> +                }
>> +        }
>> +
>> +        return 0;
>> +}
> 
> I don't follow here. What's the rationale for using write() here?
> Doesn't the libc include pretty much the same manual loop internally
> anyway as a fallback?
> 
> Are you sure that writzev() is not async signal safe? Maybe this is just
> a documentation oversight?

I don't know about writev, but a loop like the above certainly is not
signal safe, it will lead to potentially interleaving of parts of the message
with a message written from a signal handler.

OTOH the man page for writev says: "The data transfers performed by readv() and
writev()  are  atomic:  the data  written  by  writev()  is  written  as a single
block that is not intermingled with output  from  writes  in  other  processes"

Regards,

Hans


More information about the systemd-devel mailing list