L. Linux Processess - JulTob/Ada GitHub Wiki

The Linux process functions are part of the standard C library, and do not need to be linked in with -lc.

function getpid return integer;

Returns the Process Identification Number (PID) for your program.

Ownership

The owner of a program is referred to as the UID (user identification number). Under Linux, there are actually three owners to any given program: the effective UID, the real UID and the saved UID. Normally, these three are all the same login. The real and saved uids are provided for programs that must temporarily pretend to be somebody else, like a daemon that needs special login for a short period of time, or setuid/setgid programs that must temporarily switch between owners. These special functions are not covered here.

function getuid return integer;
pragma import( C, getuid );
-- Get the (real) UID of a process.
-- Example: Put_Line( "My UID is " & getuid'img );
 
function setuid (uid : integer ) return integer;
pragma import( C, setuid );
-- Change the effective (and saved and real) UID of a process to a new owner.

The GID (group identification number) is the group the program belongs to. In Linux, there's a main, effective group number, and any number of secondary groups that a program can belong to. There is also real and saved GIDs, just like UIDs.

procedure getgroups( result : out integer; num : integer; gidlist );
pragma import( C, getgroups);
pragma import_valued_procedure( getgroups );

Return a list of group numbers that a process belongs to. Gidlist is the address of a list of C strings.

function getgid return integer;
pragma import( C, getgid );
-- Get the (real) UID of the process.
-- Example: Put_Line( "My GID is " & getgid'img );
 
function setguid( gid : integer ) return integer;
pragma import( C, setgid );
-- Change the effective GID (and saved and real) of a process to a new group.
 

Linux also allows you to arrange processes into groups for easier management of multiple processes at once. Each process group as a, no surprise, a process group identification number (PGID).

function setpgid( pid, pgid : integer ) return integer;
pragma import( C, setpgid );

Place a process into a new process group. PID 0 is the current process. PGID 0 creates a new process group.

function getpgid( pid : intger ) return integer;
pragma import( C, getpgid );
-- Example: Put_Line( "My PGID is " & getpgid'img );

Returns the process group number for a process. PID 0 is the current process.

Every program and process group also belongs to a session (as in a login session). When you log off the computer, Linux automatically stops all programs that were running in your session. The session leader is the top process in a session, such as your login shell. However, if you want to create a new session for some reason, you can use the following function:

function setsid return integer;
pragma import( C, setsid );
-- Start a new session and return a new session identification number (SID).
-- Example: NewSID := etsid;

Other Functions

function kill( uid, signal : integer ) return integer;
pragma import( C, kill );

Stop a child process that your process has started, the same as using the kill command at the shell prompt. (More accuately, send a signal to a child process—some signals won't stop the child process.) Example:

Result := kill( MyRunawayChildUID, 15 ); -- send SIGTERM (terminate) signal

Signal handling, in general, is easier through Ada.Interrupts than through Linux kernel calls because of their heavy reliance on C macros.--KB

function alarm( seconds : Interfaces.C.unsigned ) return Interfaces.C.unsigned;
pragma import( C, alarm );

After the specified number of seconds, cause a SIGALRM interrupt in the current process.