1# $NetBSD: t_pgrep.sh,v 1.2 2017/02/21 21:22:45 kre Exp $
2#
3# Copyright (c) 2016 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25# POSSIBILITY OF SUCH DAMAGE.
26#
27
28atf_test_case pr50934
29basic_shift_test_head() {
30	atf_set "descr" "Test fix for PR bin/50934 (null argv[0])"
31}
32pr50934_body() {
33	atf_require_prog pgrep
34	atf_require_prog cc
35
36	cat > t.c <<'!'
37#include <stdlib.h>
38#include <unistd.h>
39
40int
41main(int argc, char **argv)
42{
43	sleep(2);
44	argv[0] = 0;
45	pause();
46	exit(0);
47}
48!
49
50	cc -o t0123456789abcdefg-beef t.c || atf_fail "t.c compile failed"
51
52	./t0123456789abcdefg-beef &
53	PID=$!
54
55	pgrep -l t0123456 >OUT	; # should find the process
56	pgrep -l beef >>OUT	; # should find the process
57
58	sleep 5			; # allow time for sleep in t.c and argv[0]=0
59
60	pgrep -l t0123456 >>OUT	; # should find the process
61	pgrep -l beef >>OUT	; # should find nothing
62
63	kill -9 $PID
64	cat OUT	; # just for the log
65
66	# note that pgrep -l only ever prints p_comm which is of limited sicze
67	atf_check_equal "$(cat OUT)" \
68	   "$PID t0123456789abcde $PID t0123456789abcde $PID t0123456789abcde"
69
70	return 0
71}
72
73atf_init_test_cases() {
74	atf_add_test_case pr50934
75}
76