57aaf7b2ddd5d726b70cf1b27a60f6f2eebcbe13
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
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 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
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.
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.
25 #include <sys/types.h>
32 * get_config_file_path
34 * Return the path with '/CONFIG_FILENAME' added to it.
36 static char *get_config_file_path(char *path
)
41 ret
= asprintf(&file_path
, "%s/%s", path
, CONFIG_FILENAME
);
43 ERR("Fail allocating config file path");
52 * Return an open FILE pointer to the config file.
54 static FILE *open_config(char *path
, const char *mode
)
59 file_path
= get_config_file_path(path
);
60 if (file_path
== NULL
) {
64 fp
= fopen(file_path
, mode
);
66 perror("config file");
80 * Create the empty config file a the path.
82 static int create_config_file(char *path
)
87 fp
= open_config(path
, "w+");
89 ERR("Unable to create config file");
103 * Create the empty config dir.
105 static int create_config_dir(char *path
)
109 /* Create session directory .lttng */
110 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
112 if (errno
== EEXIST
) {
113 ERR("Session already exist at %s", path
);
115 perror("mkdir config");
116 ERR("Couldn't init config directory at %s", path
);
129 * Append data to the config file in file_path
131 static void write_config(char *file_path
, size_t size
, char *data
)
135 fp
= open_config(file_path
, "a");
140 /* Write session name into config file */
141 fwrite(data
, size
, 1, fp
);
149 * config_get_default_path
151 * Return the HOME directory path. The output is dup so the user MUST
152 * free(3) the returned string.
154 char *config_get_default_path(void)
156 return strdup(getenv("HOME"));
162 * Destroy directory config and file config.
164 void config_destroy(char *path
)
169 config_path
= get_config_file_path(path
);
171 ret
= remove(config_path
);
173 perror("remove config file");
180 * config_read_session_name
182 * Return sesson name from the config file.
184 char *config_read_session_name(char *path
)
188 char var
[NAME_MAX
], *session_name
;
190 fp
= open_config(path
, "r");
192 ERR("Can't find valid lttng config in %s", path
);
196 session_name
= malloc(NAME_MAX
);
198 if ((ret
= fscanf(fp
, "%[^'=']=%s\n", var
, session_name
)) != 2) {
200 ERR("Missing session=NAME in config file.");
206 if (strcmp(var
, "session") == 0) {
223 * config_add_session_name
225 * Write session name option to the config file.
227 int config_add_session_name(char *path
, char *name
)
230 char session_name
[NAME_MAX
];
232 ret
= snprintf(session_name
, NAME_MAX
, "session=%s\n", name
);
237 write_config(path
, ret
, session_name
);
245 * config_generate_dir_path
247 * Return allocated path string to path/CONFIG_DIRNAME.
249 char *config_generate_dir_path(char *path
)
254 ret
= asprintf(&new_path
, "%s/%s", path
, CONFIG_DIRNAME
);
256 perror("config path problem");
267 * Init configuration directory and file.
269 int config_init(char *path
)
273 /* Create config directory (.lttng) */
274 ret
= create_config_dir(path
);
279 /* Create default config file */
280 ret
= create_config_file(path
);
285 DBG("Init config session in %s", path
);
This page took 0.035081 seconds and 4 git commands to generate.