summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/pid_output.c92
-rw-r--r--lib/vector.c17
-rw-r--r--lib/vty.c24
-rw-r--r--tests/test-vector.c296
4 files changed, 330 insertions, 99 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
diff --git a/lib/vty.c b/lib/vty.c
index 2a936170..e6dcd6a8 100644
--- a/lib/vty.c
+++ b/lib/vty.c
@@ -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) ;
diff --git a/tests/test-vector.c b/tests/test-vector.c
index 7d2121cc..109f9004 100644
--- a/tests/test-vector.c
+++ b/tests/test-vector.c
@@ -29,7 +29,8 @@ int sort_cmp(const void* const* a, const void* const* b);
void test_vector_sort(void);
void test_vector_bsearch(void);
void test_vector_move_item_here(void);
-void do_test_move_item_here(const int rider);
+void do_test_move_item_here(const int rider, vector_index ins, vector_index src,
+ char strings[][10], const vector_index len) ;
void test_vector_part_reverse(void);
void test_vector_copy_here(void);
void test_vector_move_here(void);
@@ -605,104 +606,275 @@ test_vector_bsearch(void)
void
test_vector_move_item_here(void)
{
- do_test_move_item_here(-1);
- do_test_move_item_here(0);
- do_test_move_item_here(1);
+ const uint n = 20 ;
+ const uint l = n - 1 ;
+ char strings[n][10] ;
+ uint i ;
+
+ for (i = 0 ; i < n ; ++i)
+ sprintf(strings[i], "item=%2u", i) ;
+
+ do_test_move_item_here(-1, 8, 16, strings, n);
+ do_test_move_item_here( 0, 8, 16, strings, n);
+ do_test_move_item_here(+1, 8, 16, strings, n);
+
+ do_test_move_item_here(-1, 16, 8, strings, n);
+ do_test_move_item_here( 0, 16, 8, strings, n);
+ do_test_move_item_here(+1, 16, 8, strings, n);
+
+ do_test_move_item_here(-1, 9, 9, strings, n);
+ do_test_move_item_here( 0, 9, 9, strings, n);
+ do_test_move_item_here(+1, 9, 9, strings, n);
+
+ do_test_move_item_here(-1, 10, 9, strings, n);
+ do_test_move_item_here( 0, 10, 9, strings, n);
+ do_test_move_item_here(+1, 10, 9, strings, n);
+
+ do_test_move_item_here(-1, 9, 10, strings, n);
+ do_test_move_item_here( 0, 9, 10, strings, n);
+ do_test_move_item_here(+1, 9, 10, strings, n);
+
+ do_test_move_item_here(-1, 11, 9, strings, n);
+ do_test_move_item_here( 0, 11, 9, strings, n);
+ do_test_move_item_here(+1, 11, 9, strings, n);
+
+ do_test_move_item_here(-1, 9, 11, strings, n);
+ do_test_move_item_here( 0, 9, 11, strings, n);
+ do_test_move_item_here(+1, 9, 11, strings, n);
+
+ do_test_move_item_here(-1, 0, 10, strings, n);
+ do_test_move_item_here( 0, 0, 10, strings, n);
+ do_test_move_item_here(+1, 0, 10, strings, n);
+
+ do_test_move_item_here(-1, 10, 0, strings, n);
+ do_test_move_item_here( 0, 10, 0, strings, n);
+ do_test_move_item_here(+1, 10, 0, strings, n);
+
+ do_test_move_item_here(-1, 0, l, strings, n);
+ do_test_move_item_here( 0, 0, l, strings, n);
+ do_test_move_item_here(+1, 0, l, strings, n);
+
+ do_test_move_item_here(-1, l, 0, strings, n);
+ do_test_move_item_here( 0, l, 0, strings, n);
+ do_test_move_item_here(+1, l, 0, strings, n);
+
+ do_test_move_item_here(-1, 10, l, strings, n);
+ do_test_move_item_here( 0, 10, l, strings, n);
+ do_test_move_item_here(+1, 10, l, strings, n);
+
+ do_test_move_item_here(-1, l, 10, strings, n);
+ do_test_move_item_here( 0, l, 10, strings, n);
+ do_test_move_item_here(+1, l, 10, strings, n);
+
+ do_test_move_item_here(-1, 0, 0, strings, n);
+ do_test_move_item_here( 0, 0, 0, strings, n);
+ do_test_move_item_here(+1, 0, 0, strings, n);
+
+ do_test_move_item_here(-1, 1, 1, strings, n);
+ do_test_move_item_here( 0, 1, 1, strings, n);
+ do_test_move_item_here(+1, 1, 1, strings, n);
+
+ do_test_move_item_here(-1, 0, 1, strings, n);
+ do_test_move_item_here( 0, 0, 1, strings, n);
+ do_test_move_item_here(+1, 0, 1, strings, n);
+
+ do_test_move_item_here(-1, 1, 0, strings, n);
+ do_test_move_item_here( 0, 1, 0, strings, n);
+ do_test_move_item_here(+1, 1, 0, strings, n);
+
+ do_test_move_item_here(-1, 0, 2, strings, n);
+ do_test_move_item_here( 0, 0, 2, strings, n);
+ do_test_move_item_here(+1, 0, 2, strings, n);
+
+ do_test_move_item_here(-1, 2, 0, strings, n);
+ do_test_move_item_here( 0, 2, 0, strings, n);
+ do_test_move_item_here(+1, 2, 0, strings, n);
+
+ do_test_move_item_here(-1, l, l, strings, n);
+ do_test_move_item_here( 0, l, l, strings, n);
+ do_test_move_item_here(+1, l, l, strings, n);
+
+ do_test_move_item_here(-1,l-1, l, strings, n);
+ do_test_move_item_here( 0,l-1, l, strings, n);
+ do_test_move_item_here(+1,l-1, l, strings, n);
+
+ do_test_move_item_here(-1, l,l-1, strings, n);
+ do_test_move_item_here( 0, l,l-1, strings, n);
+ do_test_move_item_here(+1, l,l-1, strings, n);
}
void
-do_test_move_item_here(const int rider)
+do_test_move_item_here(const int rider, vector_index ins, vector_index src,
+ char strings[][10], const vector_index len)
{
vector v = NULL;
- const vector_length_t len = 100;
- const vector_index_t ins = 50;
- const vector_index_t src = 70;
- vector_index_t i;
- char buf[10];
- vector_index_t check_dest = 0;
- vector_index_t check_src = 0;
- vector_index_t check_end = 0;
- int check_shift = 0;
- p_vector_item dest_item = NULL;
+ vector_index i, e, check_end ;
+ vector_index hi, lo ;
+ char* expect[len] ;
+ p_vector_item dest_item = NULL;
switch(rider)
{
case -1:
- printf("test_vector_move_here before\n");
- check_dest = ins;
- check_src = src;
- check_end = len;
- check_shift = 1;
+ printf("test_vector_move_here before dst=%2d, src=%2d\n", src, ins);
break;
+
case 0:
- printf("test_vector_move_here at\n");
- check_dest = ins;
- check_src = src - 1;
- check_end = len - 1;
- check_shift = 0;
+ printf("test_vector_move_here at dst=%2d, src=%2d\n", src, ins);
+ --check_end ;
break;
+
case 1:
- printf("test_vector_move_here after\n");
- check_dest = ins + 1;
- check_src = src;
- check_end = len;
- check_shift = 1;
+ printf("test_vector_move_here after dst=%2d, src=%2d\n", src, ins);
break;
- }
+ } ;
+
+ /* Build the test vector and perform action */
v = vector_init_new(v, 0);
for (i = 0; i < len; ++i)
- {
- sprintf(buf, "%u", i);
- vector_set_item(v, i, strdup(buf));
- }
+ vector_set_item(v, i, strdup(strings[i]));
dest_item = vector_get_item(v, ins); /* item to free if rider == 0 */
vector_move_item_here(v, ins, rider, src);
- assert_true(vector_end(v) == check_end, "vector_end(v) != check_end");
- /* check contents are correct */
+ /* Build the expected result. */
- /* from start to insertion point */
- for (i = 0; i < check_dest - 1; ++i)
+ if (ins <= src)
{
- sprintf(buf, "%u", i);
- assert_true(strcmp(vector_get_item(v, i), buf) == 0,
- "vector_get_item(v, i) != buf");
+ lo = ins ;
+ hi = src ;
}
+ else
+ {
+ lo = src ;
+ hi = ins ;
+ } ;
- /* at insertion point */
- sprintf(buf, "%u", src);
- assert_true(strcmp(vector_get_item(v, check_dest), buf) == 0,
- "vector_get_item(v, check_dest) != buf");
+ check_end = (rider != 0) ? len : len - 1 ;
+ i = 0 ;
+ e = 0 ;
- /* from insertion point to src */
- for (i = check_dest + 1; i <= check_src; ++i)
+ while (i < lo)
+ expect[i++] = strings[e++] ;
+
+ if (lo == hi)
{
- sprintf(buf, "%u", i - check_shift);
- assert_true(strcmp(vector_get_item(v, i), buf) == 0, "vector_get_item(v, i) != buf");
+ /* Special case -- do nothing if rider != 0
+ * drop entry if rider == 0
+ */
+ if (rider == 0)
+ ++e ;
}
-
- /* from src to end */
- for (i = check_src + 1; i < check_end; ++i)
+ else if (lo == (hi - 1))
{
- sprintf(buf, "%u", i - (check_shift - 1));
- assert_true(strcmp(vector_get_item(v, i), buf) == 0, "vector_get_item(v, i) != buf");
+ /* Special case -- ins and src next to each other !
+ *
+ * If rider != 0 -- insert ins and src in the required order
+ * If rider == 0 -- insert just src
+ */
+ if (rider < 0)
+ {
+ expect[i++] = strings[src] ;
+ expect[i++] = strings[ins] ;
+ }
+ else if (rider == 0)
+ {
+ expect[i++] = strings[src] ;
+ }
+ else
+ {
+ expect[i++] = strings[ins] ;
+ expect[i++] = strings[src] ;
+ } ;
+ e += 2 ;
}
+ else
+ {
+ /* Now we know that ins and src are separated by at least 1 entry.
+ */
+ uint be ;
+
+ be = hi - 1 ;
+
+ if (ins < src)
+ {
+ /* At insertion point, so insert ins and src in the required order
+ * or insert juist the src.
+ */
+ if (rider < 0)
+ {
+ expect[i++] = strings[src] ;
+ expect[i++] = strings[ins] ;
+ ++be ;
+ }
+ else if (rider == 0)
+ {
+ expect[i++] = strings[src] ;
+ }
+ else
+ {
+ expect[i++] = strings[ins] ;
+ expect[i++] = strings[src] ;
+ ++be ;
+ } ;
+
+ ++be ;
+ } ;
+
+ e += 1 ;
+
+ while (i < be)
+ expect[i++] = strings[e++] ;
+
+ if (ins > src)
+ {
+ /* At insertion point, so insert ins and src in the required order
+ * or insert juist the src.
+ */
+ if (rider < 0)
+ {
+ expect[i++] = strings[src] ;
+ expect[i++] = strings[ins] ;
+ }
+ else if (rider == 0)
+ {
+ expect[i++] = strings[src] ;
+ }
+ else
+ {
+ expect[i++] = strings[ins] ;
+ expect[i++] = strings[src] ;
+ } ;
+ } ;
+
+ e = hi + 1 ;
+ } ;
+
+ while (i < check_end)
+ expect[i++] = strings[e++] ;
+
+ /* check contents are correct */
+ assert_true(vector_end(v) == check_end, "vector_end(v) != check_end");
- /* free contents */
- for (i = 0; i < vector_end(v); ++i)
+ for (i = 0 ; i < check_end ; ++i)
{
- free(vector_slot(v, i));
- }
+ if (strcmp(vector_get_item(v, i), expect[i]) != 0)
+ {
+ assert_true(0, "vector_get_item(v, i) != expected") ;
+ break ;
+ } ;
+ } ;
+
+ /* free contents */
+ for (i = 0; i < vector_end(v); ++i)
+ free(vector_slot(v, i));
if (rider == 0)
- {
- free(dest_item);
- }
+ free(dest_item);
vector_free(v);
}