X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fconf.c;h=db91d5fb3c9b900a1d4d20d78a8b0df2a9929501;hb=325d837df2ec867c668b6a911b97e719f196ba0e;hp=af080384d4f40a588eabad1fb1b61ecbe21ea5e4;hpb=afea9096fa708b2a2e6e24c308c0f1a80eefa5cc;p=meshlink diff --git a/src/conf.c b/src/conf.c index af080384..db91d5fb 100644 --- a/src/conf.c +++ b/src/conf.c @@ -484,11 +484,13 @@ bool meshlink_confbase_exists(meshlink_handle_t *mesh) { } /// Lock the main configuration file. Creates confbase if necessary. -bool main_config_lock(meshlink_handle_t *mesh) { +bool main_config_lock(meshlink_handle_t *mesh, const char *lock_filename) { if(!mesh->confbase) { return true; } + assert(lock_filename); + if(mkdir(mesh->confbase, 0700) && errno != EEXIST) { logger(NULL, MESHLINK_ERROR, "Cannot create configuration directory %s: %s", mesh->confbase, strerror(errno)); meshlink_close(mesh); @@ -496,13 +498,10 @@ bool main_config_lock(meshlink_handle_t *mesh) { return NULL; } - char path[PATH_MAX]; - snprintf(path, sizeof(path), "%s" SLASH "meshlink.lock", mesh->confbase); - - mesh->lockfile = fopen(path, "w+"); + mesh->lockfile = fopen(lock_filename, "w+"); if(!mesh->lockfile) { - logger(NULL, MESHLINK_ERROR, "Cannot not open %s: %s\n", path, strerror(errno)); + logger(NULL, MESHLINK_ERROR, "Cannot not open %s: %s\n", lock_filename, strerror(errno)); meshlink_errno = MESHLINK_ESTORAGE; return false; } @@ -516,7 +515,7 @@ bool main_config_lock(meshlink_handle_t *mesh) { #else if(flock(fileno(mesh->lockfile), LOCK_EX | LOCK_NB) != 0) { - logger(NULL, MESHLINK_ERROR, "Cannot lock %s: %s\n", path, strerror(errno)); + logger(NULL, MESHLINK_ERROR, "Cannot lock %s: %s\n", lock_filename, strerror(errno)); fclose(mesh->lockfile); mesh->lockfile = NULL; meshlink_errno = MESHLINK_EBUSY;