E-MailRelay
Classes | Static Public Member Functions | List of all members
G::Process Class Reference

A static interface for doing things with processes. More...

#include <gprocess.h>

Classes

class  Id
 Process-id class. More...
 
class  Umask
 Used to temporarily modify the process umask. More...
 
class  UmaskImp
 

Static Public Member Functions

static void closeFiles (bool keep_stderr=false)
 Closes all open file descriptors and reopen stdin, stdout and possibly stderr to the null device. More...
 
static void closeStderr ()
 Closes stderr and reopens it to the null device. More...
 
static void closeOtherFiles (int fd_keep=-1)
 Closes all open file descriptors except the three standard ones and possibly one other. More...
 
static void cd (const Path &dir)
 Changes directory. More...
 
static bool cd (const Path &dir, std::nothrow_t)
 Changes directory. Returns false on error. More...
 
static int errno_ (const SignalSafe &=G::SignalSafe()) noexcept
 Returns the process's current 'errno' value. More...
 
static void errno_ (int e_new) noexcept
 Sets the process's 'errno' value. More...
 
static int errno_ (const SignalSafe &, int e_new) noexcept
 Sets the process's 'errno' value. More...
 
static std::string strerror (int errno_)
 Translates an 'errno' value into a meaningful diagnostic string. More...
 
static std::pair< Identity, IdentitybeOrdinaryAtStartup (const std::string &nobody, bool change_group)
 Revokes special privileges (root or suid) at startup, possibly including extra group membership, making the named user the effective identity. More...
 
static void beOrdinary (Identity ordinary_id, bool change_group)
 Releases special privileges. More...
 
static void beSpecial (Identity special_id, bool change_group=true)
 Re-acquires special privileges (either root or suid). More...
 
static void beSpecialForExit (SignalSafe, Identity special_id) noexcept
 A signal-safe version of beSpecial() that should only be used just before process exit. More...
 
static void beOrdinaryForExec (Identity run_as_id) noexcept
 Sets the real and effective user-id and group-ids to those given, on a best-effort basis. More...
 
static void setEffectiveUser (Identity)
 Sets the effective user-id. Throws on error. More...
 
static void setEffectiveGroup (Identity)
 Sets the effective group-id. Throws on error. More...
 
static std::string cwd (bool no_throw=false)
 Returns the current working directory. More...
 
static std::string exe ()
 Returns the absolute path of the current executable, independent of the argv array passed to main(). More...
 

Detailed Description

A static interface for doing things with processes.

See also
G::Identity

Definition at line 49 of file gprocess.h.

Member Function Documentation

◆ beOrdinary()

void G::Process::beOrdinary ( Identity  ordinary_id,
bool  change_group 
)
static

Releases special privileges.

If the real-id is root then the effective user-id is changed to whatever is passed in. Otherwise the effective user-id is changed to the real user-id (optionally including the group), and the identity parameter is ignored.

Logs an error message and throws on failure, resulting in a call to std::terminate() when called from a destructor (see G::Root).

This affects all threads in the calling processes, with signal hacks used in some implementations to do the synchronisation. This can lead to surprising interruptions of sleep(), select() etc.

See also class G::Root.

Definition at line 173 of file gprocess_unix.cpp.

◆ beOrdinaryAtStartup()

std::pair< G::Identity, G::Identity > G::Process::beOrdinaryAtStartup ( const std::string &  nobody,
bool  change_group 
)
static

Revokes special privileges (root or suid) at startup, possibly including extra group membership, making the named user the effective identity.

Returns the new effective identity and the original effective identity as a pair.

< auto pair = Process::beOrdinaryAtStartup( "daemon" , chgrp ) ;
< Process::beSpecial( pair.second , chgrp ) ;
< doPrivilegedStuff() ;
< Process::beOrdinary( pair.first , chgrp ) ;
<
static std::pair< Identity, Identity > beOrdinaryAtStartup(const std::string &nobody, bool change_group)
Revokes special privileges (root or suid) at startup, possibly including extra group membership,...
static void beSpecial(Identity special_id, bool change_group=true)
Re-acquires special privileges (either root or suid).
static void beOrdinary(Identity ordinary_id, bool change_group)
Releases special privileges.

