Exemplo n.º 1
0
void rpn_csh_str()
{
    char *string;
    void dummy_sigusr1();
    signal(SIGUSR1, dummy_sigusr1);

    if (!fp) {
        /* open a pipe and start csh */
#if defined(vxWorks)
      fprintf(stderr, "popen is not supported in vxWorks\n");
      exit(1);
#else
      fp = popen("csh", "w");
      pid = getpid();
#endif
    }
    if (!(string = pop_string()))
        return;

    fprintf(fp, "%s\nkill -USR1 %d\n", string, pid);
    fflush(fp);
    /* pause until SIGUSR1 is received */
    sigpause(SIGUSR1);

    /* back to default behavior for sigusr1 */
    signal(SIGUSR1, SIG_DFL);
    }
Exemplo n.º 2
0
int main()
{
	int return_value = 0;
        int result;

	return_value = sigpause(-1);
	if (return_value == -1) {
		if (errno == EINVAL) {
			printf ("Test PASSED: sigpause returned -1 and set errno to EINVAL\n");
			result = 0;
		} else {
			printf ("Test FAILED: sigpause did not set errno to EINVAL\n");
			result = 1;
		}
	} else {
		printf ("Test FAILED: sigpause did not return -1\n");
		if (errno == EINVAL)
			printf ("Test FAILED: sigpause did not set errno to EINVAL\n");
		result = 1;
	}

        if (result == 2) {
                return PTS_UNRESOLVED;
        }
        if (result == 1) {
                return PTS_FAIL;
        }

        printf("Test PASSED\n");
	return PTS_PASS;
}
Exemplo n.º 3
0
void *a_thread_func(void *arg)
{
	int return_value = 0;
	struct sigaction act;
	act.sa_flags = 0;
	act.sa_handler = handler;
	sigemptyset(&act.sa_mask);
	sigaction(SIGTOTEST, &act, 0);
	return_value = sigpause(SIGTOTEST);
	if (return_value == -1) {
		if (errno == EINTR) {
			printf ("Test PASSED: sigpause returned -1 and set errno to EINTR\n");
			result = 0;
		} else {
			printf ("Test FAILED: sigpause did not set errno to EINTR\n");
			result = 1;
		}
	} else {
		if (errno == EINTR) {
			printf ("Test FAILED: sigpause did not return -1\n");
		}
		printf ("Test FAILED: sigpause did not set errno to EINTR\n");
		printf ("Test FAILED: sigpause did not return -1\n");
		result = 1;

	}
	sem = INMAIN;
	return NULL;
}
Exemplo n.º 4
0
TEST(signal, sighold_sigpause_sigrelse) {
  static int sigalrm_handler_call_count;
  auto sigalrm_handler = [](int) { sigalrm_handler_call_count++; };
  ScopedSignalHandler sigalrm{SIGALRM, sigalrm_handler};
  ScopedSignalMask mask;
  sigset_t set;

  // sighold(SIGALRM) should add SIGALRM to the signal mask ...
  ASSERT_EQ(0, sighold(SIGALRM));
  ASSERT_EQ(0, sigprocmask(SIG_SETMASK, 0, &set));
  EXPECT_TRUE(sigismember(&set, SIGALRM));

  // ... preventing our SIGALRM handler from running ...
  raise(SIGALRM);
  ASSERT_EQ(0, sigalrm_handler_call_count);
  // ... until sigpause(SIGALRM) temporarily unblocks it.
  ASSERT_EQ(-1, sigpause(SIGALRM));
  ASSERT_EQ(EINTR, errno);
  ASSERT_EQ(1, sigalrm_handler_call_count);

  // But sigpause(SIGALRM) shouldn't permanently unblock SIGALRM.
  ASSERT_EQ(0, sigprocmask(SIG_SETMASK, 0, &set));
  EXPECT_TRUE(sigismember(&set, SIGALRM));

  ASSERT_EQ(0, sigrelse(SIGALRM));
  ASSERT_EQ(0, sigprocmask(SIG_SETMASK, 0, &set));
  EXPECT_FALSE(sigismember(&set, SIGALRM));
}
Exemplo n.º 5
0
void
pause_on_sigusr( int which )
{
  #ifndef HAVE_DOSISH_SYSTEM
   #ifdef HAVE_SIGPROCMASK
    sigset_t mask, oldmask;

    assert( which == 1 );
    sigemptyset( &mask );
    sigaddset( &mask, SIGUSR1 );

    sigprocmask( SIG_BLOCK, &mask, &oldmask );
    while( !caught_sigusr1 )
	sigsuspend( &oldmask );
    caught_sigusr1 = 0;
    sigprocmask( SIG_UNBLOCK, &mask, NULL );
   #else 
     assert (which == 1);
     sighold (SIGUSR1);
     while (!caught_sigusr1)
         sigpause(SIGUSR1);
     caught_sigusr1 = 0;
     sigrelse(SIGUSR1); ????
   #endif /*!HAVE_SIGPROCMASK*/
  #endif
}
Exemplo n.º 6
0
int
usleep(int usec)
{
struct	itimerval	value, dumb;

	(void)signal(SIGALRM, usleep_sig);

	if(getitimer(ITIMER_REAL, &value) != 0)
	{
		perror("Error: couldn't get timer");
		return(-1);
	}
	value.it_value.tv_sec = value.it_interval.tv_sec = (long) usec / 1000000;
	value.it_value.tv_usec = value.it_interval.tv_usec = (long) usec % 1000000;
        
	if(setitimer(ITIMER_REAL, &value, &dumb) != 0) /* ignore parameter 3 */
	{
		perror("Error: couldn't set timer");
		return(-1);
	}

	(void)sigpause(SIGALRM);

	if(getitimer(ITIMER_REAL, &value) != 0)
	{
		perror("Error: couldn't get timer");
		return(-1);
	}
	usec = (value.it_value.tv_sec - value.it_interval.tv_sec) * 1000000;
	usec += (value.it_value.tv_usec - value.it_interval.tv_usec);
	return usec;
}
Exemplo n.º 7
0
static void
timerPause(void)
{
#ifdef HAVE_SIGHOLD
	sighold(SIGALRM);
	if (! signalled) {
		sigpause(SIGALRM);
	}
#else
	int oldmask;
	oldmask = sigblock(sigmask(SIGALRM));
	if (! signalled) {
		sigpause(0);
	}
	sigsetmask(oldmask);
#endif
}
Exemplo n.º 8
0
delayMsec(int time)
{
    timer_went_off = 0;
    setup_ms_timer(time);  /* in msec */
    while (!timer_went_off)
       sigpause(0);
    cleanup_from_timeout();
}
Exemplo n.º 9
0
/****************************************************************************
 * Private Functions
 ****************************************************************************/
