| 1 | /* |
| 2 | * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef _CONFIG_H |
| 9 | #define _CONFIG_H |
| 10 | |
| 11 | #include <common/config/ini.h> |
| 12 | #include <common/config/config-session-abi.h> |
| 13 | #include <common/macros.h> |
| 14 | #include <stdint.h> |
| 15 | |
| 16 | struct config_entry { |
| 17 | /* section is NULL if the entry is not in a section */ |
| 18 | const char *section; |
| 19 | const char *name; |
| 20 | const char *value; |
| 21 | }; |
| 22 | |
| 23 | struct config_load_session_override_attr { |
| 24 | char *path_url; |
| 25 | char *ctrl_url; |
| 26 | char *data_url; |
| 27 | char *session_name; |
| 28 | }; |
| 29 | |
| 30 | /* Instance of a configuration writer. */ |
| 31 | struct config_writer; |
| 32 | |
| 33 | /* |
| 34 | * A config_entry_handler_cb receives config_entry structures belonging to the |
| 35 | * sections the handler has been registered to. |
| 36 | * |
| 37 | * The config_entry and its members are only valid for the duration of the call |
| 38 | * and must not be freed. |
| 39 | * |
| 40 | * config_entry_handler_cb may return negative value to indicate an error in |
| 41 | * the configuration file. |
| 42 | */ |
| 43 | typedef int (*config_entry_handler_cb)(const struct config_entry *, void *); |
| 44 | |
| 45 | /* |
| 46 | * Read a section's entries in an INI configuration file. |
| 47 | * |
| 48 | * path may be NULL, in which case the following paths will be tried: |
| 49 | * 1) $HOME/.lttng/lttng.conf |
| 50 | * 2) /etc/lttng/lttng.conf |
| 51 | * |
| 52 | * handler will only be called with entries belonging to the provided section. |
| 53 | * If section is NULL, all entries will be relayed to handler. If section is |
| 54 | * "", only the global entries are relayed. |
| 55 | * |
| 56 | * Returns 0 on success. Negative values are error codes. If the return value |
| 57 | * is positive, it represents the line number on which a parsing error occurred. |
| 58 | */ |
| 59 | LTTNG_HIDDEN |
| 60 | int config_get_section_entries(const char *path, const char *section, |
| 61 | config_entry_handler_cb handler, void *user_data); |
| 62 | |
| 63 | /* |
| 64 | * Parse a configuration value. |
| 65 | * |
| 66 | * This function expects either an unsigned integer or a boolean text option. |
| 67 | * The following strings are recognized: true, yes, on, false, no and off. |
| 68 | * |
| 69 | * Returns either the value of the parsed integer, or 0/1 if a boolean text |
| 70 | * string was recognized. Negative values indicate an error. |
| 71 | */ |
| 72 | LTTNG_HIDDEN |
| 73 | int config_parse_value(const char *value); |
| 74 | |
| 75 | /* |
| 76 | * Create an instance of a configuration writer. |
| 77 | * |
| 78 | * fd_output File to which the XML content must be written. fd_output is |
| 79 | * owned by the caller. |
| 80 | * |
| 81 | * indent If other than 0 the XML will be pretty printed |
| 82 | * with indentation and newline. |
| 83 | * |
| 84 | * Returns an instance of a configuration writer on success, NULL on |
| 85 | * error. |
| 86 | */ |
| 87 | LTTNG_HIDDEN |
| 88 | struct config_writer *config_writer_create(int fd_output, int indent); |
| 89 | |
| 90 | /* |
| 91 | * Destroy an instance of a configuration writer. |
| 92 | * |
| 93 | * writer An instance of a configuration writer. |
| 94 | * |
| 95 | * Returns zero if the XML document could be closed cleanly. Negative values |
| 96 | * indicate an error. |
| 97 | */ |
| 98 | LTTNG_HIDDEN |
| 99 | int config_writer_destroy(struct config_writer *writer); |
| 100 | |
| 101 | /* |
| 102 | * Open an element tag. |
| 103 | * |
| 104 | * writer An instance of a configuration writer. |
| 105 | * |
| 106 | * element_name Element tag name. |
| 107 | * |
| 108 | * Returns zero if the XML element could be opened. |
| 109 | * Negative values indicate an error. |
| 110 | */ |
| 111 | LTTNG_HIDDEN |
| 112 | int config_writer_open_element(struct config_writer *writer, |
| 113 | const char *element_name); |
| 114 | |
| 115 | /* |
| 116 | * Write an element tag attribute. |
| 117 | * |
| 118 | * writer An instance of a configuration writer. |
| 119 | * |
| 120 | * name Attribute name. |
| 121 | * |
| 122 | * Returns zero if the XML element's attribute could be written. |
| 123 | * Negative values indicate an error. |
| 124 | */ |
| 125 | LTTNG_HIDDEN |
| 126 | int config_writer_write_attribute(struct config_writer *writer, |
| 127 | const char *name, const char *value); |
| 128 | |
| 129 | /* |
| 130 | * Close the current element tag. |
| 131 | * |
| 132 | * writer An instance of a configuration writer. |
| 133 | * |
| 134 | * Returns zero if the XML document could be closed cleanly. |
| 135 | * Negative values indicate an error. |
| 136 | */ |
| 137 | LTTNG_HIDDEN |
| 138 | int config_writer_close_element(struct config_writer *writer); |
| 139 | |
| 140 | /* |
| 141 | * Write an element of type unsigned int. |
| 142 | * |
| 143 | * writer An instance of a configuration writer. |
| 144 | * |
| 145 | * element_name Element name. |
| 146 | * |
| 147 | * value Unsigned int value of the element |
| 148 | * |
| 149 | * Returns zero if the element's value could be written. |
| 150 | * Negative values indicate an error. |
| 151 | */ |
| 152 | LTTNG_HIDDEN |
| 153 | int config_writer_write_element_unsigned_int(struct config_writer *writer, |
| 154 | const char *element_name, uint64_t value); |
| 155 | |
| 156 | /* |
| 157 | * Write an element of type signed int. |
| 158 | * |
| 159 | * writer An instance of a configuration writer. |
| 160 | * |
| 161 | * element_name Element name. |
| 162 | * |
| 163 | * value Signed int value of the element |
| 164 | * |
| 165 | * Returns zero if the element's value could be written. |
| 166 | * Negative values indicate an error. |
| 167 | */LTTNG_HIDDEN |
| 168 | int config_writer_write_element_signed_int(struct config_writer *writer, |
| 169 | const char *element_name, int64_t value); |
| 170 | |
| 171 | /* |
| 172 | * Write an element of type boolean. |
| 173 | * |
| 174 | * writer An instance of a configuration writer. |
| 175 | * |
| 176 | * element_name Element name. |
| 177 | * |
| 178 | * value Boolean value of the element |
| 179 | * |
| 180 | * Returns zero if the element's value could be written. |
| 181 | * Negative values indicate an error. |
| 182 | */ |
| 183 | LTTNG_HIDDEN |
| 184 | int config_writer_write_element_bool(struct config_writer *writer, |
| 185 | const char *element_name, int value); |
| 186 | |
| 187 | /* |
| 188 | * Write an element of type string. |
| 189 | * |
| 190 | * writer An instance of a configuration writer. |
| 191 | * |
| 192 | * element_name Element name. |
| 193 | * |
| 194 | * value String value of the element |
| 195 | * |
| 196 | * Returns zero if the element's value could be written. |
| 197 | * Negative values indicate an error. |
| 198 | */ |
| 199 | LTTNG_HIDDEN |
| 200 | int config_writer_write_element_string(struct config_writer *writer, |
| 201 | const char *element_name, const char *value); |
| 202 | |
| 203 | /* |
| 204 | * Write an element of type double. |
| 205 | * |
| 206 | * writer An instance of a configuration writer. |
| 207 | * |
| 208 | * element_name Element name. |
| 209 | * |
| 210 | * value Double value of the element |
| 211 | * |
| 212 | * Returns zero if the element's value could be written. |
| 213 | * Negative values indicate an error. |
| 214 | */ |
| 215 | LTTNG_HIDDEN |
| 216 | int config_writer_write_element_double(struct config_writer *writer, |
| 217 | const char *element_name, |
| 218 | double value); |
| 219 | |
| 220 | /* |
| 221 | * Load session configurations from a file. |
| 222 | * |
| 223 | * path Path to an LTTng session configuration file. All *.lttng files |
| 224 | * will be loaded if path is a directory. If path is NULL, the default |
| 225 | * paths will be searched in the following order: |
| 226 | * 1) $HOME/.lttng/sessions |
| 227 | * 2) /etc/lttng/sessions |
| 228 | * |
| 229 | * session_name Name of the session to load. Will load all |
| 230 | * sessions from path if NULL. |
| 231 | * |
| 232 | * overwrite Overwrite current session configuration if it exists. |
| 233 | * autoload Tell to load the auto session(s). |
| 234 | * overrides The override attribute structure specifying override parameters. |
| 235 | * |
| 236 | * Returns zero if the session could be loaded successfully. Returns |
| 237 | * a negative LTTNG_ERR code on error. |
| 238 | */ |
| 239 | LTTNG_HIDDEN |
| 240 | int config_load_session(const char *path, const char *session_name, |
| 241 | int overwrite, unsigned int autoload, |
| 242 | const struct config_load_session_override_attr *overrides); |
| 243 | |
| 244 | #endif /* _CONFIG_H */ |