Definition at line 154 of file gprocess_unix.cpp.

◆ beOrdinaryForExec()

void G::Process::beOrdinaryForExec ( Identity  run_as_id)
staticnoexcept

Sets the real and effective user-id and group-ids to those given, on a best-effort basis.

Errors are ignored.

Definition at line 178 of file gprocess_unix.cpp.

◆ beSpecial()

void G::Process::beSpecial ( Identity  special_id,
bool  change_group = true 
)
static

Re-acquires special privileges (either root or suid).

The parameter must have come from a previous call to beOrdinaryAtStartup() and use the same change_group value.

See also class G::Root.

Definition at line 144 of file gprocess_unix.cpp.

◆ beSpecialForExit()

void G::Process::beSpecialForExit ( SignalSafe  ,
Identity  special_id 
)
staticnoexcept

A signal-safe version of beSpecial() that should only be used just before process exit.

Definition at line 149 of file gprocess_unix.cpp.

◆ cd() [1/2]

void G::Process::cd ( const Path dir)
static

Changes directory.

Definition at line 73 of file gprocess_unix.cpp.

◆ cd() [2/2]

bool G::Process::cd ( const Path dir,
std::nothrow_t   
)
static

Changes directory. Returns false on error.

Definition at line 79 of file gprocess_unix.cpp.

◆ closeFiles()

void G::Process::closeFiles ( bool  keep_stderr = false)
static

Closes all open file descriptors and reopen stdin, stdout and possibly stderr to the null device.

Definition at line 89 of file gprocess_unix.cpp.

◆ closeOtherFiles()

void G::Process::closeOtherFiles ( int  fd_keep = -1)
static

Closes all open file descriptors except the three standard ones and possibly one other.

Definition at line 102 of file gprocess_unix.cpp.

◆ closeStderr()

void G::Process::closeStderr ( )
static

Closes stderr and reopens it to the null device.

Definition at line 84 of file gprocess_unix.cpp.

◆ cwd()

std::string G::Process::cwd ( bool  no_throw = false)
static

Returns the current working directory.

Throws on error by default or returns the empty string.

Definition at line 197 of file gprocess_unix.cpp.

◆ errno_() [1/3]

int G::Process::errno_ ( const SignalSafe ,
int  e_new 
)
staticnoexcept

Sets the process's 'errno' value.

Returns the old value. Typically used in signal handlers.

Definition at line 129 of file gprocess_unix.cpp.

◆ errno_() [2/3]

int G::Process::errno_ ( const SignalSafe = G::SignalSafe())
staticnoexcept

Returns the process's current 'errno' value.

(Beware of destructors of c++ temporaries disrupting the global errno value.)

Definition at line 119 of file gprocess_unix.cpp.

◆ errno_() [3/3]

void G::Process::errno_ ( int  e_new)
staticnoexcept

Sets the process's 'errno' value.

Definition at line 124 of file gprocess_unix.cpp.

◆ exe()

std::string G::Process::exe ( )
static

Returns the absolute path of the current executable, independent of the argv array passed to main().

Returns the empty string if unknown.

Definition at line 242 of file gprocess_unix.cpp.

◆ setEffectiveGroup()

void G::Process::setEffectiveGroup ( Identity  id)
static

Sets the effective group-id. Throws on error.

Definition at line 191 of file gprocess_unix.cpp.

◆ setEffectiveUser()

void G::Process::setEffectiveUser ( Identity  id)
static

Sets the effective user-id. Throws on error.

Definition at line 184 of file gprocess_unix.cpp.

◆ strerror()

std::string G::Process::strerror ( int  errno_)
static

Translates an 'errno' value into a meaningful diagnostic string.

The returned string is non-empty, even for a zero errno.

Definition at line 136 of file gprocess_unix.cpp.


The documentation for this class was generated from the following files: