diff options
author | An-Cheng Huang <ancheng@vyatta.com> | 2007-10-30 10:56:58 -0700 |
---|---|---|
committer | An-Cheng Huang <ancheng@vyatta.com> | 2007-10-30 10:56:58 -0700 |
commit | 653569b3768a4ebfc7d111864f3d165977db37a6 (patch) | |
tree | df4c1ee8a7d237dcdc8cad5b2984b0e60cc430bc /src/cli_new.c | |
parent | 5c170940c89065df4c7bbc6561cbdfd297363aad (diff) | |
download | vyatta-cfg-653569b3768a4ebfc7d111864f3d165977db37a6.tar.gz vyatta-cfg-653569b3768a4ebfc7d111864f3d165977db37a6.zip |
handle config locking
Diffstat (limited to 'src/cli_new.c')
-rw-r--r-- | src/cli_new.c | 46 |
1 files changed, 21 insertions, 25 deletions
diff --git a/src/cli_new.c b/src/cli_new.c index 8abdbff..52a03d3 100644 --- a/src/cli_new.c +++ b/src/cli_new.c @@ -215,35 +215,31 @@ void di(vtw_sorted *srtp) printf("%u %u\n", i, *(unsigned int *)(srtp->ptrs[i])); } -static char _lock_file_[1025] = {0}; -static int _lock_fd_=-1; +#define LOCK_FILE "/var/lock/vyatta_cfg_lock" -static void clean_lock_file(void) { - if(_lock_file_[0]) { - unlink(_lock_file_); - _lock_file_[0]=0; - } - if(_lock_fd_!=-1) { - close(_lock_fd_); - _lock_fd_=-1; - } +static void +release_config_lock() +{ + unlink(LOCK_FILE); + /* error ignored */ } -boolean get_config_lock(const char* adirp, const char* lock_name) { - - boolean ret = TRUE; - - sprintf(_lock_file_, "%s/%s", adirp, lock_name); - - _lock_fd_ = open(_lock_file_, O_WRONLY | O_CREAT | O_EXCL, 0644); - - ret = (_lock_fd_!=-1); - - if(ret) { - atexit(clean_lock_file); +int +get_config_lock() +{ + int fd = open(LOCK_FILE, O_WRONLY | O_CREAT | O_EXCL, 0660); + if (fd == -1) { + return -1; } - - return ret; + if (close(fd) == -1) { + release_config_lock(); + return -1; + } + if (atexit(release_config_lock) != 0) { + release_config_lock(); + return -1; + } + return 0; } void internal_error(int line, char *file) |