xref: /freebsd/tools/test/stress2/misc/openlock.sh (revision c1d255d3)
1#!/bin/sh
2
3#
4# Copyright (c) 2017 Dell EMC Isilon
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29# Check O_EXLOCK behaviour on different file types
30# Regression test for r313549.
31
32# Test scenario by kib@
33
34[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
35
36. ../default.cfg
37
38kldstat -v | grep -q pty || { kldload pty || exit 0; }
39
40dir=/tmp
41odir=`pwd`
42cd $dir
43sed '1,/^EOF/d' < $odir/$0 > $dir/openlock.c
44mycc -o openlock -Wall -Wextra -O0 -g openlock.c || exit 1
45rm -f openlock.c
46
47e=0
48echo "expect: \"error 45 Operation not supported\""
49/tmp/openlock /dev/ptmx
50s=$?
51[ $s -eq 45 ] || { echo "Expected error 45, got $s."; e=1; }
52/tmp/openlock /bin/ls
53s=$?
54[ $s -eq 0 ] || { echo "Expected error 0, got $s."; e=$((e + 2)); }
55mkfifo openlock.fifo
56s=$?
57sleep 1 > openlock.fifo &
58/tmp/openlock openlock.fifo
59s=$?
60[ $s -eq 45 ] || { echo "Expected error 45, got $s."; e=$((e + 4)); }
61wait
62
63rm -f /tmp/openlock openlock.fifo
64exit $e
65EOF
66
67/* $Id: openlock.c,v 1.1 2017/02/10 14:07:24 kostik Exp kostik $ */
68
69#include <sys/fcntl.h>
70#include <errno.h>
71#include <stdio.h>
72#include <string.h>
73#include <unistd.h>
74
75int
76main(int argc, char *argv[])
77{
78	int e, fd, i;
79
80	for (i = 1; i < argc; i++) {
81		fd = open(argv[i], O_RDONLY | O_EXLOCK);
82		if (fd == -1) {
83			printf("%s error %d %s\n", argv[i], errno,
84			    strerror(errno));
85			e = errno;
86		} else {
87			printf("%s success\n", argv[i]);
88			close(fd);
89			e = 0;
90		}
91	}
92	return (e);
93}
94
95