plog = 0;
debugmode = 0;
- if (!log_config_read(&conf))
+ if (log_config_read(&conf) < 0)
goto failure;
conf_value = log_config_get(&conf, "plog");
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;
}
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;
}
*/
signal(SIGPIPE, SIG_IGN);
- if (!log_config_read(&conf)) {
+ if (log_config_read(&conf) < 0) {
write_to_log = __write_to_log_null;
return;
}
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;
}
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;
}
} 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");
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) {
*/
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);
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));
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);
return 1;
}
- if (!log_config_read(&conf)) {
+ if (log_config_read(&conf) < 0) {
printf("Error: config could not be read!\n");
return 1;
}
* @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)
{
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)) {
}
}
- if (fd >= 0)
+ if (fd >= 0) {
fcntl(fd, F_SETFL, (fcntl(fd, F_GETFL, 0) & ~O_NONBLOCK));
-
- return fd;
+ return fd;
+ } else
+ return -EIO;
}
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
+#include <errno.h>
/**
* @brief A singular config entry list element.
* @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
*/
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;
}
/**
* @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)
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);
}
fclose(file);
- return 1;
+ return 0;
}
/**
* @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;
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;
}
/**
#include <stdlib.h>
#include <string.h>
#include <assert.h>
+#include <errno.h>
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);