diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pid_output.c | 92 | ||||
-rw-r--r-- | lib/vector.c | 17 | ||||
-rw-r--r-- | lib/vty.c | 24 |
3 files changed, 96 insertions, 37 deletions
diff --git a/lib/pid_output.c b/lib/pid_output.c index 17081dad..df1862bb 100644 --- a/lib/pid_output.c +++ b/lib/pid_output.c @@ -59,50 +59,74 @@ pid_output (const char *path) pid_t pid_output (const char *path) { - int tmp; - int fd; - pid_t pid; - char buf[16]; - struct flock lock; - mode_t oldumask; + const char* fail ; + int err ; + + pid_t pid ; + mode_t oldumask ; + int fd ; + struct flock lock ; + char buf[32] ; + size_t pidsize ; pid = getpid (); oldumask = umask(0777 & ~PIDFILE_MASK); - fd = open (path, O_RDWR | O_CREAT, PIDFILE_MASK); + + fail = "Failed to open pid lock file '%s' for pid %d (%s)" ; + fd = open (path, O_RDWR | O_CREAT, PIDFILE_MASK) ; + err = errno ; + + umask(oldumask); + if (fd < 0) - { - zlog_err("Can't create pid lock file %s (%s), exiting", - path, errtoa(errno, 0).str); - umask(oldumask); - exit(1); - } - else - { - size_t pidsize; + goto failed_err ; - umask(oldumask); - memset (&lock, 0, sizeof(lock)); + memset (&lock, 0, sizeof(lock)); + lock.l_type = F_WRLCK; + lock.l_whence = SEEK_SET; - lock.l_type = F_WRLCK; - lock.l_whence = SEEK_SET; + if (fcntl(fd, F_SETLK, &lock) < 0) + { + fail = "Failed to write lock pid lock file '%s' for pid %d (%s)" ; + err = errno ; - if (fcntl(fd, F_SETLK, &lock) < 0) + if ((err == EACCES) || (err == EAGAIN)) { - zlog_err("Could not lock pid_file %s, exiting", path); - exit(1); - } - - sprintf (buf, "%d\n", (int) pid); - pidsize = strlen(buf); - if ((tmp = write (fd, buf, pidsize)) != (int)pidsize) - zlog_err("Could not write pid %d to pid_file %s, rc was %d: %s", - (int)pid,path,tmp, errtoa(errno, 0).str); - else if (ftruncate(fd, pidsize) < 0) - zlog_err("Could not truncate pid_file %s to %u bytes: %s", - path,(u_int)pidsize, errtoa(errno, 0).str); - } + fail = "Failed to write lock pid lock file '%s', " + "blocked by pid %d (%s)" ; + fcntl(fd, F_GETLK, &lock) ; + pid = lock.l_pid ; + } ; + + goto failed_err ; + } ; + + pidsize = sprintf (buf, "%d\n", (int)pid) ; + + fail = "Failed to write pid to pid lock file '%s' for pid %d (%s)" ; + if (write(fd, buf, pidsize) != (ssize_t)pidsize) + goto failed ; + + fail = "Failed to truncate pid lock file '%s' to length for pid %d (%s)" ; + if (ftruncate(fd, pidsize) < 0) + goto failed ; + + fail = "Failed to fsync pid lock file '%s' for pid %d (%s)" ; + if (fsync(fd) < 0) + goto failed ; + return pid; + +failed: + err = errno ; + +failed_err: + zlog_err(fail, path, (int)pid, errtoa(err, 0).str) ; + fprintf(stderr, fail, path, (int)pid, errtoa(err, 0).str) ; + fprintf(stderr, "\n") ; + + exit(1) ; } #endif /* HAVE_FCNTL */ diff --git a/lib/vector.c b/lib/vector.c index cd81191f..74f61913 100644 --- a/lib/vector.c +++ b/lib/vector.c @@ -473,11 +473,13 @@ vector_move_item(vector v, vector_index_t i_dst, vector_index_t i_src) * * rider < 0 -- move to before the item at the given position * rider == 0 -- move to replace item at the given position - * rider > 0 -- insert after the item at the given position + * rider > 0 -- move to after the item at the given position * * NB: it is the caller's responsibility to release the any existing value * that will be replaced. * + * NB: replacing an item by itself (rider == 0, i_dst == i_src) deletes it ! + * * Move items and extend vector as required. */ extern void @@ -486,8 +488,17 @@ vector_move_item_here(vector v, vector_index_t i_dst, int rider, { if (rider != 0) { - if (rider > 0) - ++i_dst ; + if (i_src < i_dst) + { + if (rider < 0) + --i_dst ; /* moving up places src after dst */ + } + else if (i_src > i_dst) + { + if (rider > 0) + ++i_dst ; /* moving down places src before dst */ + } ; + vector_move_item(v, i_dst, i_src) ; } else @@ -984,6 +984,30 @@ vty_read_config_file (int fd, const char* name, cmd_command first_cmd, vty vty ; qtime_t taking ; + /* Set up configuration file reader VTY -- which buffers all output */ + vty = vty_open(VTY_CONFIG_READ); + vty->node = CONFIG_NODE; + + /* Make sure we have a suitable buffer, and set vty->buf to point at + * it -- same like other command execution. + */ + qs_need(&vty->vio->clx, VTY_BUFSIZ) ; + vty->buf = qs_chars(&vty->vio->clx) ; + + taking = qt_get_monotonic() ; + + /* Execute configuration file */ + ret = config_from_file (vty, confp, first_cmd, &vty->vio->clx, + ignore_warnings) ; + + taking = (qt_get_monotonic() - taking) / (QTIME_SECOND / 1000) ; + + zlog_info("Finished reading configuration in %d.%dsecs%s", + (int)(taking / 1000), (int)(taking % 1000), + (ret == CMD_SUCCESS) ? "." : " -- FAILED") ; + + VTY_LOCK() ; + vty = vty_config_read_open(fd, name, full_lex) ; vty_cmd_loop_prepare(vty) ; |