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