static void taskmgr_pause_handler(int signo, siginfo_t *info, void *extra)
{
	if (signo != SIGTM_PAUSE) {
		tmdbg("[TM] Invalid signal. signo = %d\n", signo);
		return;
	}

	sigpause(SIGTM_RESUME);
}
Exemplo n.º 10
0
int
sigsuspend (sigset_t *mask)
{
	if (!mask) {
		errno = EINVAL;
		return -1;
	}
	return sigpause(*mask);
}
Exemplo n.º 11
0
void sig_pause(void)
{
#ifdef SD_HAVE_SIGPROCMASK
  sigset_t ss;
  sigemptyset(&ss);
  sigsuspend(&ss);
#else
  sigpause(0);
#endif
}
Exemplo n.º 12
0
int main()
{
	int child_pid, child_pgid;

	if ((child_pid = fork()) == 0) {
		/* child here */
		struct sigaction act;
		act.sa_handler=myhandler;
		act.sa_flags=0;
		sigemptyset(&act.sa_mask);
		sigaction(SIGTOTEST, &act, 0);

		/* change child's process group id */
		setpgrp();

		sigpause(SIGABRT);

		return 0;
	} else {
		/* parent here */
		int i;
		sigignore(SIGTOTEST);

		sleep(1);
		if ((child_pgid = getpgid(child_pid)) == -1) {
			printf("Could not get pgid of child\n");
			return PTS_UNRESOLVED;
		}


		if (killpg(child_pgid, SIGTOTEST) != 0) {
			printf("Could not raise signal being tested\n");
			return PTS_UNRESOLVED;
		}

		if (wait(&i) == -1) {
			perror("Error waiting for child to exit\n");
			return PTS_UNRESOLVED;
		}

		if (WEXITSTATUS(i)) {
			printf("Child exited normally\n");
			printf("Test PASSED\n");
			return PTS_PASS;
		} else {
			printf("Child did not exit normally.\n");
			printf("Test FAILED\n");
			return PTS_FAIL;
		}
	}

	printf("Should have exited from parent\n");
	printf("Test FAILED\n");
	return PTS_FAIL;
}
Exemplo n.º 13
0
void
wait_for_signal(void)
{
	int mask, omask;

	mask = sigmask(SIGIO) | sigmask(SIGALRM);
	omask = sigblock(0);
	omask &= ~mask;
	if (sigpause(omask) && (errno != EINTR))
	    msyslog(LOG_ERR, "wait_for_signal: sigspause() failed: %m");
}
Exemplo n.º 14
0
main()
{

    int n ;
    char buff[BUFFSIZE];


    signal( SIGIO , sigio_func );

    if( fcntl( 0 , F_SETOWN , getpid() ) <  0 ){
        return 1;
    }

    if( fcntl( 0 , F_SETFL , FASYNC ) < 0 ){
        return 2;
    }

    for(;;){

        sigblock( sigmask(SIGIO) );

        printf( ">");
        while( sigflag == 0 ){
            sigpause(0);
        }
#if 0
        if( ( n = read( 0 , buff , BUFFSIZE )) > 0 ){
            if( write(1,buff,n) != n ){
                return 5;
            }
        } else {

            if( n < 0 ){
                return 8;
            } else if( n == 0 ){
                return 0;
            }
        }
#endif
        {
            char *p = readline( "" );
            printf( "<%s>\n", p );
        }
        
        sigflag = 0;

        sigsetmask(0);
    }
}
Exemplo n.º 15
0
void *timer_function(void *time){

int id;
id = *(int *)time;

while(id<20)
id++;

printf("Timer ran : %d sec \n", id);

printf("Setting alarm in timer thread\n");
set_timer();

for (;;)
        sigpause();
printf("Timer wait done\n");
}
Exemplo n.º 16
0
Arquivo: t_pause.c Projeto: malaise/c
int main(void) {

  printf ("My pid is %d\n", getpid());
  printf (
   "Signal managed : SIGTALRM(intern), SIGUSR1(30), SIGUSR2(31)\n");


  set_handler (SIGALRM, handler_sigalrm, NULL);
  set_handler (SIGUSR1, handler_sigusr1, NULL);
  set_handler (SIGUSR2, handler_sigusr2, NULL);

  printf ("Start timer\n");
  arm_timer (ITIMER_REAL, (long)ALRM_S, (long)ALRM_U, 1);

  sigpause (sigmask(SIGALRM) | sigmask(SIGUSR1));
  printf ("Done.\n");
  exit(0);
}
Exemplo n.º 17
0
unsigned int
sleep(unsigned int seconds) {
	sig_t ovec;
	long omask;

	if (seconds == 0) {
		return 0;
	}
	ovec = signal(SIGALRM, _sleephandler);
	omask = sigblock(sigmask(SIGALRM));
	_ringring = 0;
        alarm(seconds);
	while (!_ringring) {
		sigpause(omask &~ sigmask(SIGALRM));
	}
	signal(SIGALRM, ovec);
	sigsetmask(omask);
	return 0;
}
Exemplo n.º 18
0
/* Timer: wait for n milliseconds */
static void Timer(int n)
{
   long   usec;
   struct itimerval it;
   
   usec = (long) n * 1000;
   memset(&it, 0, sizeof(it));
   if (usec>=1000000L) {  /* more than 1 second */
      it.it_value.tv_sec = usec / 1000000L;
      usec %= 1000000L;
   }
   it.it_value.tv_usec = usec; timerDone=FALSE;
   signal(SIGALRM, AlarmOn);
   setitimer(ITIMER_REAL, &it, NULL);
   while (1) {
      sigblock(sigmask(SIGALRM)); 
      if (timerDone) break;       
      sigpause(0);                
   }
   sigblock(0);
   signal(SIGALRM, SIG_DFL);
}
Exemplo n.º 19
0
void
runstats(FILE *fd, struct statshandler *sh)
{
	u_long off;
	int line, omask;

	if (sh->interval < 1)
		sh->interval = 1;
	signal(SIGALRM, catchalarm);
	signalled = 0;
	alarm(sh->interval);
banner:
	sh->printbanner(sh, fd);
	fflush(fd);
	line = 0;
loop:
	if (line != 0) {
		sh->getstats(sh, sh->cur);
		sh->reportdelta(sh, fd);
		sh->update(sh);
	} else {
		sh->getstats(sh, sh->total);
		sh->reporttotal(sh, fd);
	}
	putc('\n', fd);
	fflush(fd);
	omask = sigblock(sigmask(SIGALRM));
	if (!signalled)
		sigpause(0);
	sigsetmask(omask);
	signalled = 0;
	alarm(sh->interval);
	line++;
	if (line == 21)		/* XXX tty line count */
		goto banner;
	else
		goto loop;
	/*NOTREACHED*/
}
Exemplo n.º 20
0
void rpn_csh()
{
    char *ptr;
    void dummy_sigusr1();
    static char s[1024];

    signal(SIGUSR1, dummy_sigusr1);

    if (!fp) {
        /* open a pipe and start csh */
#if defined(vxWorks)
      fprintf(stderr, "popen is not supported in vxWorks\n");
      exit(1);
#else
      fp = popen("csh", "w");
      pid = getpid();
#endif
    }

    /* loop to print prompt and accept commands */
    while (fputs("csh> ", stdout), fgets(s, 100, stdin)) {
        /* send user's command along with another than causes subprocess
         * to send the SIGUSR1 signal this process 
         */
        ptr = s;
        while (isspace(*ptr))
            ptr++;
        if (strncmp(ptr, "quit", 4)==0 || strncmp(ptr, "exit", 4)==0)
            break;
        fprintf(fp, "%s\nkill -USR1 %d\n", s, pid);
        fflush(fp);
        /* pause until SIGUSR1 is received */
        sigpause(SIGUSR1);
        }

    /* back to default behavior for sigusr1 */
    signal(SIGUSR1, SIG_DFL);
    }
