More useful return values for shared functions 60/97560/2
authorMichal Bloch <m.bloch@samsung.com>
Fri, 28 Oct 2016 21:01:47 +0000 (23:01 +0200)
committerMichal Bloch <m.bloch@samsung.com>
Thu, 24 Nov 2016 13:58:57 +0000 (14:58 +0100)
Now returning 0/-errno instead of 1/0

Change-Id: I4c831e85c8ec7e93f183f6990839afdb63ce0a63
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
src/libdlog/log.c
src/libdlog/log_android.c
src/libdlog/log_kmsg.c
src/libdlog/log_pipe.c
src/logctrl/logctrl.c
src/logger/logger.c
src/loginit/loginit.c
src/logutil/logutil.c
src/shared/logcommon.c
src/shared/logconfig.c
src/tests/config.c

index dd8ea8a..68c9de1 100644 (file)
@@ -110,7 +110,7 @@ static void __configure(void)
        plog = 0;
        debugmode = 0;
 
-       if (!log_config_read(&conf))
+       if (log_config_read(&conf) < 0)
                goto failure;
 
        conf_value = log_config_get(&conf, "plog");
index 6483615..78ccfc8 100644 (file)
@@ -62,7 +62,7 @@ void __dlog_init_android()
        struct log_config conf;
        log_id_t buf_id;
 
