]> sigrok.org Git - libserialport.git/blobdiff - serialport.c
Use O_CLOEXEC where available
[libserialport.git] / serialport.c
index 6e681533ed24896a33de59f7e17597a4f0bf5082..9e56e89aed61c1347bef3b08f867ff2344f7772e 100644 (file)
@@ -49,19 +49,19 @@ static const struct std_baudrate std_baudrates[] = {
 
 void (*sp_debug_handler)(const char *format, ...) = sp_default_debug_handler;
 
-static void get_time(struct timeval *time);
-
 static enum sp_return get_config(struct sp_port *port, struct port_data *data,
        struct sp_port_config *config);
 
 static enum sp_return set_config(struct sp_port *port, struct port_data *data,
        const struct sp_port_config *config);
 
+#ifndef _WIN32
 static void get_time(struct timeval *time)
 {
 #ifdef HAVE_CLOCK_GETTIME
        struct timespec ts;
-       clock_gettime(CLOCK_MONOTONIC, &ts);
+       if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1)
+               clock_gettime(CLOCK_REALTIME, &ts);
        time->tv_sec = ts.tv_sec;
        time->tv_usec = ts.tv_nsec / 1000;
 #elif defined(__APPLE__)
@@ -75,6 +75,7 @@ static void get_time(struct timeval *time)
        gettimeofday(time, NULL);
 #endif
 }
+#endif
 
 SP_API enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr)
 {
@@ -562,7 +563,7 @@ SP_API enum sp_return sp_open(struct sp_port *port, enum sp_mode flags)
                RETURN_CODEVAL(ret);
        }
 #else
-       int flags_local = O_NONBLOCK | O_NOCTTY;
+       int flags_local = O_NONBLOCK | O_NOCTTY | O_CLOEXEC;
 
        /* Map 'flags' to the OS-specific settings. */
        if ((flags & SP_MODE_READ_WRITE) == SP_MODE_READ_WRITE)