Exemplo n.º 21
0
Arquivo: aiocat.c Projeto: rkks/refer
main()
{
	int		n;
	char		buff[BUFFSIZE];
	int		sigio_func();

	signal(SIGIO, sigio_func);

	if (fcntl(0, F_SETOWN, getpid()) < 0)
		err_sys("F_SETOWN error");

	if (fcntl(0, F_SETFL, FASYNC) < 0)
		err_sys("F_SETFL FASYNC error");

	for ( ; ; ) {
		sigblock(sigmask(SIGIO));
		while (sigflag == 0)
			sigpause(0);		/* wait for a signal */

		/*
		 * We're here if (sigflag != 0).  Also, we know that the
		 * SIGIO signal is currently blocked.
		 */

		if ( (n = read(0, buff, BUFFSIZE)) > 0) {
			if (write(1, buff, n) != n)
				err_sys("write error");
		} else if (n < 0)
			err_sys("read error");
		else if (n == 0)
			exit(0);		/* EOF */

		sigflag = 0;			/* turn off our flag */

		sigsetmask(0);			/* and reenable signals */
	}
}
Exemplo n.º 22
0
void
pause_on_sigusr( int which )
{
#if defined(HAVE_SIGPROCMASK) && defined(HAVE_SIGSET_T)
    sigset_t mask, oldmask;

    assert( which == 1 );
    sigemptyset( &mask );
    sigaddset( &mask, SIGUSR1 );

    sigprocmask( SIG_BLOCK, &mask, &oldmask );
    while( !caught_sigusr1 )
	sigsuspend( &oldmask );
    caught_sigusr1 = 0;
    sigprocmask( SIG_UNBLOCK, &mask, NULL );
#else
     assert (which == 1);
     sighold (SIGUSR1);
     while (!caught_sigusr1)
         sigpause(SIGUSR1);
     caught_sigusr1 = 0;
     sigrelse(SIGUSR1);
#endif /*! HAVE_SIGPROCMASK && HAVE_SIGSET_T */
}
Exemplo n.º 23
0
void
mkfs(char *fsys, int fi, int fo, const char *mfscopy)
{
	long i, mincpc, mincpg, inospercg;
	long cylno, rpos, blk, j, emitwarn = 0;
	long used, mincpgcnt, bpcg;
	off_t usedb;
	long mapcramped, inodecramped;
	long postblsize, rotblsize, totalsbsize;
	int status, fd;
	time_t utime;
	quad_t sizepb;
	int width;
	char tmpbuf[100];	/* XXX this will break in about 2,500 years */

	time(&utime);
#ifdef FSIRAND
	if (!randinit) {
		randinit = 1;
		srandomdev();
	}
#endif
	if (mfs) {
		int omask;
		pid_t child;

		mfs_ppid = getpid();
		signal(SIGUSR1, parentready);
		if ((child = fork()) != 0) {
			/*
			 * Parent
			 */
			if (child == -1)
				err(10, "mfs");
			if (mfscopy)
			    copyroot = FSCopy(&copyhlinks, mfscopy);
			signal(SIGUSR1, started);
			kill(child, SIGUSR1);
			while (waitpid(child, &status, 0) != child)
				;
			exit(WEXITSTATUS(status));
			/* NOTREACHED */
		}

		/*
		 * Child
		 */
		omask = sigblock(sigmask(SIGUSR1));
		while (parentready_signalled == 0)
			sigpause(omask);
		sigsetmask(omask);
		if (filename != NULL) {
			unsigned char buf[BUFSIZ];
			unsigned long l, l1;
			ssize_t w;

			fd = open(filename, O_RDWR|O_TRUNC|O_CREAT, 0644);
			if(fd < 0)
				err(12, "%s", filename);
			l1 = fssize * sectorsize;
			if (l1 > BUFSIZ)
				l1 = BUFSIZ;
			for (l = 0; l < fssize * (u_long)sectorsize; l += l1) {
				w = write(fd, buf, l1);
				if (w < 0 || (u_long)w != l1)
					err(12, "%s", filename);
			}
			membase = mmap(NULL, fssize * sectorsize,
				       PROT_READ|PROT_WRITE,
				       MAP_SHARED, fd, 0);
			if (membase == MAP_FAILED)
				err(12, "mmap");
			close(fd);
		} else {
			membase = mmap(NULL, fssize * sectorsize,
				       PROT_READ|PROT_WRITE,
				       MAP_SHARED|MAP_ANON, -1, 0);
			if (membase == MAP_FAILED)
				errx(13, "mmap (anonymous memory) failed");
		}
	}
	fsi = fi;
	fso = fo;
	if (Oflag) {
		sblock.fs_inodefmt = FS_42INODEFMT;
		sblock.fs_maxsymlinklen = 0;
	} else {
		sblock.fs_inodefmt = FS_44INODEFMT;
		sblock.fs_maxsymlinklen = MAXSYMLINKLEN;
	}
	if (Uflag)
		sblock.fs_flags |= FS_DOSOFTDEP;
	if (Lflag)
		strlcpy(sblock.fs_volname, volumelabel, MAXVOLLEN);

	/*
	 * Validate the given file system size.
	 * Verify that its last block can actually be accessed.
	 */
	if (fssize == 0)
		printf("preposterous size %lu\n", fssize), exit(13);
	wtfs(fssize - (realsectorsize / DEV_BSIZE), realsectorsize,
		 (char *)&sblock);
	/*
	 * collect and verify the sector and track info
	 */
	sblock.fs_nsect = nsectors;
	sblock.fs_ntrak = ntracks;
	if (sblock.fs_ntrak <= 0)
		printf("preposterous ntrak %d\n", sblock.fs_ntrak), exit(14);
	if (sblock.fs_nsect <= 0)
		printf("preposterous nsect %d\n", sblock.fs_nsect), exit(15);
	/*
	 * collect and verify the filesystem density info
	 */
	sblock.fs_avgfilesize = avgfilesize;
	sblock.fs_avgfpdir = avgfilesperdir;
	if (sblock.fs_avgfilesize <= 0)
		printf("illegal expected average file size %d\n",
		    sblock.fs_avgfilesize), exit(14);
	if (sblock.fs_avgfpdir <= 0)
		printf("illegal expected number of files per directory %d\n",
		    sblock.fs_avgfpdir), exit(15);
	/*
	 * collect and verify the block and fragment sizes
	 */
	sblock.fs_bsize = bsize;
	sblock.fs_fsize = fsize;
	if (!POWEROF2(sblock.fs_bsize)) {
		printf("block size must be a power of 2, not %d\n",
		    sblock.fs_bsize);
		exit(16);
	}
	if (!POWEROF2(sblock.fs_fsize)) {
		printf("fragment size must be a power of 2, not %d\n",
		    sblock.fs_fsize);
		exit(17);
	}
	if (sblock.fs_fsize < sectorsize) {
		printf("fragment size %d is too small, minimum is %d\n",
		    sblock.fs_fsize, sectorsize);
		exit(18);
	}
	if (sblock.fs_bsize < MINBSIZE) {
		printf("block size %d is too small, minimum is %d\n",
		    sblock.fs_bsize, MINBSIZE);
		exit(19);
	}
	if (sblock.fs_bsize < sblock.fs_fsize) {
		printf("block size (%d) cannot be smaller than fragment size (%d)\n",
		    sblock.fs_bsize, sblock.fs_fsize);
		exit(20);
	}
	sblock.fs_bmask = ~(sblock.fs_bsize - 1);
	sblock.fs_fmask = ~(sblock.fs_fsize - 1);
	sblock.fs_qbmask = ~sblock.fs_bmask;
	sblock.fs_qfmask = ~sblock.fs_fmask;
	for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1)
		sblock.fs_bshift++;
	for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1)
		sblock.fs_fshift++;
	sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize);
	for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1)
		sblock.fs_fragshift++;
	if (sblock.fs_frag > MAXFRAG) {
		printf("fragment size %d is too small, minimum with block size %d is %d\n",
		    sblock.fs_fsize, sblock.fs_bsize,
		    sblock.fs_bsize / MAXFRAG);
		exit(21);
	}
	sblock.fs_nrpos = nrpos;
	sblock.fs_nindir = sblock.fs_bsize / sizeof(daddr_t);
	sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs1_dinode);
	sblock.fs_nspf = sblock.fs_fsize / sectorsize;
	for (sblock.fs_fsbtodb = 0, i = NSPF(&sblock); i > 1; i >>= 1)
		sblock.fs_fsbtodb++;
	sblock.fs_sblkno =
	    roundup(howmany(bbsize + sbsize, sblock.fs_fsize), sblock.fs_frag);
	sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno +
	    roundup(howmany(sbsize, sblock.fs_fsize), sblock.fs_frag));
	sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag;
	sblock.fs_cgoffset = roundup(
	    howmany(sblock.fs_nsect, NSPF(&sblock)), sblock.fs_frag);
	for (sblock.fs_cgmask = 0xffffffff, i = sblock.fs_ntrak; i > 1; i >>= 1)
		sblock.fs_cgmask <<= 1;
	if (!POWEROF2(sblock.fs_ntrak))
		sblock.fs_cgmask <<= 1;
	sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
	for (sizepb = sblock.fs_bsize, i = 0; i < NIADDR; i++) {
		sizepb *= NINDIR(&sblock);
		sblock.fs_maxfilesize += sizepb;
	}
	/*
	 * Validate specified/determined secpercyl
	 * and calculate minimum cylinders per group.
	 */
	sblock.fs_spc = secpercyl;
	for (sblock.fs_cpc = NSPB(&sblock), i = sblock.fs_spc;
	     sblock.fs_cpc > 1 && (i & 1) == 0;
	     sblock.fs_cpc >>= 1, i >>= 1)
		/* void */;
	mincpc = sblock.fs_cpc;
	bpcg = sblock.fs_spc * sectorsize;
	inospercg = roundup(bpcg / sizeof(struct ufs1_dinode), INOPB(&sblock));
	if (inospercg > MAXIPG(&sblock))
		inospercg = MAXIPG(&sblock);
	used = (sblock.fs_iblkno + inospercg / INOPF(&sblock)) * NSPF(&sblock);
	mincpgcnt = howmany(sblock.fs_cgoffset * (~sblock.fs_cgmask) + used,
	    sblock.fs_spc);
	mincpg = roundup(mincpgcnt, mincpc);
	/*
	 * Ensure that cylinder group with mincpg has enough space
	 * for block maps.
	 */
	sblock.fs_cpg = mincpg;
	sblock.fs_ipg = inospercg;
	if (maxcontig > 1)
		sblock.fs_contigsumsize = MIN(maxcontig, FS_MAXCONTIG);
	mapcramped = 0;
	while (CGSIZE(&sblock) > (uint32_t)sblock.fs_bsize) {
		mapcramped = 1;
		if (sblock.fs_bsize < MAXBSIZE) {
			sblock.fs_bsize <<= 1;
			if ((i & 1) == 0) {
				i >>= 1;
			} else {
Exemplo n.º 24
0
/*---------------------------------------------------------------------+
|                               main ()                                |
| ==================================================================== |
|                                                                      |
| Function:  Main program  (see prolog for more details)               |
|                                                                      |
+---------------------------------------------------------------------*/
int main(int argc, char **argv)
{
	sigset_t mask,		/* Initial process signal mask */
	 newmask,		/* Second process signal mask */
	 oldmask;		/* Signal mask returned by sigblock */
	pid_t pid = getpid();	/* Process ID (of this process) */

	/* Print out program header */
	printf("%s: IPC Signals TestSuite program\n\n", *argv);

	/*
	 * Establish signal handler for each signal & reset "valid signals"
	 * array, and setup alternative stack for processing signals
	 */
	init_sig_vec();
	reset_valid_sig();
#ifdef _LINUX_
	// sigstack function is obsolete, use sigaltstack instead
	if (sigaltstack(&stack, NULL) < 0)
		sys_error("sigaltstack failed", __LINE__);
#else
	if (sigstack(&stack, NULL) < 0)
		sys_error("sigstack failed", __LINE__);
#endif
	/*
	 * Send SIGILL, SIGALRM & SIGIOT signals to this process:
	 *
	 * First indicate which signals the signal handler should expect
	 * by setting the corresponding valid_sig[] array fields.
	 *
	 * Then send the signals to this process.
	 *
	 * And finally verify that the signals were caught by the signal
	 * handler by checking to see if the corresponding valid_sig[] array
	 * fields were reset.
	 */
	printf("\tSend SIGILL, SIGALRM, SIGIOT signals to process\n");
	valid_sig[SIGILL] = 1;
	valid_sig[SIGALRM] = 1;
	valid_sig[SIGIOT] = 1;

	kill(pid, SIGILL);
	kill(pid, SIGALRM);
	kill(pid, SIGIOT);

	if (valid_sig[SIGILL])
		error("failed to receive SIGILL signal!", __LINE__);
	if (valid_sig[SIGALRM])
		error("failed to receive SIGALRM signal!", __LINE__);
	if (valid_sig[SIGIOT])
		error("failed to receive SIGIOT signal!", __LINE__);

	/*
	 * Block SIGILL, SIGALRM & SIGIOT signals:
	 *
	 * First create the process signal mask by ORing together the
	 * signal values.
	 *
	 * Then change the process signal mask with sigsetmask ().
	 *
	 * Verify that the desired signals are blocked from interrupting the
	 * process, by sending both blocked and unblocked signals to the
	 * process. Only the unblocked signals should interrupt the process.
	 */
	printf("\n\tBlock SIGILL, SIGALRM, SIGIOT signals, "
	       "and resend signals + others\n");
	sigemptyset(&mask);
	sigaddset(&mask, SIGILL);
	sigaddset(&mask, SIGALRM);
	sigaddset(&mask, SIGIOT);
#ifdef _LINUX_
	sigprocmask(SIG_SETMASK, &mask, NULL);
#else
	if (sigsetmask(mask) < 0)
		sys_error("setsigmask failed", __LINE__);
#endif
	valid_sig[SIGFPE] = 1;
	valid_sig[SIGTERM] = 1;
	valid_sig[SIGINT] = 1;

	kill(pid, SIGILL);
	kill(pid, SIGALRM);
	kill(pid, SIGIOT);
	kill(pid, SIGFPE);
	kill(pid, SIGTERM);
	kill(pid, SIGINT);

	if (valid_sig[SIGFPE])
		error("failed to receive SIGFPE signal!", __LINE__);
	if (valid_sig[SIGTERM])
		error("failed to receive SIGTERM signal!", __LINE__);
	if (valid_sig[SIGINT])
		error("failed to receive SIGINT signal!", __LINE__);

	/*
	 * Block additional SIGFPE, SIGTERM & SIGINT signals:
	 *
	 * Create a signal mask containing the additional signals to block.
	 *
	 * Change the process signal mask to block the additional signals
	 * with the sigprocmask () function.
	 *
	 * Verify that all of the desired signals are now blocked from
	 * interrupting the process.  None of the specified signals should
	 * interrupt the process until the process signal mask is changed.
	 */
	printf("\n\tBlock rest of signals\n");
	sigemptyset(&newmask);
	sigaddset(&newmask, SIGFPE);
	sigaddset(&newmask, SIGTERM);
	sigaddset(&newmask, SIGINT);
	sigemptyset(&oldmask);
	if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
		perror("sigprocmask failed");
		exit(-1);
	}
	if (memcmp(&mask, &oldmask, sizeof(mask)) != 0)
		error("value returned by sigblock () does not match the "
		      "old signal mask", __LINE__);

	kill(pid, SIGILL);
	kill(pid, SIGALRM);
	kill(pid, SIGIOT);
	kill(pid, SIGFPE);
	kill(pid, SIGTERM);
	kill(pid, SIGINT);

	/* Wait two seconds just to make sure that none of the specified
	 * signals interrupt the process (They should all be blocked).
	 */
	sleep(2);

	/* Change the process signal mask:
	 *
	 * Now allow the SIGINT signal to interrupt the process.
	 * Thus by using sigpause (), force the process to suspend
	 * execution until delivery of an unblocked signal (here SIGINT).
	 *
	 * Additionally, verify that the SIGINT signal was received.
	 */
	valid_sig[SIGINT] = 1;

	printf
	    ("\n\tChange signal mask & wait until signal interrupts process\n");
	if (sigpause(SIGINT) != -1 || errno != 4)
		sys_error("sigpause failed", __LINE__);

	if (valid_sig[SIGINT])
		error("failed to receive SIGINT signal!", __LINE__);

	/* Program completed successfully -- exit */
	printf("\nsuccessful!\n");

	return (0);
}
Exemplo n.º 25
0
int main(int argc, char *argv[])
{
#if !defined(NOALARM)
   struct itimerval new_timer, old_timer;
#endif /* !NOALARM */
   struct rlimit rlp;
#if defined(USE_SIGACTION)
   struct sigaction sigact;
#endif
   struct sockaddr_in sadd;

   getrlimit(RLIMIT_NOFILE, &rlp);
   rlp.rlim_cur = rlp.rlim_max;
   setrlimit(RLIMIT_NOFILE, &rlp);

   /* First things first, let the client know we're alive */
   write(1, SERVER_CONNECT_MSG, strlen(SERVER_CONNECT_MSG));

   /* Need this to keep in sync with the client side
    *
    * <start><ident_id><local_port><sin_family><s_addr><sin_port>
    */
   req_size = sizeof(char) + sizeof(ident_identifier) + sizeof(short int)
                + sizeof(sadd.sin_family) + sizeof(sadd.sin_addr.s_addr)
                + sizeof(sadd.sin_port);

   /* Set up signal handling */
#if defined( USE_SIGACTION )
#if defined( USE_SIGEMPTYSET )
  sigemptyset(&(sigact.sa_mask));
#if !defined( FREEBSD ) && !defined( GLIBC )
  sigact.sa_sigaction = 0;
#endif
#else
  sigact.sa_mask = 0;
#if defined ( LINUX )
   sigact.sa_restorer = (void *) 0;
#endif
#endif /* USE_SIGEMPTYSET */
 sigact.sa_handler = catch_sigterm;
 sigaction(SIGTERM, &sigact, (struct sigaction *) 0);
#if !defined ( NOALARM )
  sigact.sa_handler = catch_sigalrm;
  sigaction(SIGALRM, &sigact, (struct sigaction *) 0);
#endif /* !NOALARM */
 sigact.sa_handler = SIG_IGN;
 sigaction(SIGPIPE, &sigact, (struct sigaction *) 0);
#else /* !USE_SIGACTION */
 signal(SIGTERM, catch_sigterm);
#if !defined(NOALARM)
  signal(SIGALRM, catch_sigalrm);
#endif /* !NOALARM */
 signal(SIGPIPE, SIG_IGN);
#endif /* USE_SIGACTION */

#if !defined(NOALARM)
   beats_per_second = 5;
   /* Set up a timer to wake us up now and again */
   new_timer.it_interval.tv_sec = 0;
   new_timer.it_interval.tv_usec = 1000000 / beats_per_second;
   new_timer.it_value.tv_sec = 0;
   new_timer.it_value.tv_usec = new_timer.it_interval.tv_usec;
   if (0 > setitimer(ITIMER_REAL, &new_timer, &old_timer))
   {
      perror("ident");
   }
#endif /* !NOALARM */
#if defined(HAVE_BZERO)
#ifdef SUNOS
    bzero((char *)&idents_in_progress[0], MAX_IDENTS_IN_PROGRESS * sizeof(ident_request));
#elif OSF
    bzero((void *)&idents_in_progress[0], MAX_IDENTS_IN_PROGRESS * sizeof(ident_request));
#else
    bzero(&idents_in_progress[0], MAX_IDENTS_IN_PROGRESS * sizeof(ident_request));
#endif
#else /* !HAVE_BZERO */
   memset(&idents_in_progress[0], 0,
        MAX_IDENTS_IN_PROGRESS * sizeof(ident_request));
#endif /* HAVE_BZERO */
   /* Now enter the main loop */
   status = STATUS_RUNNING;
   while (status != STATUS_SHUTDOWN)
   {
      if (1 == getppid())
      {
      /* If our parent is now PID 1 (init) the talker must have died without
       * killing us, so we have no business still being here
       *
       * time to die...
       */
         exit(0);
      }
      check_requests();
      check_connections();
#if !defined(NOALARM)
      sigpause(0);
#endif /* !NOALARM */
   }

   return 0;
}
Exemplo n.º 26
0
Arquivo: tty.c Projeto: Distrotech/joe
static void pauseit(void)
{
	sigpause(0);
}
Exemplo n.º 27
0
/** This method controls the standard timer. After the arrival of a signal it
    checks which messages should be sent / received by the clients and handles
    them appropriately. */
void
StandardTimer::run()
{
    double lcmt = 0.0;

    // the sequence of messages will always repeat itself after lcm_st ms.
    // the number of messages that should be processed before the sequence
    // "quits" thus equals (lcm_st / x) where x is the time interval for the
    // specific message
    // Example: lcm_st = 300, sim_st = 100, q_simt = 300 / 100 = 3.
    //          three simulations steps have to occur in one sequence
    // in the default case the listed variables will have the following values
    // q_simt = 3  (simulation step)
    // q_sent = 8  (sending visual information - low quality, narrow width)
    // q_rect = 30 (process arrival of messages)
    // q_sbt  = 3  (sense body step)
    // q_svt  = 3  (coach look interval)
    // c_* will contain the number of processed messages in this sequence
    int c_simt = 1, c_sent = 1, c_rect = 1, c_sbt = 1, c_svt = 1,
        q_simt = ServerParam::instance().lcmStep() / ServerParam::instance().simStep(),
        q_sent = ServerParam::instance().lcmStep() / ServerParam::instance().sendStep()*4,
        q_rect = ServerParam::instance().lcmStep() / ServerParam::instance().recvStep(),
        q_sbt = ServerParam::instance().lcmStep() / ServerParam::instance().senseBodyStep(),
        q_svt = ServerParam::instance().lcmStep() / ServerParam::instance().coachVisualStep();
    int c_synch_see = 1;
    bool sent_synch_see = false;

    // create a timer that will be called every TIMEDELTA msec. Each time
    // this timer is called, lcmt is raised and it is checked which message
    // should be processed according to the part of the sequence we are in

#ifdef RCSS_WIN
    HANDLE gDoneEvent = NULL;
    initTimer(gDoneEvent);
#else
    struct itimerval itv_prev;
    struct sigaction alarm_prev;
    initTimer( itv_prev, alarm_prev );
#endif
    //for (;;)
    while ( getTimeableRef().alive() )
    {
#ifdef RCSS_WIN
        if ( WaitForSingleObject(gDoneEvent, INFINITE) != WAIT_OBJECT_0 )
            printf( "WaitForSingleObject failed (%d)\n", GetLastError() );
#else
        if ( ! gotsig )
            sigpause( SIGUSR1 );
#endif
        gotsig = false;

        lock_timedelta = true;
        lcmt += timedelta;
        timedelta = 0;
        lock_timedelta = false;

        if ( lcmt >= ServerParam::instance().simStep() * c_simt )
        {
            c_simt = static_cast< int >( std::floor( lcmt / ServerParam::instance().simStep() ) );
            lcmt = ServerParam::instance().simStep() * c_simt;
            // the above lines are needed to
            // prevent short "catch up" cycles if
            // the current cycle was slow
        }

        // receive messages
        if ( lcmt >= ServerParam::instance().recvStep() * c_rect )
        {
            getTimeableRef().recvFromClients();
            c_rect = static_cast< int >( std::floor( lcmt / ServerParam::instance().recvStep() ) );
            if ( q_rect <= c_rect )
                c_rect = 1;
            else
                c_rect++;
        }

        // update after new simulation step
        if ( lcmt >= ServerParam::instance().simStep() * c_simt )
        {
            getTimeableRef().newSimulatorStep();
            if ( q_simt <= c_simt )
                c_simt = 1;
            else
                c_simt++;
            sent_synch_see = false;
        }

        // send sense body
        if ( lcmt >= ServerParam::instance().senseBodyStep() * c_sbt )
        {
            getTimeableRef().sendSenseBody();
            c_sbt = static_cast< int >( std::floor( lcmt / ServerParam::instance().senseBodyStep() ) );
            if ( q_sbt <= c_sbt )
                c_sbt = 1;
            else
                c_sbt++;
        }

        // send visual messages
        if ( lcmt >= ( ServerParam::instance().sendStep() * 0.25 ) * c_sent )
        {
            getTimeableRef().sendVisuals();
            c_sent = static_cast< int >( std::floor( lcmt/(ServerParam::instance().sendStep() * 0.25 ) ) );
            if ( q_sent <= c_sent )
                c_sent = 1;
            else
                c_sent++;
        }

        // send synch visual message
        if ( ! sent_synch_see
             && lcmt >= ( ServerParam::instance().simStep() * ( c_synch_see - 1 )
                          + ServerParam::instance().synchSeeOffset() ) )
        {
            //std::cerr << "lcmt=" << lcmt << "  c_synch_see=" << c_synch_see << '\n';
            getTimeableRef().sendSynchVisuals();
            ++c_synch_see;
            sent_synch_see = true;
        }

        // send coach look messages
        if ( lcmt >= ServerParam::instance().coachVisualStep() * c_svt )
        {
            getTimeableRef().sendCoachMessages();
            c_svt = static_cast< int >( std::floor( lcmt / ServerParam::instance().coachVisualStep() ) );
            if ( q_svt <= c_svt )
                c_svt = 1;
            else
                c_svt++;
        }

        if ( lcmt >= ServerParam::instance().lcmStep() )
        {
            lcmt = 0;
            c_synch_see = 1;
        }
    }
#ifdef RCSS_WIN
#else
    restoreTimer( itv_prev, alarm_prev );
#endif
    getTimeableRef().quit();
}
Exemplo n.º 28
0
int main(int argc, char *argv[]) {

	int parent_pid = getpid();
	int worker_pid;
	if(argc != 2){
		printf(1,"Needs 2 arguments... Try again.\n");
		exit();
	}
	int n = atoi(argv[1]);
	if(n > NPROC){
		printf(1,"Too many workers, can't handle it!!!! Goodbye...\n");
		exit();
	}

	int i = 0;
	sigset(&parent_handler);
	printf(1, "workers pids:\n");
	for (i = 0; i < n; i++) { //creating n processes
		worker_pid = fork();
		if (worker_pid < 0)
			break;
		if (worker_pid == 0) { //child process
			sigset(&worker_handler); // init sig_handler of the child process
			while (1) {
				sigpause();
			}
		} else {
			workers[i].pid = worker_pid;
			workers[i].free = 1;
			printf(1, "%d\n", worker_pid);

		}
	}
	if (getpid() == parent_pid) {
		char buf[128];
		for (;;) {
//			sleep(1);
			printf(1, "please enter a number: ");
			read(1, buf, 128);
			if (strlen(buf) == 1 && buf[0] == '\n')
				continue;
			int x = atoi(buf);
			if (x < 0){ //Doesn't support negative numbers (or bigger than max_integer)
				x = 1;
				printf(1,"Primsrv doesn't support your number :( changing it to 1...\n");
			}
			memset(buf, '\0', 128);
			if (x == 0) { //need to exit program
				for (i = 0; i < n; i++) {
					sigsend(workers[i].pid, x);
				}
				int pid;
				while ((pid = wait()) > -1)
					printf(1, "worker %d exit\n", pid);
				printf(1, "primsrv exit\n");
				break;
			} else { //search for idle worker
				i = 0;
				while (i < n && !workers[i].free) {

					i++;
				}
				if (i == n) {
					printf(1, "no idle workers\n");
					continue;
				} else {
					workers[i].free = 0;
					workers[i].last_number = x;
					sigsend(workers[i].pid, x);
				}
			}
		}
	}
	exit();
}
Exemplo n.º 29
0
int
main(int argc, char *argv[])
{
	struct athstatfoo *wf;
	const char *ifname;
	int c, banner = 1;

	ifname = getenv("ATH");
	if (ifname == NULL)
		ifname = "ath0";
	wf = athstats_new(ifname, getfmt("default"));
	while ((c = getopt(argc, argv, "bi:lo:z")) != -1) {
		switch (c) {
		case 'b':
			banner = 0;
			break;
		case 'i':
			wf->setifname(wf, optarg);
			break;
		case 'l':
			wf->print_fields(wf, stdout);
			return 0;
		case 'o':
			wf->setfmt(wf, getfmt(optarg));
			break;
		case 'z':
			wf->zerostats(wf);
			break;
		default:
			errx(-1, "usage: %s [-a] [-i ifname] [-l] [-o fmt] [-z] [interval]\n", argv[0]);
			/*NOTREACHED*/
		}
	}
	argc -= optind;
	argv += optind;

	if (argc > 0) {
		u_long interval = strtoul(argv[0], NULL, 0);
		int line, omask;

		if (interval < 1)
			interval = 1;
		signal(SIGALRM, catchalarm);
		signalled = 0;
		alarm(interval);
	banner:
		if (banner)
			wf->print_header(wf, stdout);
		line = 0;
	loop:
		if (line != 0) {
			wf->collect_cur(wf);
			wf->print_current(wf, stdout);
			wf->update_tot(wf);
		} else {
			wf->collect_tot(wf);
			wf->print_total(wf, stdout);
		}
		fflush(stdout);
		omask = sigblock(sigmask(SIGALRM));
		if (!signalled)
			sigpause(0);
		sigsetmask(omask);
		signalled = 0;
		alarm(interval);
		line++;
		if (line == 21)		/* XXX tty line count */
			goto banner;
		else
			goto loop;
		/*NOTREACHED*/
	} else {
		wf->collect_tot(wf);
		wf->print_verbose(wf, stdout);
	}
	return 0;
}
Exemplo n.º 30
0
static int start()
{
	int	lockfd;
	int	pipe3fd[2];
	char	temp_buf;

	lockfd=ll_daemon_start(LOCKFILE);
	if (lockfd <= 0)
	{
		close(3);
		return (lockfd);
	}

	if (lockfd == 3)
	{
		lockfd=dup(lockfd);	/* Get it out of the way */
		if (lockfd < 0)
		{
			perror("dup");
			return (-1);
		}
	}
	if (reserve3(pipe3fd))
	{
		return (1);
	}

	fcntl(lockfd, F_SETFD, FD_CLOEXEC);
	fcntl(pipe3fd[0], F_SETFD, FD_CLOEXEC);

	fcntl(3, F_SETFD, FD_CLOEXEC);	/* For the logger */
	clog_start_logger("courierfilter");
	clog_open_stderr(0);
	fcntl(3, F_SETFD, 0);

	signal(SIGPIPE, SIG_IGN);
	dup2(2, 1);
	sighup();
	close(0);
	open("/dev/null", O_RDONLY);

	close(3);
	if (read(pipe3fd[0], &temp_buf, 1) != 1)
		; /* ignore */
	close(pipe3fd[0]);

	signal(SIGHUP, sighuphandler);
	signal(SIGTERM, sigtermhandler);

	ll_daemon_started(PIDFILE, lockfd);

	while (!sigterm_received)
	{
		if (sighup_received)
		{
			sighup_received=0;
			if (reserve3(pipe3fd) == 0)
			{
				sighup();
				close(3);
				close(pipe3fd[0]);
			}
			signal(SIGHUP, sighuphandler);
		}
#if	HAVE_SIGSUSPEND

	{
	sigset_t	ss;

		sigemptyset(&ss);
		sigsuspend(&ss);
	}
#else
		sigpause(0);
#endif
	}
	while (filterlist)
		kill1filter(&filterlist);
	unlink(PIDFILE);
	return (0);
}