summaryrefslogtreecommitdiff
path: root/src/cli_new.c
diff options
context:
space:
mode:
authorAn-Cheng Huang <ancheng@vyatta.com>2007-12-13 18:35:06 -0800
committerAn-Cheng Huang <ancheng@vyatta.com>2007-12-13 18:35:06 -0800
commit8d6714e356bf64016f5be55f5c4b9bb0a71b5a3c (patch)
treececd2c55cf2e5c58680cc65ab5e2a3997d03e125 /src/cli_new.c
parent601785f01f89ccac4cae7587bafc4958888b8db1 (diff)
downloadvyatta-cfg-8d6714e356bf64016f5be55f5c4b9bb0a71b5a3c.tar.gz
vyatta-cfg-8d6714e356bf64016f5be55f5c4b9bb0a71b5a3c.zip
put pid in config lock file
Diffstat (limited to 'src/cli_new.c')
-rw-r--r--src/cli_new.c47
1 files changed, 36 insertions, 11 deletions
diff --git a/src/cli_new.c b/src/cli_new.c
index d4380d5..3f64489 100644
--- a/src/cli_new.c
+++ b/src/cli_new.c
@@ -240,19 +240,44 @@ release_config_lock()
int
get_config_lock()
{
- int fd = open(LOCK_FILE, O_WRONLY | O_CREAT | O_EXCL, 0660);
- if (fd == -1) {
- return -1;
- }
- if (close(fd) == -1) {
- release_config_lock();
- return -1;
- }
- if (atexit(release_config_lock) != 0) {
+ int fd = -1;
+ FILE *lfile = NULL;
+ int ret = -1;
+
+ do {
+ /* create lock file */
+ fd = open(LOCK_FILE, O_WRONLY | O_CREAT | O_EXCL, 0660);
+ if (fd == -1) {
+ break;
+ }
+
+ /* write pid into lock file */
+ if ((lfile = fdopen(fd, "w")) == NULL) {
+ break;
+ }
+ if (fprintf(lfile, "%u", getpid()) < 0) {
+ break;
+ }
+ /* fclose also closes fd */
+ if (fclose(lfile) != 0) {
+ break;
+ }
+ /* clean up on exit */
+ if (atexit(release_config_lock) != 0) {
+ break;
+ }
+ ret = 0;
+ } while (0);
+
+ if (ret == -1) {
+ if (lfile) {
+ fclose(lfile);
+ } else if (fd != -1) {
+ close(fd);
+ }
release_config_lock();
- return -1;
}
- return 0;
+ return ret;
}
void internal_error(int line, char *file)