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 reopens 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 optionally one other. More...
 
static void inheritStandardFiles ()
 Makes sure that the standard file descriptors are inherited. 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::string errorMessage (DWORD error)
 Translates a GetLastError() 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 Path cwd ()
 Returns the current working directory. Throws on error. More...
 
static Path cwd (std::nothrow_t)
 Returns the current working directory. More...
 
static Path 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).

(Note that the identity switch applies to all threads in the calling processes, so some run-time libraries engage in signal shenanigans to synchronise the change across threads, which can lead to surprising interruptions of sleep(), select() etc.)

See also class G::Root.

Definition at line 186 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 167 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 191 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 157 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 162 of file gprocess_unix.cpp.

◆ cd() [1/2]

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

Changes directory.

Definition at line 74 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 80 of file gprocess_unix.cpp.

◆ closeFiles()

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

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

Definition at line 90 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 optionally one other.

Definition at line 104 of file gprocess_unix.cpp.

◆ closeStderr()

void G::Process::closeStderr ( )
static

Closes stderr and reopens it to the null device.

Definition at line 85 of file gprocess_unix.cpp.

◆ cwd() [1/2]

G::Path G::Process::cwd ( )
static

Returns the current working directory. Throws on error.

Definition at line 210 of file gprocess_unix.cpp.

◆ cwd() [2/2]

G::Path G::Process::cwd ( std::nothrow_t  )
static

Returns the current working directory.

Returns the empty path on error.

Definition at line 215 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 135 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 125 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 130 of file gprocess_unix.cpp.

◆ errorMessage()

std::string G::Process::errorMessage ( DWORD  error)
static

Translates a GetLastError() value into a meaningful diagnostic string.

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

Definition at line 151 of file gprocess_unix.cpp.

◆ exe()

G::Path G::Process::exe ( )
static

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

Returns the empty path if unknown.

Definition at line 265 of file gprocess_unix.cpp.

◆ inheritStandardFiles()

void G::Process::inheritStandardFiles ( )
static

Makes sure that the standard file descriptors are inherited.

Definition at line 118 of file gprocess_unix.cpp.

◆ setEffectiveGroup()

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

Sets the effective group-id. Throws on error.

Definition at line 204 of file gprocess_unix.cpp.

◆ setEffectiveUser()

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

Sets the effective user-id. Throws on error.

Definition at line 197 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 142 of file gprocess_unix.cpp.


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