[lxc-devel] [PATCH] replace redundant creat() with open()

S.Çağlar Onur caglar at 10ur.org
Fri Nov 8 21:14:56 UTC 2013


Hey Serge,

On Fri, Nov 8, 2013 at 4:06 PM, Serge Hallyn <serge.hallyn at ubuntu.com> wrote:
> Quoting S.Çağlar Onur (caglar at 10ur.org):
>> creat() is equivalent to open() with flags equal to O_CREAT|O_WRONLY|O_TRUNC
>
> Hi,
>
> I'm confused - what is redundant in the use of creat()?  If there is an
> improvement here then I don't understand what it is.  Otherwise I'd
> argue creat() is more concise and clearer about its intent.

There is no improvement other than a syntactic change (assuming
creat() is also protected by glibc like open()). I saw them while
trying to come up with a list of functions protected by process_lock
and replaced them with open() without checking what glibc does. If you
think creat is also protected than please feel free to drop this :)

>> Signed-off-by: S.Çağlar Onur <caglar at 10ur.org>
>> ---
>>  src/lxc/bdev.c      | 2 +-
>>  src/lxc/conf.c      | 6 +++---
>>  src/lxc/lxc_start.c | 2 +-
>>  3 files changed, 5 insertions(+), 5 deletions(-)
>>
>> diff --git a/src/lxc/bdev.c b/src/lxc/bdev.c
>> index c7e5e5e..0194ccd 100644
>> --- a/src/lxc/bdev.c
>> +++ b/src/lxc/bdev.c
>> @@ -1594,7 +1594,7 @@ static int do_loop_create(const char *path, unsigned long size, const char *fsty
>>       int fd, ret;
>>       // create the new loopback file.
>>       process_lock();
>> -     fd = creat(path, S_IRUSR|S_IWUSR);
>> +     fd = open(path, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR);
>>       process_unlock();
>>       if (fd < 0)
>>               return -1;
>> diff --git a/src/lxc/conf.c b/src/lxc/conf.c
>> index a756731..dc34568 100644
>> --- a/src/lxc/conf.c
>> +++ b/src/lxc/conf.c
>> @@ -909,7 +909,7 @@ static int setup_tty(const struct lxc_rootfs *rootfs,
>>                               return -1;
>>                       }
>>                       process_lock();
>> -                     ret = creat(lxcpath, 0660);
>> +                     ret = open(lxcpath, O_CREAT|O_WRONLY|O_TRUNC, 0660);
>>                       process_unlock();
>>                       if (ret==-1 && errno != EEXIST) {
>>                               SYSERROR("error creating %s\n", lxcpath);
>> @@ -945,7 +945,7 @@ static int setup_tty(const struct lxc_rootfs *rootfs,
>>                       /* If we populated /dev, then we need to create /dev/ttyN */
>>                       if (access(path, F_OK)) {
>>                               process_lock();
>> -                             ret = creat(path, 0660);
>> +                             ret = open(path, O_CREAT|O_WRONLY|O_TRUNC, 0660);
>>                               process_unlock();
>>                               if (ret==-1) {
>>                                       SYSERROR("error creating %s\n", path);
>> @@ -1546,7 +1546,7 @@ static int setup_ttydir_console(const struct lxc_rootfs *rootfs,
>>       }
>>
>>       process_lock();
>> -     ret = creat(lxcpath, 0660);
>> +     ret = open(lxcpath, O_CREAT|O_WRONLY|O_TRUNC, 0660);
>>       process_unlock();
>>       if (ret==-1 && errno != EEXIST) {
>>               SYSERROR("error %d creating %s\n", errno, lxcpath);
>> diff --git a/src/lxc/lxc_start.c b/src/lxc/lxc_start.c
>> index add2542..f8fc6d4 100644
>> --- a/src/lxc/lxc_start.c
>> +++ b/src/lxc/lxc_start.c
>> @@ -62,7 +62,7 @@ static int ensure_path(char **confpath, const char *path)
>>
>>       if (path) {
>>               if (access(path, W_OK)) {
>> -                     fd = creat(path, 0600);
>> +                     fd = open(path, O_CREAT|O_WRONLY|O_TRUNC, 0600);
>>                       if (fd < 0 && errno != EEXIST) {
>>                               SYSERROR("failed to create '%s'", path);
>>                               goto err;
>> --
>> 1.8.3.2
>>
>>
>> ------------------------------------------------------------------------------
>> November Webinars for C, C++, Fortran Developers
>> Accelerate application performance with scalable programming models. Explore
>> techniques for threading, error checking, porting, and tuning. Get the most
>> from the latest Intel processors and coprocessors. See abstracts and register
>> http://pubads.g.doubleclick.net/gampad/clk?id=60136231&iu=/4140/ostg.clktrk
>> _______________________________________________
>> Lxc-devel mailing list
>> Lxc-devel at lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/lxc-devel



-- 
S.Çağlar Onur <caglar at 10ur.org>




More information about the lxc-devel mailing list