2012-04-11 20:50:16 +04:00
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd .
Copyright 2011 Lennart Poettering
systemd is free software ; you can redistribute it and / or modify it
2012-04-12 02:20:58 +04:00
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation ; either version 2.1 of the License , or
2012-04-11 20:50:16 +04:00
( at your option ) any later version .
systemd is distributed in the hope that it will be useful , but
WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
2012-04-12 02:20:58 +04:00
Lesser General Public License for more details .
2012-04-11 20:50:16 +04:00
2012-04-12 02:20:58 +04:00
You should have received a copy of the GNU Lesser General Public License
2012-04-11 20:50:16 +04:00
along with systemd ; If not , see < http : //www.gnu.org/licenses/>.
* * */
# include <sys/types.h>
# include <stdlib.h>
# include <unistd.h>
# include <string.h>
# include <sys/prctl.h>
# include <signal.h>
# include <fcntl.h>
2012-04-12 00:37:13 +04:00
# include <errno.h>
# include <sys/poll.h>
2012-04-11 20:50:16 +04:00
# include "log.h"
# include "util.h"
# include "spawn-polkit-agent.h"
2013-02-14 01:56:43 +04:00
# ifdef ENABLE_POLKIT
2012-04-11 20:50:16 +04:00
static pid_t agent_pid = 0 ;
int polkit_agent_open ( void ) {
int r ;
2012-04-12 00:37:13 +04:00
int pipe_fd [ 2 ] ;
char notify_fd [ 10 + 1 ] ;
2012-04-11 20:50:16 +04:00
if ( agent_pid > 0 )
return 0 ;
/* We check STDIN here, not STDOUT, since this is about input,
* not output */
if ( ! isatty ( STDIN_FILENO ) )
return 0 ;
2012-04-12 00:37:13 +04:00
if ( pipe2 ( pipe_fd , 0 ) < 0 )
return - errno ;
snprintf ( notify_fd , sizeof ( notify_fd ) , " %i " , pipe_fd [ 1 ] ) ;
char_array_0 ( notify_fd ) ;
r = fork_agent ( & agent_pid ,
& pipe_fd [ 1 ] , 1 ,
POLKIT_AGENT_BINARY_PATH ,
2012-04-13 23:52:25 +04:00
POLKIT_AGENT_BINARY_PATH , " --notify-fd " , notify_fd , " --fallback " , NULL ) ;
2012-04-12 00:37:13 +04:00
/* Close the writing side, because that's the one for the agent */
2014-03-18 22:22:43 +04:00
safe_close ( pipe_fd [ 1 ] ) ;
2012-04-12 00:37:13 +04:00
2012-04-11 20:50:16 +04:00
if ( r < 0 )
2014-11-28 13:58:34 +03:00
log_error_errno ( - r , " Failed to fork TTY ask password agent: %m " ) ;
2012-04-12 00:37:13 +04:00
else
/* Wait until the agent closes the fd */
2014-07-29 14:23:31 +04:00
fd_wait_for_event ( pipe_fd [ 0 ] , POLLHUP , USEC_INFINITY ) ;
2012-04-12 00:37:13 +04:00
2014-03-18 22:22:43 +04:00
safe_close ( pipe_fd [ 0 ] ) ;
2012-04-11 20:50:16 +04:00
return r ;
}
void polkit_agent_close ( void ) {
if ( agent_pid < = 0 )
return ;
/* Inform agent that we are done */
kill ( agent_pid , SIGTERM ) ;
kill ( agent_pid , SIGCONT ) ;
2014-11-09 17:51:04 +03:00
( void ) wait_for_terminate ( agent_pid , NULL ) ;
2012-04-11 20:50:16 +04:00
agent_pid = 0 ;
}
2013-02-14 01:56:43 +04:00
# else
int polkit_agent_open ( void ) {
return 0 ;
}
void polkit_agent_close ( void ) {
}
# endif