lib/fileutils.c
# lib/test_* binaries
-noinst_PROGRAMS = lib/test_strutils lib/test_fileutils lib/test_nsutils
+noinst_PROGRAMS = \
+ lib/test_strutils \
+ lib/test_fileutils \
+ lib/test_nsutils \
+ lib/test_process
lib_test_strutils_SOURCES = lib/test_strutils.c lib/strutils.c
lib_test_strutils_LDADD =
.dirstamp
test_fileutils
+test_process
test_strutils
test_nsutils
--- /dev/null
+/*
+ * test_procps -- program to create a process to test procps
+ *
+ * Copyright 2015 Craig Small <csmall@enc.com.au>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <signal.h>
+#include <sys/prctl.h>
+#include "c.h"
+
+#define DEFAULT_SLEEPTIME 300
+#define MY_NAME "spcorp"
+
+static void usage(void)
+{
+ fprintf(stderr, " %s [options]\n", program_invocation_short_name);
+ fprintf(stderr, " -s <seconds>\n");
+ exit(EXIT_FAILURE);
+}
+
+
+void
+signal_handler(int signum)
+{
+ switch(signum) {
+ case SIGUSR1:
+ printf("SIG SIGUSR1\n");
+ break;
+ case SIGUSR2:
+ printf("SIG SIGUSR2\n");
+ break;
+ default:
+ printf("SIG unknown\n");
+ exit(EXIT_FAILURE);
+ }
+}
+
+int main(int argc, char *argv[])
+{
+ int sleep_time, opt;
+ struct sigaction signal_action;
+
+ sleep_time = DEFAULT_SLEEPTIME;
+ while ((opt = getopt(argc, argv, "s:")) != -1) {
+ switch(opt) {
+ case 's':
+ sleep_time = atoi(optarg);
+ if (sleep_time < 1) {
+ fprintf(stderr, "sleep time must be 1 second or more\n");
+ usage();
+ }
+ break;
+ default:
+ usage();
+ }
+ }
+
+ /* Setup signal handling */
+ signal_action.sa_handler = signal_handler;
+ sigemptyset (&signal_action.sa_mask);
+ signal_action.sa_flags = 0;
+ sigaction(SIGUSR1, &signal_action, NULL);
+ sigaction(SIGUSR2, &signal_action, NULL);
+
+ /* set process name */
+ prctl(PR_SET_NAME, MY_NAME, NULL, NULL, NULL);
+
+ while (sleep_time > 0) {
+ sleep_time = sleep(sleep_time);
+ }
+ return EXIT_SUCCESS;
+}
}
proc make_testproc { } {
- global testproc_path testproc_comm testproc1_pid testproc2_pid sleep_time
+ global testproc_path testproc_comm testproc1_pid testproc2_pid topdir
- # Time to run the whole job
- set sleep_time 300
+ set testproc_realpath "${topdir}/lib/.libs/test_process"
+ set testproc_comm "spcorp"
set testproc_path [ exec mktemp -u ]
- set sleep_path [ exec sh -c "command -v sleep" ]
- exec ln -s $sleep_path $testproc_path
- set testproc_comm [ exec basename $testproc_path ]
+ exec ln -s $testproc_realpath $testproc_path
spawn readlink $testproc_path
expect {
- -re "^$sleep_path\\s*$" { }
- timeout { perror "test proc does not link to sleep 1" }
- eof { perror "test proc does not link to sleep 1" }
+ -re "^$testproc_realpath\\s*$" { }
+ timeout { perror "test proc does not link to test process" }
+ eof { perror "test proc does not link to test process" }
}
- set testproc1_pid [ exec $testproc_path $sleep_time & ]
- set testproc2_pid [ exec $testproc_path $sleep_time & ]
+
+ set testproc1_pid [ exec $testproc_path & ]
+ set testproc2_pid [ exec $testproc_path & ]
}
proc kill_testproc { } {
expect_pass "$test" "^$testproc1_pid\\s+$testproc2_pid\\s*$"
# In Debian only
-#set test "pgrep counts 2 test pids"
-#spawn $pgrep -c $testproc_comm
-#expect_pass "$test" "^2\\s*"
+set test "pgrep counts 2 test pids"
+spawn $pgrep -c $testproc_comm
+expect_pass "$test" "^2\\s*"
set test "pgrep with : delimiter"
spawn $pgrep -d : $testproc_comm
set test "pgrep with full command line"
spawn $pgrep -a $testproc_comm
-expect_pass "$test" "^$testproc1_pid\\s+$testproc_path\\s+$sleep_time\\s+$testproc2_pid\\s+$testproc_path\\s+$sleep_time\\s*$"
+expect_pass "$test" "^$testproc1_pid\\s+$testproc_path\\s+$testproc2_pid\\s+$testproc_path\\s*$"
set test "pgrep find newest test pid"
spawn $pgrep -n $testproc_comm
spawn $pkill -e $testproc_comm -0
expect_pass "$test" "^$testproc_comm killed \\(pid $testproc1_pid\\)\\s+$testproc_comm killed \\(pid $testproc2_pid\\)\\s*$"
-# In Debian only
-#set test "pkill counts 2 test pids"
-#spawn $pkill -c $testproc
-#expect_pass "$test" "^2\\s*"
-
-set test "pkill with matching gid"
-#spawn $pkill -G $gid $testproc
-#expect_pass "$test" "^$testproc1_pid\\s+$testproc2_pid\\s*$"
-untested "$test"
-
-set test "pkill with not matching gid"
-#spawn $pkill -G $not_gid $testproc
-#expect_blank $test
-untested "$test"
-
-set test "pkill with process name"
-#spawn $pkill -l $testproc
-#expect_pass "$test" "^$testproc1_pid\\s+$testproc\\s+$testproc2_pid\\s+$testproc\\s*$"
-untested "$test"
-
-set test "pkill find newest test pid"
-#spawn $pkill -n $testproc
-#expect_pass "$test" "^$testproc2_pid\\s*$"
-untested "$test"
-
-set test "pkill find oldest test pid"
-#spawn $pkill -o $testproc
-#expect_pass "$test" "^$testproc1_pid\\s*$"
-untested "$test"
-
-set test "pkill matches with parent pid"
-#spawn $pkill -P $mypid $testproc
-#expect_pass "$test" "^$testproc1_pid\\s+$testproc2_pid\\s*$"
-untested "$test"
-
-set test "pkill doesn't match with bogus parent pid"
-#spawn $pkill -P $not_ppid $testproc
-#expect_blank "$test"
-untested "$test"
-
-set test "pkill matches with its own sid"
-#spawn $pkill -s $sleep1_sid $testproc
-#expect_pass "$test" "^$testproc1_pid\\s+$testproc2_pid\\s*$"
-untested "$test"
-
-set test "pkill doesn't match with bogus sid"
-#spawn $pkill -s 1 $testproc
-#expect_blank "$test"
-untested "$test"
-
-set test "pkill matches on tty"
-#spawn $pkill -t $tty $testproc
-#expect_pass "$test" "^$testproc1_pid\\s+$testproc2_pid\\s*$"
-untested "$test"
-
-set test "pkill doesn't match with bogus tty"
-#spawn $pkill -t glass $testproc
-#expect_blank "$test"
-untested "$test"
-
-set test "pkill with matching euid"
-#spawn $pkill -u $uid $testproc
-#expect_pass "$test" "^$testproc1_pid\\s+$testproc2_pid\\s*$"
-untested "$test"
-
-set test "pkill with not matching euid"
-#spawn $pkill -u $not_uid $testproc
-#expect_blank $test
-untested "$test"
-
-set test "pkill with matching uid"
-#spawn $pkill -U $uid $testproc
-#expect_pass "$test" "^$testproc1_pid\\s+$testproc2_pid\\s*$"
-untested "$test"
-
-set test "pkill with not matching uid"
-#spawn $pkill -U $not_uid $testproc
-#expect_blank $test
-untested "$test"
-
-set test "pkill matches on substring"
-#spawn $pkill $testproc_trim
-#expect_pass "$test" "^$testproc1_pid\\s+$testproc2_pid\\s*$"
-untested "$test"
-
-set test "pkill matches full string with exact"
-#spawn $pkill -x $testproc
-#expect_pass "$test" "^$testproc1_pid\\s+$testproc2_pid\\s*$"
-untested "$test"
-
-set test "pkill does not match substring with exact"
-#spawn $pkill -x $testproc_trim
-#expect_blank $test
-untested "$test"
-
-
# Cleanup
kill_testproc
+make_testproc
+
set pwdx "${topdir}pwdx"
# Run pwdx with no arguments
set test "pwdx no args"
expect_pass "$test" "\(lt-\)\?pwdx\: invalid process id\: 0"
# Run pwdx with existing pid
-set test "pwdx finds sleep in cwd"
-set sleep_pid [ exec sleep 600 & ]
-set sleep_pwd [ pwd ]
-spawn $pwdx $sleep_pid
-expect_pass "$test" "^$sleep_pid: $sleep_pwd"
-exec kill $sleep_pid
+set test "pwdx find test process cwd"
+set my_pwd [ pwd ]
+spawn $pwdx $testproc1_pid
+expect_pass "$test" "^$testproc1_pid: $my_pwd"
+# Cleanup
+kill_testproc