1d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes/*
2d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * Check decoding of ERESTARTSYS error code.
3d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes *
4d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
5d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * All rights reserved.
6d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes *
7d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * Redistribution and use in source and binary forms, with or without
8d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * modification, are permitted provided that the following conditions
9d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * are met:
10d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * 1. Redistributions of source code must retain the above copyright
11d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes *    notice, this list of conditions and the following disclaimer.
12d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * 2. Redistributions in binary form must reproduce the above copyright
13d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes *    notice, this list of conditions and the following disclaimer in the
14d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes *    documentation and/or other materials provided with the distribution.
15d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * 3. The name of the author may not be used to endorse or promote products
16d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes *    derived from this software without specific prior written permission.
17d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes *
18d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes */
29d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
30d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes#include "tests.h"
31d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
32d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes#include <signal.h>
33d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes#include <stdio.h>
34d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes#include <sys/time.h>
35d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes#include <sys/socket.h>
36d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes#include <unistd.h>
37d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
38d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughesstatic int sv[2];
39d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
40d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughesstatic void
41d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hugheshandler(int sig)
42d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes{
43d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	close(sv[1]);
44d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	sv[1] = -1;
45d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes}
46d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
47d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughesint
48d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughesmain(void)
49d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes{
50d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv))
51d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes		perror_msg_and_skip("socketpair");
52d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
53d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	const struct sigaction act = {
54d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes		.sa_handler = handler,
55d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes		.sa_flags = SA_RESTART
56d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	};
57d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	if (sigaction(SIGALRM, &act, NULL))
58d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes		perror_msg_and_fail("sigaction");
59d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
60d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	sigset_t mask;
61d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	sigemptyset(&mask);
62d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	sigaddset(&mask, SIGALRM);
63d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	if (sigprocmask(SIG_UNBLOCK, &mask, NULL))
64d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes		perror_msg_and_fail("sigprocmask");
65d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
66d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	const struct itimerval itv = { .it_value.tv_usec = 123456 };
67d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	if (setitimer(ITIMER_REAL, &itv, NULL))
68d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes		perror_msg_and_fail("setitimer");
69d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
70d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	if (recvfrom(sv[0], &sv[1], sizeof(sv[1]), 0, NULL, NULL))
71d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes		perror_msg_and_fail("recvfrom");
72d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
73d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	printf("recvfrom(%d, %p, %d, 0, NULL, NULL) = ? ERESTARTSYS"
74d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	       " (To be restarted if SA_RESTART is set)\n",
75d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	       sv[0], &sv[1], (int) sizeof(sv[1]));
76d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	printf("recvfrom(%d, \"\", %d, 0, NULL, NULL) = 0\n",
77d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	       sv[0], (int) sizeof(sv[1]));
78d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
79d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	puts("+++ exited with 0 +++");
80d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	return 0;
81d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes}
82