xref: /freebsd/lib/libulog/ulog_login_pseudo.c (revision 2208eadf43351a36e70377f3e796daf76a06b8d9)
12208eadfSEd Schouten /*-
22208eadfSEd Schouten  * Copyright (c) 2009 Ed Schouten <ed@FreeBSD.org>
32208eadfSEd Schouten  * All rights reserved.
42208eadfSEd Schouten  *
52208eadfSEd Schouten  * Redistribution and use in source and binary forms, with or without
62208eadfSEd Schouten  * modification, are permitted provided that the following conditions
72208eadfSEd Schouten  * are met:
82208eadfSEd Schouten  * 1. Redistributions of source code must retain the above copyright
92208eadfSEd Schouten  *    notice, this list of conditions and the following disclaimer.
102208eadfSEd Schouten  * 2. Redistributions in binary form must reproduce the above copyright
112208eadfSEd Schouten  *    notice, this list of conditions and the following disclaimer in the
122208eadfSEd Schouten  *    documentation and/or other materials provided with the distribution.
132208eadfSEd Schouten  *
142208eadfSEd Schouten  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
152208eadfSEd Schouten  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
162208eadfSEd Schouten  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172208eadfSEd Schouten  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
182208eadfSEd Schouten  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
192208eadfSEd Schouten  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
202208eadfSEd Schouten  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212208eadfSEd Schouten  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222208eadfSEd Schouten  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232208eadfSEd Schouten  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
242208eadfSEd Schouten  * SUCH DAMAGE.
252208eadfSEd Schouten  */
262208eadfSEd Schouten 
272208eadfSEd Schouten #include <sys/cdefs.h>
282208eadfSEd Schouten __FBSDID("$FreeBSD$");
292208eadfSEd Schouten 
302208eadfSEd Schouten #include <sys/wait.h>
312208eadfSEd Schouten 
322208eadfSEd Schouten #include <errno.h>
332208eadfSEd Schouten #include <signal.h>
342208eadfSEd Schouten #include <stdlib.h>
352208eadfSEd Schouten #include <sysexits.h>
362208eadfSEd Schouten #include <unistd.h>
372208eadfSEd Schouten 
382208eadfSEd Schouten #include "ulog_internal.h"
392208eadfSEd Schouten 
402208eadfSEd Schouten #define	_PATH_ULOG_HELPER	"/usr/libexec/ulog-helper"
412208eadfSEd Schouten 
422208eadfSEd Schouten /*
432208eadfSEd Schouten  * Registering login sessions.
442208eadfSEd Schouten  */
452208eadfSEd Schouten 
462208eadfSEd Schouten static void
472208eadfSEd Schouten ulog_exec_helper(int fd, char const * const argv[])
482208eadfSEd Schouten {
492208eadfSEd Schouten 	sigset_t oblock, nblock;
502208eadfSEd Schouten 	pid_t pid, wpid;
512208eadfSEd Schouten 	int status;
522208eadfSEd Schouten 
532208eadfSEd Schouten 	/* Block SIGCHLD. */
542208eadfSEd Schouten 	sigemptyset(&nblock);
552208eadfSEd Schouten 	sigaddset(&nblock, SIGCHLD);
562208eadfSEd Schouten 	sigprocmask(SIG_BLOCK, &nblock, &oblock);
572208eadfSEd Schouten 
582208eadfSEd Schouten 	switch (pid = fork()) {
592208eadfSEd Schouten 	case -1:
602208eadfSEd Schouten 		break;
612208eadfSEd Schouten 	case 0:
622208eadfSEd Schouten 		/* Execute helper program. */
632208eadfSEd Schouten 		if (dup2(fd, STDIN_FILENO) == -1)
642208eadfSEd Schouten 			exit(EX_UNAVAILABLE);
652208eadfSEd Schouten 		sigprocmask(SIG_SETMASK, &oblock, NULL);
662208eadfSEd Schouten 		execv(_PATH_ULOG_HELPER, __DECONST(char * const *, argv));
672208eadfSEd Schouten 		exit(EX_UNAVAILABLE);
682208eadfSEd Schouten 	default:
692208eadfSEd Schouten 		/* Wait for helper to finish. */
702208eadfSEd Schouten 		do {
712208eadfSEd Schouten 			wpid = waitpid(pid, &status, 0);
722208eadfSEd Schouten 		} while (wpid == -1 && errno == EINTR);
732208eadfSEd Schouten 		break;
742208eadfSEd Schouten 	}
752208eadfSEd Schouten 
762208eadfSEd Schouten 	sigprocmask(SIG_SETMASK, &oblock, NULL);
772208eadfSEd Schouten }
782208eadfSEd Schouten 
792208eadfSEd Schouten void
802208eadfSEd Schouten ulog_login_pseudo(int fd, const char *host)
812208eadfSEd Schouten {
822208eadfSEd Schouten 	char const * const args[4] = { "ulog-helper", "login", host, NULL };
832208eadfSEd Schouten 
842208eadfSEd Schouten 	ulog_exec_helper(fd, args);
852208eadfSEd Schouten }
862208eadfSEd Schouten 
872208eadfSEd Schouten void
882208eadfSEd Schouten ulog_logout_pseudo(int fd)
892208eadfSEd Schouten {
902208eadfSEd Schouten 	char const * const args[3] = { "ulog-helper", "logout", NULL };
912208eadfSEd Schouten 
922208eadfSEd Schouten 	ulog_exec_helper(fd, args);
932208eadfSEd Schouten }
94