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