File: inspect.c

package info (click to toggle)
libpipeline 1.5.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,048 kB
  • sloc: ansic: 34,415; sh: 5,519; makefile: 168
file content (140 lines) | stat: -rw-r--r-- 3,507 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * Copyright (C) 2010 Colin Watson.
 *
 * This file is part of libpipeline.
 *
 * libpipeline 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.
 *
 * libpipeline 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 libpipeline; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA.
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

#include "error.h"

#include "common.h"

const char *program_name = "inspect";

START_TEST (test_inspect_command)
{
	pipecmd *cmd;
	char *str;

	cmd = pipecmd_new ("foo");
	str = pipecmd_tostring (cmd);
	ck_assert_str_eq (str, "foo");
	free (str);
	pipecmd_free (cmd);

	cmd = pipecmd_new_args ("foo", "bar", "baz quux", nullptr);
	str = pipecmd_tostring (cmd);
	/* TODO: not ideal representation of commands with metacharacters */
	ck_assert_str_eq (str, "foo bar baz quux");
	free (str);
	pipecmd_free (cmd);
}
END_TEST

START_TEST (test_inspect_pipeline)
{
	pipeline *p;
	char *str;

	p = pipeline_new ();
	pipeline_command_args (p, "foo", "bar", nullptr);
	pipeline_command_args (p, "grep", "baz", "quux", nullptr);
	ck_assert_int_eq (pipeline_get_ncommands (p), 2);
	pipecmd_setenv (pipeline_get_command (p, 1), "KEY", "value");
	str = pipeline_tostring (p);
	ck_assert_str_eq (str, "foo bar | KEY=value grep baz quux");
	free (str);
	pipeline_free (p);
}
END_TEST

static void pid_helper (void *data _GL_UNUSED)
{
	struct sigaction sa;

	/* Get rid of check's SIGTERM handler. */
	memset (&sa, 0, sizeof sa);
	sa.sa_handler = SIG_DFL;
	sigemptyset (&sa.sa_mask);
	sa.sa_flags = 0;
	if (sigaction (SIGTERM, &sa, NULL) == -1)
		error (1, errno, "can't install SIGTERM handler");

	printf ("%ld\n", (long) getpid ());
	fflush (stdout);
	pause ();
}

START_TEST (test_inspect_pid)
{
	pipeline *p;
	pipecmd *cmd;
	const char *line;
	pid_t pid;

	p = pipeline_new ();
	cmd = pipecmd_new_function ("pid_helper", pid_helper, NULL, NULL);
	pipeline_command (p, cmd);
	pipeline_want_out (p, -1);
	pipeline_start (p);
	line = pipeline_readline (p);
	ck_assert_ptr_ne (line, NULL);
	pid = (pid_t) atol (line);
	ck_assert_msg (pid == pipeline_get_pid (p, 0), "pids match");
	/* Note that this test may hang if pipeline_get_pid does not work.
	 * We might be able to fix this by calling setsid at the start of
	 * the test and then killing the process group, but I'm not sure if
	 * that's sufficiently portable.
	 */
	if (pid == pipeline_get_pid (p, 0)) {
		int status;

		/* Suppress "Terminated" errors. */
		setenv ("PIPELINE_QUIET", "1", 1);
		kill (pid, SIGTERM);
		status = pipeline_wait (p);

		ck_assert_msg (status == 128 + SIGTERM,
		               "pid_helper did not indicate SIGTERM");
	}
	pipeline_free (p);
}
END_TEST

static Suite *inspect_suite (void)
{
	Suite *s = suite_create ("Inspect");

	TEST_CASE (s, inspect, command);
	TEST_CASE (s, inspect, pipeline);
	TEST_CASE (s, inspect, pid);

	return s;
}

MAIN (inspect)