⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test8.c

📁 MINIX2.0操作系统源码 MINIX2.0操作系统源码
💻 C
📖 第 1 页 / 共 2 页
字号:
void test8e()
{
  struct sigaction act, oact;
  sigset_t set, oset;

  subtest = 5;
  clearsigstate();

  act.sa_handler = catch4;
  sigemptyset(&act.sa_mask);
  sigaddset(&act.sa_mask, SIGTERM);
  sigaddset(&act.sa_mask, SIGHUP);
  act.sa_flags = 0;
  if (sigaction(SIGINT, &act, &oact) == -1) e(2);

  if (sigemptyset(&set) == -1) e(3);
  if (sigaddset(&set, SIGPIPE) == -1) e(4);
  if (sigprocmask(SIG_SETMASK, &set, &oset) == -1) e(5);
  if (kill(getpid(), SIGINT) == -1) e(6);
  if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, &oset) == -1) e(7);
  if (sigemptyset(&set) == -1) e(8);
  if (sigaddset(&set, SIGPIPE) == -1) e(9);
  if (set != oset) e(10);
}

/*---------------------------------------------------------------------------*/

/* Test the basic functionality of sigsuspend(2). */

void catch5(signo)
int signo;
{
  x = 1;
}

void test8f()
{
  sigset_t set;
  int r;
  struct sigaction act;
  pid_t pid;

  subtest = 6;
  clearsigstate();

  switch (pid = fork()) {
      case 0:			/* child */
	errct = 0;
	sleep(1);
	if (kill(getppid(), SIGINT) == -1) e(1);
	exit(errct == 0 ? 0 : 1);
      case -1:	e(2);	break;
      default:			/* parent */
	if (sigemptyset(&act.sa_mask) == -1) e(3);
	act.sa_flags = 0;
	act.sa_handler = catch5;
	if (sigaction(SIGINT, &act, (struct sigaction *) NULL) == -1) e(4);

	if (sigemptyset(&set) == -1) e(5);
	r = sigsuspend(&set);

	if (r != -1 || errno != EINTR || x != 1) e(6);
	wait_for(pid);
	break;
  }
}

/*----------------------------------------------------------------------*/

/* Test that sigsuspend() does block the signals specified in its
* argument, and after sigsuspend returns, the previous signal
* mask is restored.
*
* The child sends two signals to the parent SIGINT and then SIGPIPE,
* separated by a long delay.  The parent executes sigsuspend() with
* SIGINT blocked.  It is expected that the parent's SIGPIPE handler
* will be invoked, then sigsuspend will return restoring the
* original signal mask, and then the SIGPIPE handler will be
* invoked.
*/

void sigint_handler(signo)
int signo;
{
  x = 1;
  z++;
}

void sigpipe_handler(signo)
int signo;
{
  x = 2;
  z++;
}

void test8g()
{
  sigset_t set;
  int r;
  struct sigaction act;
  pid_t pid;

  subtest = 7;
  clearsigstate();
  x = 0;
  z = 0;

  switch (pid = fork()) {
      case 0:			/* child */
	errct = 0;
	sleep(1);
	if (kill(getppid(), SIGINT) == -1) e(1);
	sleep(1);
	if (kill(getppid(), SIGPIPE) == -1) e(2);
	exit(errct == 0 ? 0 : 1);
      case -1:	e(3);	break;
      default:			/* parent */
	if (sigemptyset(&act.sa_mask) == -1) e(3);
	act.sa_flags = 0;
	act.sa_handler = sigint_handler;
	if (sigaction(SIGINT, &act, (struct sigaction *) NULL) == -1) e(4);

	act.sa_handler = sigpipe_handler;
	if (sigaction(SIGPIPE, &act, (struct sigaction *) NULL) == -1) e(5);

	if (sigemptyset(&set) == -1) e(6);
	if (sigaddset(&set, SIGINT) == -1) e(7);
	r = sigsuspend(&set);
	if (r != -1) e(8);
	if (errno != EINTR) e(9);
	if (z != 2) e(10);
	if (x != 1) e(11);
	wait_for(pid);
	break;
  }
}

/*--------------------------------------------------------------------------*/

/* Test that sigsuspend() does block the signals specified in its
* argument, and after sigsuspend returns, the previous signal
* mask is restored.
*
* The child sends three signals to the parent: SIGHUP, then SIGPIPE,
* and then SIGTERM, separated by a long delay.  The parent executes
* sigsuspend() with SIGHUP and SIGPIPE blocked.  It is expected that
* the parent's SIGTERM handler will be invoked first, then sigsuspend()
* will return restoring the original signal mask, and then the other
* two handlers will be invoked.
*/

