Report errors in write_config
[lttng-tools.git] / lttng / conf.c
index ac1c172964ca263029e0906e02c12e2fa1182bd7..5479a3cd016e740100d14198e210d45769da013d 100644 (file)
@@ -3,8 +3,8 @@
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * as published by the Free Software Foundation; only version 2
+ * of the License.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 #include "lttngerr.h"
 
 /*
- *  get_config_file_path
+ *  config_get_file_path
  *
  *  Return the path with '/CONFIG_FILENAME' added to it.
  */
-static char *get_config_file_path(char *path)
+char *config_get_file_path(char *path)
 {
        int ret;
        char *file_path;
@@ -56,7 +56,7 @@ static FILE *open_config(char *path, const char *mode)
        FILE *fp = NULL;
        char *file_path;
 
-       file_path = get_config_file_path(path);
+       file_path = config_get_file_path(path);
        if (file_path == NULL) {
                goto error;
        }
@@ -97,63 +97,41 @@ error:
        return ret;
 }
 
-/*
- *  create_config_dir
- *
- *  Create the empty config dir.
- */
-static int create_config_dir(char *path)
-{
-       int ret;
-
-       /* Create session directory .lttng */
-       ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP);
-       if (ret < 0) {
-               if (errno != EEXIST) {
-                       perror("mkdir config");
-                       ERR("Couldn't init config directory at %s", path);
-                       ret = -errno;
-                       goto error;
-               } else {
-                       ret = 0;
-               }
-       }
-
-error:
-       return ret;
-}
-
 /*
  *  write_config
  *
  *  Append data to the config file in file_path
  */
-static void write_config(char *file_path, size_t size, char *data)
+static int write_config(char *file_path, size_t size, char *data)
 {
        FILE *fp;
+       size_t len;
+       int ret = 0;
 
        fp = open_config(file_path, "a");
        if (fp == NULL) {
-               goto error;
+               ret = -1;
+               goto end;
        }
 
        /* Write session name into config file */
-       fwrite(data, size, 1, fp);
+       len = fwrite(data, size, 1, fp);
+       if (len < 1) {
+               ret = -1;
+       }
        fclose(fp);
-
-error:
-       return;
+end:
+       return ret;
 }
 
 /*
  *  config_get_default_path
  *
- *  Return the HOME directory path. The output is dup so the user MUST
- *  free(3) the returned string.
+ *  Return the HOME directory path. Caller MUST NOT free(3) the return pointer.
  */
 char *config_get_default_path(void)
 {
-       return strdup(getenv("HOME"));
+       return getenv("HOME");
 }
 
 /*
@@ -166,7 +144,10 @@ void config_destroy(char *path)
        int ret;
        char *config_path;
 
-       config_path = get_config_file_path(path);
+       config_path = config_get_file_path(path);
+       if (config_path == NULL) {
+               return;
+       }
 
        ret = remove(config_path);
        if (ret < 0) {
@@ -189,7 +170,7 @@ char *config_read_session_name(char *path)
 
        fp = open_config(path, "r");
        if (fp == NULL) {
-               ERR("Can't find valid lttng config in %s", path);
+               ERR("Can't find valid lttng config %s/.lttngrc", path);
                goto error;
        }
 
@@ -233,46 +214,24 @@ int config_add_session_name(char *path, char *name)
        if (ret < 0) {
                goto error;
        }
-
-       write_config(path, ret, session_name);
-       ret = 0;
-
+       ret = write_config(path, ret, session_name);
 error:
        return ret;
 }
 
-/*
- *  config_generate_dir_path
- *
- *  Return allocated path string to path/CONFIG_DIRNAME.
- */
-char *config_generate_dir_path(char *path)
-{
-       int ret;
-       char *new_path;
-
-       ret = asprintf(&new_path, "%s/%s", path, CONFIG_DIRNAME);
-       if (ret < 0) {
-               perror("config path problem");
-               goto error;
-       }
-
-error:
-       return new_path;
-}
-
 /*
  *  config_init
  *
  *  Init configuration directory and file.
  */
-int config_init(char *path)
+int config_init(char *session_name)
 {
        int ret;
+       char *path;
 
-       /* Create config directory (.lttng) */
-       ret = create_config_dir(path);
-       if (ret < 0) {
+       path = config_get_default_path();
+       if (path == NULL) {
+               ret = -1;
                goto error;
        }
 
@@ -282,6 +241,11 @@ int config_init(char *path)
                goto error;
        }
 
+       ret = config_add_session_name(path, session_name);
+       if (ret < 0) {
+               goto error;
+       }
+
        DBG("Init config session in %s", path);
 
 error:
This page took 0.026073 seconds and 4 git commands to generate.