FreeBSD/usr.sbin/jail/command.c
/*
 * Add a process to the hash, tied to a jail.
 */
Add a process to the hash, tied to a jail.
Add a process to the hash tied to a jail .
static int
add_proc(struct cfjail *j, pid_t pid)
{
	struct kevent ke;
	struct cfjail *tj;
	struct phash *ph;
	if (!kq && (kq = kqueue()) < 0)
		err(1, "kqueue");
	EV_SET(&ke, pid, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL);
	if (kevent(kq, &ke, 1, NULL, 0, NULL) < 0) {
		if (errno == ESRCH)
			return 0;
		err(1, "kevent");
	}
	ph = emalloc(sizeof(struct phash));
	ph->j = j;
	ph->pid = pid;
	LIST_INSERT_HEAD(&phash[pid % PHASH_SIZE], ph, le);
	j->nprocs++;
	j->flags |= JF_SLEEPQ;
	if (j->timeout.tv_sec == 0)
		requeue(j, &sleeping);
	else {
		TAILQ_REMOVE(j->queue, j, tq);
		TAILQ_FOREACH(tj, &sleeping, tq) {
			if (!tj->timeout.tv_sec ||
			    j->timeout.tv_sec < tj->timeout.tv_sec ||
			    (j->timeout.tv_sec == tj->timeout.tv_sec &&
			    j->timeout.tv_nsec <= tj->timeout.tv_nsec)) {
				TAILQ_INSERT_BEFORE(tj, j, tq);
				break;
			}
		}
		if (tj == NULL)
			TAILQ_INSERT_TAIL(&sleeping, j, tq);
		j->queue = &sleeping;
	}
	return 1;
}
static int add_proc ( struct cfjail * j , pid_t pid ) { struct kevent ke ; struct cfjail * tj ; struct phash * ph ; if ( ! kq && ( kq = kqueue ( ) ) < 0 ) err ( 1 , " " ) ; EV_SET ( & ke , pid , EVFILT_PROC , EV_ADD , NOTE_EXIT , 0 , NULL ) ; if ( kevent ( kq , & ke , 1 , NULL , 0 , NULL ) < 0 ) { if ( errno == ESRCH ) return 0 ; err ( 1 , " " ) ; } ph = emalloc ( sizeof ( struct phash ) ) ; ph -> j = j ; ph -> pid = pid ; LIST_INSERT_HEAD ( & phash [ pid % PHASH_SIZE ] , ph , le ) ; j -> nprocs ++ ; j -> flags |= JF_SLEEPQ ; if ( j -> timeout . tv_sec == 0 ) requeue ( j , & sleeping ) ; else { TAILQ_REMOVE ( j -> queue , j , tq ) ; TAILQ_FOREACH ( tj , & sleeping , tq ) { if ( ! tj -> timeout . tv_sec || j -> timeout . tv_sec < tj -> timeout . tv_sec || ( j -> timeout . tv_sec == tj -> timeout . tv_sec && j -> timeout . tv_nsec <= tj -> timeout . tv_nsec ) ) { TAILQ_INSERT_BEFORE ( tj , j , tq ) ; break ; } } if ( tj == NULL ) TAILQ_INSERT_TAIL ( & sleeping , j , tq ) ; j -> queue = & sleeping ; } return 1 ; }
Add a process to the hash, tied to a jail.
Add a process to the hash tied to a jail .
/* File the jail in the sleep queue according to its timeout. */