xref: /openbmc/qemu/qga/cutils.c (revision f0984d40)
1 /*
2  * This work is licensed under the terms of the GNU GPL, version 2 or later.
3  * See the COPYING file in the top-level directory.
4  */
5 
6 #include "qemu/osdep.h"
7 #include "cutils.h"
8 #include "qapi/error.h"
9 
10 /**
11  * qga_open_cloexec:
12  * @name: the pathname to open
13  * @flags: as in open()
14  * @mode: as in open()
15  *
16  * A wrapper for open() function which sets O_CLOEXEC.
17  *
18  * On error, -1 is returned.
19  */
20 int qga_open_cloexec(const char *name, int flags, mode_t mode)
21 {
22     int ret;
23 
24 #ifdef O_CLOEXEC
25     ret = open(name, flags | O_CLOEXEC, mode);
26 #else
27     ret = open(name, flags, mode);
28     if (ret >= 0) {
29         qemu_set_cloexec(ret);
30     }
31 #endif
32 
33     return ret;
34 }
35