void sighup8(signo)
int signo;
{
  x = 1;
  z++;
}

void sigpip8(signo)
int signo;
{
  x = 1;
  z++;
}

void sigter8(signo)
int signo;
{
  x = 2;
  z++;
}

void test8h()
{
  sigset_t set;
  int r;
  struct sigaction act;
  pid_t pid;

  subtest = 8;
  clearsigstate();
  x = 0;
  z = 0;

  switch (pid = fork()) {
      case 0:			/* child */
	errct = 0;
	sleep(1);
	if (kill(getppid(), SIGHUP) == -1) e(1);
	sleep(1);
	if (kill(getppid(), SIGPIPE) == -1) e(2);
	sleep(1);
	if (kill(getppid(), SIGTERM) == -1) e(3);
	exit(errct == 0 ? 0 : 1);
      case -1:	e(5);	break;
      default:			/* parent */
	if (sigemptyset(&act.sa_mask) == -1) e(6);
	act.sa_flags = 0;
	act.sa_handler = sighup8;
	if (sigaction(SIGHUP, &act, (struct sigaction *) NULL) == -1) e(7);

	act.sa_handler = sigpip8;
	if (sigaction(SIGPIPE, &act, (struct sigaction *) NULL) == -1) e(8);

	act.sa_handler = sigter8;
	if (sigaction(SIGTERM, &act, (struct sigaction *) NULL) == -1) e(9);

	if (sigemptyset(&set) == -1) e(10);
	if (sigaddset(&set, SIGHUP) == -1) e(11);
	if (sigaddset(&set, SIGPIPE) == -1) e(12);
	r = sigsuspend(&set);
	if (r != -1) e(13);
	if (errno != EINTR) e(14);
	if (z != 3) e(15);
	if (x != 1) e(16);
	wait_for(pid);
	break;
  }
}

/*--------------------------------------------------------------------------*/

/* Block SIGHUP and SIGTERM with sigprocmask(), send ourself SIGHUP
* and SIGTERM, unblock these signals with sigprocmask, and verify
* that these signals are delivered.
*/

void sighup9(signo)
int signo;
{
  y++;
}

void sigter9(signo)
int signo;
{
  z++;
}

void test8i()
{
  sigset_t set;
  struct sigaction act;

  subtest = 9;
  clearsigstate();
  y = 0;
  z = 0;

  if (sigemptyset(&act.sa_mask) == -1) e(1);
  act.sa_flags = 0;

  act.sa_handler = sighup9;
  if (sigaction(SIGHUP, &act, (struct sigaction *) NULL) == -1) e(2);

  act.sa_handler = sigter9;
  if (sigaction(SIGTERM, &act, (struct sigaction *) NULL) == -1) e(3);

  if (sigemptyset(&set) == -1) e(4);
  if (sigaddset(&set, SIGTERM) == -1) e(5);
  if (sigaddset(&set, SIGHUP) == -1) e(6);
  if (sigprocmask(SIG_SETMASK, &set, (sigset_t *)NULL) == -1) e(7);

  if (kill(getpid(), SIGHUP) == -1) e(8);
  if (kill(getpid(), SIGTERM) == -1) e(9);
  if (y != 0) e(10);
  if (z != 0) e(11);

  if (sigemptyset(&set) == -1) e(12);
  if (sigprocmask(SIG_SETMASK, &set, (sigset_t *)NULL) == -1) e(12);
  if (y != 1) e(13);
  if (z != 1) e(14);
}

/*---------------------------------------------------------------------------*/

/* Block SIGINT and then send this signal to ourself.
*
* Install signal handlers for SIGALRM and SIGINT.
*
* Set an alarm for 6 seconds, then sleep for 7.
*
* The SIGALRM should interrupt the sleep, but the SIGINT
* should remain pending.
*/

void sighup10(signo)
int signo;
{
  y++;
}

void sigalrm_handler10(signo)
int signo;
{
  z++;
}

void test8j()
{
  sigset_t set, set2;
  struct sigaction act;

  subtest = 10;
  clearsigstate();
  y = 0;
  z = 0;

  if (sigemptyset(&act.sa_mask) == -1) e(1);
  act.sa_flags = 0;

  act.sa_handler = sighup10;
  if (sigaction(SIGHUP, &act, (struct sigaction *) NULL) == -1) e(2);

  act.sa_handler = sigalrm_handler10;
  if (sigaction(SIGALRM, &act, (struct sigaction *) NULL) == -1) e(3);

  if (sigemptyset(&set) == -1) e(4);
  if (sigaddset(&set, SIGHUP) == -1) e(5);
  if (sigprocmask(SIG_SETMASK, &set, (sigset_t *)NULL) == -1) e(6);

  if (kill(getpid(), SIGHUP) == -1) e(7);
  if (sigpending(&set) == -1) e(8);
  if (sigemptyset(&set2) == -1) e(9);
  if (sigaddset(&set2, SIGHUP) == -1) e(10);
  if (set2 != set) e(11);
  alarm(6);
  sleep(7);
  if (sigpending(&set) == -1) e(12);
  if (set != set2) e(13);
  if (y != 0) e(14);
  if (z != 1) e(15);
}

