35 void noCloseOnExec(
int fd )
37 ::fcntl( fd , F_SETFD , 0 ) ;
55 throw CannotChangeDirectory( dir.
str() ) ;
60 return 0 == ::chdir( dir.
str().c_str() ) ;
65 ::close( STDERR_FILENO ) ;
67 noCloseOnExec( STDERR_FILENO ) ;
72 closeFiles( keep_stderr ? STDERR_FILENO : -1 ) ;
77 G_ASSERT( keep == -1 || keep >= STDERR_FILENO ) ;
78 std::cout << std::flush ;
79 std::cerr << std::flush ;
82 long rc = ::sysconf( _SC_OPEN_MAX ) ;
84 n =
static_cast<int>( rc ) ;
86 for(
int fd = 0 ; fd < n ; fd++ )
98 if( keep != STDERR_FILENO )
102 noCloseOnExec( STDIN_FILENO ) ;
103 noCloseOnExec( STDOUT_FILENO ) ;
104 noCloseOnExec( STDERR_FILENO ) ;
121 char * p = std::strerror( errno_ ) ;
122 return std::string( p ? p :
"" ) ;
130 G_IGNORE_RETURN(
int) ::setgroups( 0U , &dummy ) ;
138 setEffectiveUserTo( special_identity ) ;
140 setEffectiveGroupTo( special_identity ) ;
141 return old_identity ;
148 setEffectiveUserTo( safe , special_identity ) ;
150 setEffectiveGroupTo( safe , special_identity ) ;
151 return old_identity ;
161 setEffectiveGroupTo( nobody ) ;
162 setEffectiveUserTo( nobody ) ;
170 return special_identity ;
180 setEffectiveGroupTo( safe , nobody ) ;
181 setEffectiveUserTo( safe , nobody ) ;
189 return special_identity ;
197 setRealGroupTo( nobody ) ;
198 setRealUserTo( nobody ) ;
213 int fd = ::open( path ? path :
"" , O_RDONLY ) ;
216 const size_t buffer_size = 11U ;
217 char buffer[buffer_size] ;
219 ssize_t rc = ::read( fd , buffer , buffer_size - 1U ) ;
221 for(
const char * p = buffer ; rc > 0 && *p >=
'0' && *p <=
'9' ; p++ , rc-- )
224 m_pid += ( *p -
'0' ) ;
233 throw Process::InvalidId() ;
238 std::ostringstream ss ;
245 return m_pid == other.m_pid ;
273 m_imp->
m_old_mode = ::umask( umask_value(mode) ) ;
278 G_IGNORE_RETURN(mode_t) ::umask( m_imp->m_old_mode ) ;
284 G_IGNORE_RETURN(mode_t) ::umask( umask_value(mode) ) ;
std::string str() const
Returns the path string.
bool isRoot() const
Returns true if the userid is zero.
A private implementation class used by G::Process::Umask.
An empty structure that is used to indicate a signal-safe, reentrant implementation.
static Identity root()
Returns the superuser identity.
static Identity real()
Returns the calling process's real identity.
static const char * nullDevice()
static void closeStderr()
Closes stderr.
A very low-level interface to getpwnam() and the get/set/e/uid/gid functions.
static Identity effective()
Returns the current effective identity.
static int errno_()
Returns the process's current 'errno' value.
static Identity beOrdinary(Identity nobody, bool change_group=true)
Revokes special privileges (root or suid).
static Identity beSpecial(Identity special, bool change_group=true)
Re-acquires special privileges (either root or suid).
bool operator==(const Id &) const
An overload discriminator for Process.
static void revokeExtraGroups()
Revokes secondary group memberships if really root or if suid.
static void cd(const Path &dir)
Changes directory.
static void closeFiles(bool keep_stderr=false)
Closes all open file descriptors.
static void beNobody(Identity)
If currently running with a real identity of root then the real identity is set to the nobody identit...
A private implementation class used by G::Process.
A Path object represents a file system path.
static std::string strerror(int errno_)
Translates an 'errno' value into a meaningful diagnostic string.