-       if (!log_config_read(&conf)) {
+       if (log_config_read(&conf) < 0) {
                write_to_log = __write_to_log_null;
                return;
        }
index 4f3f619..d7fcb08 100644 (file)
@@ -69,7 +69,7 @@ void __dlog_init_kmsg()
        struct log_config conf;
        log_id_t buf_id;
 
-       if (!log_config_read(&conf)) {
+       if (log_config_read(&conf) < 0) {
                write_to_log = __write_to_log_null;
                return;
        }
index 93c7c35..d79766f 100644 (file)
@@ -163,7 +163,7 @@ void __dlog_init_pipe()
         */
        signal(SIGPIPE, SIG_IGN);
 
-       if (!log_config_read(&conf)) {
+       if (log_config_read(&conf) < 0) {
                write_to_log = __write_to_log_null;
                return;
        }
index 18d9367..b533f91 100644 (file)
@@ -101,7 +101,7 @@ int main(int argc, char ** argv)
 
        conf_etc.begin = conf_etc.last = NULL;
        conf_run.begin = conf_run.last = NULL;
-       if (!log_config_read_file(&conf_etc, filename)) {
+       if (log_config_read_file(&conf_etc, filename) < 0) {
                printf("Error: cannot open the config file!\n");
                return 1;
        }
@@ -118,7 +118,7 @@ int main(int argc, char ** argv)
 
        conf = &conf_etc;
        if (!strncmp(backend, "kmsg", sizeof("kmsg") + 1)) {
-               if (!log_config_read_file(&conf_run, KMSG_CONFIG_PATH)) {
+               if (log_config_read_file(&conf_run, KMSG_CONFIG_PATH) < 0) {
                        printf("Error: cannot open the config file!\n");
                        return 1;
                }
@@ -139,12 +139,12 @@ int main(int argc, char ** argv)
        } else if (opt.should_clear) {
                if (!log_config_remove(conf, key))
                        goto err_entry;
-               if (!log_config_write(conf, filename))
+               if (log_config_write(conf, filename) < 0)
                        goto err_save;
        } else if (opt.should_set) {
                if (!log_config_set(conf, key, val))
                        log_config_push(conf, key, val);
-               if (!log_config_write(conf, filename))
+               if (log_config_write(conf, filename) < 0)
                        goto err_save;
        } else {
                printf("Error: invalid options\n");
index 52f5bdd..e8c1b78 100644 (file)
@@ -914,10 +914,9 @@ static int parse_command_line(const char* cmdl, struct logger* server, struct wr
        reader->dumpcount = 0;
        reader->partial_log_size = 0;
 
-       if (!log_config_read(&conf)) {
-               retval = -1;
+       retval = log_config_read(&conf);
+       if (retval < 0)
                goto cleanup;
-       }
 
        conf_val = log_config_get(&conf, "backend");
        if (!conf_val) {
@@ -1623,10 +1622,7 @@ err:
  */
 static int logger_init(struct logger** server, struct log_config* config)
 {
-       if (*server)
-               logger_free(*server);
-
-       if (!log_config_read(config))
+       if (log_config_read(config) < 0)
                return -EINVAL;
 
        *server = logger_create(config);
index ecce864..e0ddb03 100644 (file)
@@ -94,7 +94,8 @@ int main()
        struct log_config conf_in;
        struct log_config conf_out;
 
-       log_config_read(&conf_in);
+       if (log_config_read(&conf_in) < 0)
+               return EXIT_FAILURE;
 
        memset(&conf_out, 0, sizeof(struct log_config));
 
@@ -121,7 +122,7 @@ int main()
                log_config_push(&conf_out, log_name_by_id(i), val);
        }
 
-       return !log_config_write(&conf_out, KMSG_CONFIG_PATH);
+       return (0 > log_config_write(&conf_out, KMSG_CONFIG_PATH)) ? EXIT_FAILURE : EXIT_SUCCESS;
 
 error:
        remove_kmsg_devs(kmsg_fd);
index c0d56d1..6035141 100644 (file)
@@ -805,7 +805,7 @@ int main(int argc, char ** argv)
                return 1;
        }
 
-       if (!log_config_read(&conf)) {
+       if (log_config_read(&conf) < 0) {
                printf("Error: config could not be read!\n");
                return 1;
        }
index 4541959..7bb9460 100644 (file)
@@ -94,7 +94,8 @@ void syslog_critical_failure(const char * message)
  * @brief Receive a file descriptor from a socket
  * @details Receives a message from given socket fd and retrieves another fd from inside
  * @param[in] sock_fd The socket's file descriptor
- * @return The file descriptor received from the socket, -1 if none.
+ * @return The file descriptor received from the socket, -errno on failure
+ * @retval -EIO No descriptor was received from the socket
  */
 int recv_file_descriptor(int socket)
 {
@@ -120,8 +121,11 @@ int recv_file_descriptor(int socket)
        message.msg_iov = iov;
        message.msg_iovlen = 1;
 
-       if ((res = recvmsg(socket, &message, 0)) <= 0)
-               return -1;
+       res = recvmsg(socket, &message, 0);
+       if (res < 0)
+               return -errno;
+       if (res == 0)
+               return -EIO;
 
        /* Iterate through header to find if there is a file descriptor */
        for (control_message = CMSG_FIRSTHDR(&message); control_message != NULL; control_message = CMSG_NXTHDR(&message, control_message)) {
@@ -131,8 +135,9 @@ int recv_file_descriptor(int socket)
                }
        }
 
-       if (fd >= 0)
+       if (fd >= 0) {
                fcntl(fd, F_SETFL, (fcntl(fd, F_GETFL, 0) & ~O_NONBLOCK));
-
-       return fd;
+               return fd;
+       } else
+               return -EIO;
 }
index 2b0f366..fbec801 100644 (file)
@@ -2,6 +2,7 @@
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <errno.h>
 
 /**
  * @brief A singular config entry list element.
@@ -71,7 +72,7 @@ int log_config_set(struct log_config* c, const char* key, const char* value)
  * @details Read the entire configuration.
  * @param[in] config The config to fill
  * @remarks The path defaults can be overridden with the DLOG_CONFIG_PATH env var
- * @return 1 if anything was read, 0 if everything failed
+ * @return 0 on success, -errno on failure
  * @see log_config_free
  * @see log_config_write
  */
@@ -80,27 +81,25 @@ int log_config_read(struct log_config* config)
        char const * override;
        char const * backend;
 
-       if (!config)
-               return 0;
-
        config->begin = config->last = NULL;
 
        override = getenv("DLOG_CONFIG_PATH");
        if (override)
                return log_config_read_file(config, override);
-       if (!log_config_read_file(config, DEFAULT_CONFIG_PATH))
-               return 0;
+       int ret = log_config_read_file(config, DEFAULT_CONFIG_PATH);
+       if (ret < 0)
+               return ret;
 
        backend = log_config_get(config, "backend");
        if (!backend) {
                log_config_free(config);
-               return 0;
+               return -ENOENT;
        }
 
        if (!strncmp(backend, "kmsg", sizeof("kmsg") + 1))
                log_config_read_file(config, KMSG_CONFIG_PATH);
 
-       return 1;
+       return 0;
 }
 
 /**
@@ -109,7 +108,7 @@ int log_config_read(struct log_config* config)
  * @remarks Comments are ignored and there is no ordering guarantee
  * @param[in] config The config to append to
  * @param[in] filename The filename of the config file
- * @return 1 on success, 0 on failure
+ * @return 0 on success, -errno on failure
  * @see log_config_read
  */
 int log_config_read_file(struct log_config* config, const char* filename)
@@ -118,12 +117,9 @@ int log_config_read_file(struct log_config* config, const char* filename)
        char line[MAX_CONF_ENTRY_LEN];
        char * tok;
 
-       if (!config)
-               return 0;
-
        file = fopen(filename, "r");
        if (!file)
-               return 0;
+               return -errno;
 
        while (fgets(line, MAX_CONF_ENTRY_LEN, file)) {
                int len = strlen(line);
@@ -146,7 +142,7 @@ int log_config_read_file(struct log_config* config, const char* filename)
        }
 
        fclose(file);
-       return 1;
+       return 0;
 }
 
 /**
@@ -172,19 +168,16 @@ void log_config_free(struct log_config* config)
  * @details Saves the given config under the given filename
  * @param[in] config The config whose contents to save
  * @see log_config_read
- * @return 0 on failure, else 1
+ * @return 0 on success, -errno on failure
  */
 int log_config_write(struct log_config* config, char const * filename)
 {
        FILE * file;
        struct log_conf_entry * e;
 
-       if (!config)
-               return 0;
-
        file = fopen(filename, "w");
        if (!file)
-               return 0;
+               return -errno;
 
        e = config->begin;
 
@@ -192,15 +185,16 @@ int log_config_write(struct log_config* config, char const * filename)
 
        while (e) {
                if (fprintf(file, "%s=%s\n", e->key, e->value) < 0) {
+                       int err = -errno;
                        fclose(file);
-                       return 0;
+                       return err;
                }
 
                e = e->next;
        }
 
        fclose(file);
-       return 1;
+       return 0;
 }
 
 /**
index b5738c3..b2d0b52 100644 (file)
@@ -3,6 +3,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <assert.h>
+#include <errno.h>
 
 int main()
 {
@@ -10,10 +11,10 @@ int main()
        struct log_config config;
 
        setenv("DLOG_CONFIG_PATH", "./non_existent_file", 1);
-       assert(!log_config_read(&config));
+       assert(log_config_read(&config) == -ENOENT);
 
        setenv("DLOG_CONFIG_PATH", "src/tests/test.conf", 1);
-       assert(log_config_read(&config));
+       assert(!log_config_read(&config));
 
        get = log_config_get(&config, "foo");
        assert(get);