/*--------------------------------------------------------------------------*/

void test8k()
{
  subtest = 11;
}
void test8l()
{
  subtest = 12;
}

/*---------------------------------------------------------------------------*/

/* Basic test for setjmp/longjmp.  This includes testing that the
* signal mask is properly restored.
*/

void test8m()
{
  jmp_buf jb;
  sigset_t ss;

  subtest = 13;
  clearsigstate();

  ss = 0x32;
  if (sigprocmask(SIG_SETMASK, &ss, (sigset_t *)NULL) == -1) e(1);
  if (setjmp(jb)) {
	if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, &ss) == -1) e(2);
	if (ss != 0x32) e(388);
	return;
  }
  ss = 0x3abc;
  if (sigprocmask(SIG_SETMASK, &ss, (sigset_t *)NULL) == -1) e(4);
  longjmp(jb, 1);
}

void longjerr()
{
  e(5);
}

/*--------------------------------------------------------------------------*/

/* Test for setjmp/longjmp.
*
* Catch a signal.  While in signal handler do setjmp/longjmp.
*/

void catch14(signo, code, scp)
int signo;
int code;
struct sigcontext *scp;
{
  jmp_buf jb;

  if (setjmp(jb)) {
	x++;
	sigreturn(scp);
	e(1);
  }
  y++;
  longjmp(jb, 1);
  e(2);
}

void test8n()
{
  struct sigaction act;
  typedef _PROTOTYPE( void (*sighandler_t), (int sig) );

  subtest = 14;
  clearsigstate();
  x = 0;
  y = 0;

  act.sa_flags = 0;
  act.sa_mask = 0;
  act.sa_handler = (sighandler_t) catch14;	/* fudge */
  if (sigaction(SIGSEGV, &act, (struct sigaction *) NULL) == -1) e(3);
  if (kill(getpid(), SIGSEGV) == -1) e(4);

  if (x != 1) e(5);
  if (y != 1) e(6);
}

/*---------------------------------------------------------------------------*/

/* Test for setjmp/longjmp.
 *
 * Catch a signal.  Longjmp out of signal handler.
 */
jmp_buf glo_jb;

void catch15(signo)
int signo;
{
  z++;
  longjmp(glo_jb, 7);
  e(1);

}

void test8o()
{
  struct sigaction act;
  int k;

  subtest = 15;
  clearsigstate();
  z = 0;

  act.sa_flags = 0;
  act.sa_mask = 0;
  act.sa_handler = catch15;
  if (sigaction(SIGALRM, &act, (struct sigaction *) NULL) == -1) e(2);

  if ((k = setjmp(glo_jb))) {
	if (z != 1) e(399);
	if (k != 7) e(4);
	return;
  }
  if (kill(getpid(), SIGALRM) == -1) e(5);
}

void clearsigstate()
{
  int i;
  sigset_t sigset_var;

  /* Clear the signal state. */
  for (i = 1; i <= _NSIG; i++) signal(i, SIG_IGN);
  for (i = 1; i <= _NSIG; i++) signal(i, SIG_DFL);
  sigfillset(&sigset_var);
  sigprocmask(SIG_UNBLOCK, &sigset_var, (sigset_t *)NULL);
}

void quit()
{

  chdir("..");
  system("rm -rf DIR*");

  if (errct == 0) {
	printf("ok\n");
	exit(0);
  } else {
	printf("%d errors\n", errct);
	exit(4);
  }
}

void wait_for(pid)
pid_t pid;
{
/* Expect exactly one child, and that it exits with 0. */

  int r;
  int status;

  errno = 0;
  while (1) {
	errno = 0;
	r = wait(&status);
	if (r == pid) {
		errno = 0;
		if (status != 0) e(90);
		return;
	}
	if (r < 0) {
		e(91);
		return;
	}
	e(92);
  }
}

void e(n)
int n;
{
  char msgbuf[80];

  sprintf(msgbuf, "Subtest %d,  error %d  errno=%d  ", subtest, n, errno);
  perror(msgbuf);
  if (errct++ > MAX_ERROR) {
	fprintf(stderr, "Too many errors;  test aborted\n");
	chdir("..");
	system("rm -rf DIR*");
	exit(1);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -