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");
178 perror("rmdir config dir");
185 * config_read_session_name
187 * Return sesson name from the config file.
189 char *config_read_session_name(char *path
)
193 char var
[NAME_MAX
], *session_name
;
195 fp
= open_config(path
, "r");
197 ERR("Can't find valid lttng config in %s", path
);
201 session_name
= malloc(NAME_MAX
);
203 if ((ret
= fscanf(fp
, "%[^'=']=%s\n", var
, session_name
)) != 2) {
205 ERR("Missing session=NAME in config file.");
211 if (strcmp(var
, "session") == 0) {
228 * config_add_session_name
230 * Write session name option to the config file.
232 int config_add_session_name(char *path
, char *name
)
235 char session_name
[NAME_MAX
];
237 ret
= snprintf(session_name
, NAME_MAX
, "session=%s\n", name
);
242 write_config(path
, ret
, session_name
);
250 * config_generate_dir_path
252 * Return allocated path string to path/CONFIG_DIRNAME.
254 char *config_generate_dir_path(char *path
)
259 ret
= asprintf(&new_path
, "%s/%s", path
, CONFIG_DIRNAME
);
261 perror("config path problem");
272 * Init configuration directory and file.
274 int config_init(char *path
)
278 /* Create config directory (.lttng) */
279 ret
= create_config_dir(path
);
284 /* Create default config file */
285 ret
= create_config_file(path
);
290 DBG("Init config session in %s", path
);
This page took 0.040968 seconds and 5 git commands to generate.