[systemd-devel] [RFC PATCH 1/2] Replace mkostemp+unlink with open(O_TMPFILE)

Zbigniew Jędrzejewski-Szmek zbyszek at in.waw.pl
Mon Jan 27 09:27:16 PST 2014


On Mon, Jan 27, 2014 at 05:57:56PM +0100, Kay Sievers wrote:
> On Mon, Jan 27, 2014 at 5:37 PM, Zbigniew Jędrzejewski-Szmek
> <zbyszek at in.waw.pl> wrote:
> > On Mon, Jan 27, 2014 at 11:52:48AM -0200, Lucas De Marchi wrote:
> >> On Sun, Jan 26, 2014 at 3:21 AM, Zbigniew Jędrzejewski-Szmek
> >> > +int open_tmpfile(const char *path, int flags) {
> >> > +        int fd;
> >> > +        char *p;
> >> > +
> >> > +#ifdef O_TMPFILE
> >> > +        fd = open(path, flags | O_TMPFILE, S_IRUSR | S_IWUSR);
> >> > +        if (fd >= 0)
> >> > +                return fd;
> >> > +#endif
> >> > +        p = strappenda(path, "/systemd-tmp-XXXXXX");
> >> > +
> >> > +        RUN_WITH_UMASK(0077) {
> >> > +                fd = mkostemp(p, O_RDWR|O_CLOEXEC);
> >> > +        }
> >> > +
> >> > +        if (fd < 0)
> >> > +                return -errno;
> >> > +
> >> > +        unlink(path);
> >>
> >> This unlink() doesn't make much sense with O_TMPFILE.
> >
> > unlink is only reached if open(O_TMPFILE) failed and fd comes
> > from mkostemp.
> 
> Can we expect open(O_TMPFILE) to fail on kernels which do not support it?
> I guess they will just silently ignore it? Then we never unlink?
No, it is supposed to fail properly if it doesn't work. The flags were
changed to be incompatible with old kernels. We should probably have a
unit test for that. I have a small utility I was using for tests, I'll
dig it up.

Zbyszek


More information about the systemd-devel mailing list