return result;
}
- result = a->line - b->line;
-
- if(result) {
- return result;
- } else {
- return a->file ? strcmp(a->file, b->file) : 0;
- }
+ return result = a->line - b->line;
}
void init_configuration(splay_tree_t **config_tree) {
}
void free_config(config_t *cfg) {
- if(cfg->variable) {
- free(cfg->variable);
- }
-
- if(cfg->value) {
- free(cfg->value);
- }
-
- if(cfg->file) {
- free(cfg->file);
- }
-
+ free(cfg->variable);
+ free(cfg->value);
free(cfg);
}
config_t cfg, *found;
cfg.variable = variable;
- cfg.file = NULL;
cfg.line = 0;
found = splay_search_closest_greater(config_tree, &cfg);
return true;
}
- logger(NULL, MESHLINK_ERROR, "\"yes\" or \"no\" expected for configuration variable %s in %s line %d",
- cfg->variable, cfg->file, cfg->line);
+ logger(NULL, MESHLINK_ERROR, "\"yes\" or \"no\" expected for configuration variable %s in line %d",
+ cfg->variable, cfg->line);
return false;
}
return true;
}
- logger(NULL, MESHLINK_ERROR, "Integer expected for configuration variable %s in %s line %d",
- cfg->variable, cfg->file, cfg->line);
+ logger(NULL, MESHLINK_ERROR, "Integer expected for configuration variable %s in line %d",
+ cfg->variable, cfg->line);
return false;
}
return true;
}
- logger(NULL, MESHLINK_ERROR, "Hostname or IP address expected for configuration variable %s in %s line %d",
- cfg->variable, cfg->file, cfg->line);
+ logger(NULL, MESHLINK_ERROR, "Hostname or IP address expected for configuration variable %s in line %d",
+ cfg->variable, cfg->line);
return false;
}
cfg = new_config();
cfg->variable = xstrdup(variable);
cfg->value = xstrdup(value);
- cfg->file = xstrdup(fname);
cfg->line = lineno;
return cfg;
typedef struct config_t {
char *variable;
char *value;
- char *file;
int line;
} config_t;
extern bool set_config_string(config_t *, const char *);
extern bool get_config_address(const config_t *, struct addrinfo **);
-extern config_t *parse_config_line(char *, const char *, int);
-extern bool read_config_file(struct splay_tree_t *, const char *);
-extern bool write_config_file(const struct splay_tree_t *, const char *);
-
extern bool read_server_config(struct meshlink_handle *mesh);
extern bool read_host_config(struct meshlink_handle *mesh, struct splay_tree_t *, const char *);
extern bool write_host_config(struct meshlink_handle *mesh, const struct splay_tree_t *, const char *);
[MESHLINK_ESTORAGE] = "Storage error",
[MESHLINK_ENETWORK] = "Network error",
[MESHLINK_EPEER] = "Error communicating with peer",
+ [MESHLINK_ENOTSUP] = "Operation not supported",
+ [MESHLINK_EBUSY] = "MeshLink instance already in use",
};
const char *meshlink_strerror(meshlink_errno_t err) {
}
}
+ // Open the configuration file and lock it
+
+ mesh->conffile = fopen(filename, "r");
+
+ if(!mesh->conffile) {
+ logger(NULL, MESHLINK_ERROR, "Cannot not open %s: %s\n", filename, strerror(errno));
+ meshlink_close(mesh);
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return NULL;
+ }
+
+#ifdef FD_CLOEXEC
+ fcntl(fileno(mesh->conffile), F_SETFD, FD_CLOEXEC);
+#endif
+
+#ifdef HAVE_MINGW
+ // TODO: use _locking()?
+#else
+ if(flock(fileno(mesh->conffile), LOCK_EX | LOCK_NB) != 0) {
+ logger(NULL, MESHLINK_ERROR, "Cannot lock %s: %s\n", filename, strerror(errno));
+ meshlink_close(mesh);
+ meshlink_errno = MESHLINK_EBUSY;
+ return NULL;
+ }
+#endif
+
// Read the configuration
init_configuration(&mesh->config);
free(mesh->confbase);
pthread_mutex_destroy(&(mesh->mesh_mutex));
+ if(mesh->conffile) {
+ fclose(mesh->conffile);
+ }
+
memset(mesh, 0, sizeof(*mesh));
free(mesh);
/// Code of most recent error encountered.
typedef enum {
- MESHLINK_OK, ///< Everything is fine
- MESHLINK_EINVAL, ///< Invalid parameter(s) to function call
- MESHLINK_ENOMEM, ///< Out of memory
- MESHLINK_ENOENT, ///< Node is not known
- MESHLINK_EEXIST, ///< Node already exists
+ MESHLINK_OK, ///< Everything is fine
+ MESHLINK_EINVAL, ///< Invalid parameter(s) to function call
+ MESHLINK_ENOMEM, ///< Out of memory
+ MESHLINK_ENOENT, ///< Node is not known
+ MESHLINK_EEXIST, ///< Node already exists
MESHLINK_EINTERNAL, ///< MeshLink internal error
- MESHLINK_ERESOLV, ///< MeshLink could not resolve a hostname
- MESHLINK_ESTORAGE, ///< MeshLink coud not load or write data from/to disk
- MESHLINK_ENETWORK, ///< MeshLink encountered a network error
- MESHLINK_EPEER, ///< A peer caused an error
- MESHLINK_ENOTSUP, ///< The operation is not supported in the current configuration of MeshLink
+ MESHLINK_ERESOLV, ///< MeshLink could not resolve a hostname
+ MESHLINK_ESTORAGE, ///< MeshLink coud not load or write data from/to disk
+ MESHLINK_ENETWORK, ///< MeshLink encountered a network error
+ MESHLINK_EPEER, ///< A peer caused an error
+ MESHLINK_ENOTSUP, ///< The operation is not supported in the current configuration of MeshLink
+ MESHLINK_EBUSY, ///< The MeshLink instance is already in use by another process
} meshlink_errno_t;
/// Device class
dev_class_t devclass;
char *confbase;
+ FILE *conffile;
meshlink_receive_cb_t receive_cb;
meshlink_node_status_cb_t node_status_cb;
if(!check_id(name)) {
logger(mesh, MESHLINK_ERROR,
- "Invalid name for outgoing connection in %s line %d",
- cfg->file, cfg->line);
+ "Invalid name for outgoing connection in line %d",
+ cfg->line);
free(name);
continue;
}