| 1 | /* |
| 2 | * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * as published by the Free Software Foundation; only version 2 |
| 7 | * of the License. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along |
| 15 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | */ |
| 18 | |
| 19 | #define _GNU_SOURCE |
| 20 | #include <limits.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | #include <common/error.h> |
| 29 | |
| 30 | #include "conf.h" |
| 31 | |
| 32 | /* |
| 33 | * config_get_file_path |
| 34 | * |
| 35 | * Returns the path with '/CONFIG_FILENAME' added to it; |
| 36 | * path will be NULL if an error occurs. |
| 37 | */ |
| 38 | char *config_get_file_path(char *path) |
| 39 | { |
| 40 | int ret; |
| 41 | char *file_path; |
| 42 | |
| 43 | ret = asprintf(&file_path, "%s/%s", path, CONFIG_FILENAME); |
| 44 | if (ret < 0) { |
| 45 | ERR("Fail allocating config file path"); |
| 46 | } |
| 47 | |
| 48 | return file_path; |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | * open_config |
| 53 | * |
| 54 | * Returns an open FILE pointer to the config file; |
| 55 | * on error, NULL is returned. |
| 56 | */ |
| 57 | static FILE *open_config(char *path, const char *mode) |
| 58 | { |
| 59 | FILE *fp = NULL; |
| 60 | char *file_path; |
| 61 | |
| 62 | file_path = config_get_file_path(path); |
| 63 | if (file_path == NULL) { |
| 64 | goto error; |
| 65 | } |
| 66 | |
| 67 | fp = fopen(file_path, mode); |
| 68 | if (fp == NULL) { |
| 69 | goto error; |
| 70 | } |
| 71 | |
| 72 | error: |
| 73 | if (file_path) { |
| 74 | free(file_path); |
| 75 | } |
| 76 | return fp; |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * create_config_file |
| 81 | * |
| 82 | * Creates the empty config file at the path. |
| 83 | * On success, returns 0; |
| 84 | * on error, returns -1. |
| 85 | */ |
| 86 | static int create_config_file(char *path) |
| 87 | { |
| 88 | int ret; |
| 89 | FILE *fp; |
| 90 | |
| 91 | fp = open_config(path, "w+"); |
| 92 | if (fp == NULL) { |
| 93 | ERR("Unable to create config file"); |
| 94 | ret = -1; |
| 95 | goto error; |
| 96 | } |
| 97 | |
| 98 | ret = fclose(fp); |
| 99 | |
| 100 | error: |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * write_config |
| 106 | * |
| 107 | * Append data to the config file in file_path |
| 108 | * On success, returns 0; |
| 109 | * on error, returns -1. |
| 110 | */ |
| 111 | static int write_config(char *file_path, size_t size, char *data) |
| 112 | { |
| 113 | FILE *fp; |
| 114 | size_t len; |
| 115 | int ret = 0; |
| 116 | |
| 117 | fp = open_config(file_path, "a"); |
| 118 | if (fp == NULL) { |
| 119 | ret = -1; |
| 120 | goto end; |
| 121 | } |
| 122 | |
| 123 | /* Write session name into config file */ |
| 124 | len = fwrite(data, size, 1, fp); |
| 125 | if (len != 1) { |
| 126 | ret = -1; |
| 127 | } |
| 128 | fclose(fp); |
| 129 | end: |
| 130 | return ret; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * config_get_default_path |
| 135 | * |
| 136 | * Returns the HOME directory path. Caller MUST NOT free(3) the return pointer. |
| 137 | */ |
| 138 | char *config_get_default_path(void) |
| 139 | { |
| 140 | return getenv("HOME"); |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * config_destroy |
| 145 | * |
| 146 | * Destroys directory config and file config. |
| 147 | */ |
| 148 | void config_destroy(char *path) |
| 149 | { |
| 150 | int ret; |
| 151 | char *config_path; |
| 152 | |
| 153 | config_path = config_get_file_path(path); |
| 154 | if (config_path == NULL) { |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | ret = remove(config_path); |
| 159 | if (ret < 0) { |
| 160 | perror("remove config file"); |
| 161 | } |
| 162 | |
| 163 | free(config_path); |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * config_read_session_name |
| 168 | * |
| 169 | * Returns the session name from the config file. |
| 170 | * The caller is responsible for freeing the returned string. |
| 171 | * On error, NULL is returned. |
| 172 | */ |
| 173 | char *config_read_session_name(char *path) |
| 174 | { |
| 175 | int ret; |
| 176 | FILE *fp; |
| 177 | char var[NAME_MAX], *session_name; |
| 178 | |
| 179 | session_name = malloc(NAME_MAX); |
| 180 | if (session_name == NULL) { |
| 181 | ERR("Out of memory"); |
| 182 | goto error; |
| 183 | } |
| 184 | |
| 185 | fp = open_config(path, "r"); |
| 186 | if (fp == NULL) { |
| 187 | ERR("Can't find valid lttng config %s/.lttngrc", path); |
| 188 | MSG("Did you create a session? (lttng create <my_session>)"); |
| 189 | goto error; |
| 190 | } |
| 191 | |
| 192 | while (!feof(fp)) { |
| 193 | if ((ret = fscanf(fp, "%[^'=']=%s\n", var, session_name)) != 2) { |
| 194 | if (ret == -1) { |
| 195 | ERR("Missing session=NAME in config file."); |
| 196 | goto error_close; |
| 197 | } |
| 198 | continue; |
| 199 | } |
| 200 | |
| 201 | if (strcmp(var, "session") == 0) { |
| 202 | goto found; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | error_close: |
| 207 | fclose(fp); |
| 208 | |
| 209 | error: |
| 210 | return NULL; |
| 211 | |
| 212 | found: |
| 213 | fclose(fp); |
| 214 | return session_name; |
| 215 | |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * config_add_session_name |
| 220 | * |
| 221 | * Write session name option to the config file. |
| 222 | * On success, returns 0; |
| 223 | * on error, returns -1. |
| 224 | */ |
| 225 | int config_add_session_name(char *path, char *name) |
| 226 | { |
| 227 | int ret; |
| 228 | char session_name[NAME_MAX]; |
| 229 | |
| 230 | /* |
| 231 | * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small; |
| 232 | * With GNU C >= 2.1, snprintf returns the required size (excluding closing null) |
| 233 | */ |
| 234 | ret = snprintf(session_name, NAME_MAX, "session=%s\n", name); |
| 235 | if ((ret < 0) || (ret >= NAME_MAX)) { |
| 236 | ret = -1; |
| 237 | goto error; |
| 238 | } |
| 239 | ret = write_config(path, ret, session_name); |
| 240 | error: |
| 241 | return ret; |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * config_init |
| 246 | * |
| 247 | * Init configuration directory and file. |
| 248 | * On success, returns 0; |
| 249 | * on error, returns -1. |
| 250 | */ |
| 251 | int config_init(char *session_name) |
| 252 | { |
| 253 | int ret; |
| 254 | char *path; |
| 255 | |
| 256 | path = config_get_default_path(); |
| 257 | if (path == NULL) { |
| 258 | ret = -1; |
| 259 | goto error; |
| 260 | } |
| 261 | |
| 262 | /* Create default config file */ |
| 263 | ret = create_config_file(path); |
| 264 | if (ret < 0) { |
| 265 | goto error; |
| 266 | } |
| 267 | |
| 268 | ret = config_add_session_name(path, session_name); |
| 269 | if (ret < 0) { |
| 270 | goto error; |
| 271 | } |
| 272 | |
| 273 | DBG("Init config session in %s", path); |
| 274 | |
| 275 | error: |
| 276 | return ret; |
| 277 | } |