System Functions

Emulate System Library Functions

Serveral built-in functions enable you to execute various system library calls from within you Perl program, each one corresponds to a Unix system library function. To get more help on these functions use the Unix man pages.

getgrent, setgrent, endgrent getgrent - enables to retrieve an item from the user group file (/etc/group)
setgrent - tells the interpreter to rewind the /etc/group file to the beginning and keep it open
endgrent - tells perl thast you no longer need access to the /etc/group file
getgrnam retrieve the group file entry corresponding to a particular group name
getgrid retrieve the group file entry corresponding to a particular group ID
getnetent enables you to step through the /etc/services file
getnetgyaddr enables you to retrieve the line of input from the /etc/networks that matches a particular network number
getnetbyname enables you to retrieve the line of input from the /etc/networks that matches a particular network name
setnetent,endnetent setnetent - tells the interpreter to rewind the /etc/networks file to the beginning and keep it open
endnetent - tells perl thast you no longer need access to the /etc/networks file
gethostbyaddr searches the /etc/hosts file for the host name corresponding to a particular IP address
gethostbyname searches the /etc/hosts file for the host name corresponding to a particular Server name
gethostent, sethostent, endhostent gethostent - enables you to read each item of the /etc/hosts file in turn
sethostent - tells the interpreter to rewind the /etc/hosts file to the beginning and keep it open
endhostent - tells perl thast you no longer need access to the /etc/hosts file
getlogin returns the user ID under which you are logged in.
getpgrp, setpgrp getpgrp - retrieves the process group ID for a particular process
setpgrp - sets the process group ID for a particular process
getppid retrieve the parent process ID of your program, can be used with fork
getpwname enables you to retrieve the password entry in /etc/passwd for a particular user name
getpwuid enables you to retrieve the password entry in /etc/passwd for a particular user ID
getpwent, setpwent, endpwent getpwent - enable you to retrieve a single entry from the password file
setpwent - tells the interpreter to rewind the /etc/passwd file to the beginning and keep it open
endpwent - tells perl thast you no longer need access to the /etc/passwd file
getpriority, setpriority getpriority - retrieves the current priority for a process
setpriority - change the current priority of a process
getprotoent enables you to search for a particular protocol in the /etc/protocols files
getprotobyname enables you to search for a particular protocol in the /etc/protocols files using a name
getprotobynumber enables you to search for a particular protocol in the /etc/protocols files using a number
setprotoent, endprotoent setprotoent - tells the interpreter to rewind the /etc/protocols file to the beginning and keep it open
endprotoent - tells perl thast you no longer need access to the /etc/protocols file
getservent enables you to search for a particular service in the /etc/services files
getservbyname, getsrvbyport getservbyname - enables you to search for a particular service in the /etc/services files by service name
getservbyport - enables you to search for a particular service in the /etc/services files by service port number
setservent, endservent setservent - tells the interpreter to rewind the /etc/services file to the beginning and keep it open
endservent - tells perl thast you no longer need access to the /etc/services file
chroot enables you to specify the root directory for your program and any subprocesses that it creates
ioctl enables you to set system-dependent file attributes
alarm ends a special "alarm" signal SIGALARM to your prgram
select enables you to call the Unix select (not the Perl select statement)
dump generate a Unix core dump from within your program
Examples
getgrent, setgrent, endgrent

## getgrent
while( ($name,$passwd,$gid,$members) = getgrent() ) {
   print "Name = $name\n";
   print "Password = $passwd\n";
   print "GID = $gid\n";
   print "Members = $members\n";
}

## setgrent
setgrent();

## endgrent
endgrent();

getgrnam $group_name = 'root'
($name,$passwd,$gid,$members) = getgrnam($group_name);
getgrid $group_id = 0;
($name,$passwd,$gid,$members) = getgrid($group_id);
getnetent ($name, $altnames, $addrtype, $rawaddr) = getnetent();
getnetgyaddr

@addrbytes = (192,168,0,1);
$packedaddr = pack("C4", @addrbytes);
($name, $altnames, $addrtype, $addr) = getnetbyaddr($packedaddr, AF_NET);

Note: the address passed to getnetbyaddr must be a packed 4 byte integer, see pack for more information regarding packing

getnetbyname ($name, $altnames, $addrtype, $addr) = getnetbyaddr($server_name);
setnetent,endnetent

setnetent(<keepopen>);    ## keep open the networks file after getnetbyaddr or getnetbyname is
                          ## called

endnetent;                ## clode the /etc/networks file

gethostbyaddr @addrbytes = (192,168,0,1);
$packedaddr = pack("C4", @addrbytes);
($name, $altnames, $addrtype, $len $addr) = gethostbyaddr($packedaddr, AF_NET);
gethostbyname ($name, $altnames, $addrtype, $len $addr) = gethostbyaddr($server_name);
gethostent, sethostent, endhostent

## gethostent
($name, $altnames, $addrtype, $len $addr) = gethostent();

## sethostent
sethostent(<keepopen>);

## endhostent
endhostent();

getlogin $user_ID = getlogin();
getpgrp, setpgrp

## getpgrp
$prgroup = getpgrp(<pid>);

## setpgrp
setpgrp (<pid>,<grouppid>);

getppid $parent_PID = getppid();
getpwname ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwnam("root");

print "Name = $name\n";
print "Password = $passwd\n";
print "UID = $uid\n";
print "GID = $gid\n";
print "Quota = $quota\n";
print "Comment = $comment\n";
print "Gcos = $gcos\n";
print "HOME DIR = $dir\n";
print "Shell = $shell\n";
getpwuid ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwnam(0);
getpwent, setpwent, endpwent

## getpwent
($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwent();

## setpwent
setpwent(<keepopen>);

## endpwent
endpwent();

getpriority, setpriority

## getpriority
($username, password, $userid) = getpwnam("pvalle");
$current_priority = getpriority(2, $userid);

## setpriority
setpriority(2, $userid, $current_priority + 1);

getprotoent ($name, $alias, $number) = getprotoent();
getprotobyname ($name, $alias, $number) = getprotoent(<search_name>);
getprotobynumber ($name, $alias, $number) = getprotoent(<search_number>);
setprotoent, endprotoent

## setprotoent
setprotoent(<keepopen>);

## endprotoent
endprotoent();

getservent ($name, $aliases, $portnum, $protoname) = getservent();
getservbyname, getsrvbyport

## getservbyname
($name, $aliases, $portnum, $protoname) = getservbyname(<searchname>,<searchproto>););

## getservbyport
($name, $aliases, $portnum, $protoname) = getservbyport(<searchportnum>,<searchproto>););

setservent, endservent

## setservent
setservent(<keepopen>);

## endservent
endservent();

chroot chroot("/export/home/pvalle");
ioctl ioctl(<filevar>,<attribute>,<value>);
alarm alarm(30);              ## time is in seconds
select select (<rmask>,<wmask>,<emask>,<timeout>);
dump dump();

Working with Sockets

socket defines a socket and associates it with a file variable
bind bind the socket to a particular port
listen this waits for connections to be established with it.
accept have the process call accept the connection.
connect this connects to a port that is listening and accepting connections
shutdown enables you to indicate that traffic in one or both directions i no longer needed
socketpair creates a pair of sockets instead of just one
getsockopt, setsockopt obtain and set socket options
getsockname, getpeername enables you to retrieve the addresses of the two ends of a socket connection
Examples
socket, bind, listen, accept

## A simple server program
$line = "Hello World\n";

$port = 8888;

while (getservbyport ($port, "tcp")) {
   $port++;
}

($d1, $d2, $prototype) = getprotobyname("tcp");
($d1, $d2, $d3, $d4, $rawserver) = gethostbyname("xblade");

$serveraddr = pack("Sna4x8", 2, $port, $rawserver);

socket (SSOCKET, 2, 1, $protoype) || die("ERROR: unable to open socket\n");
bind (SSOCKET, $serveraddr) || die("ERROR: unable to bind to socket\n");
listen (SSOCKET, 1) || die("ERROR: unable to listen on socket\n");

($clientaddr = accept (SOCKET, SSOCKET)) || die("ERROR: unable to accept\n");

select(SOCKET);
$| = 1 ;
print SOCKET ("$line\n");
close(SOCKET);
close(SSOCKET);

connect

## A simple client program
$port = 8888;

while (getservbyport ($port, "tcp")) {
   $port++;
}

($d1, $d2, $prototype) = getprotobyname("tcp");
($d1, $d2, $d3, $d4, $rawclient) = gethostbyname("mercury");
($d1, $d2, $d3, $d4, $rawserver) = gethostbyname("xblade");

$clientaddr = pack("Sna4x8", 2, $port, $rawclient);
$serveraddr = pack("Sna4x8", 2, $port, $rawserver);

socket (SOCKET, 2, 1, $protoype) || die("ERROR: unable to open socket\n");
bind (SOCKET, $clientaddr) || die("ERROR: unable to bind to socket\n");
connect (SOCKET, $serveraddr) || die("ERROR: unable to listen on socket\n");

$line = <SOCKET>;
print("$line\n");
close(SOCKET);

shutdown

shutdown (SOCKET, <direction>);

Note : direction can be either 0, 1, 2
0 - indicates that the program can send through the socket but can no longer receive data
1 - indicates that the program can receive data from the socket but can no longer send
2 - indicates that both sending and receiving are disallowed

socketpair socketpair(<socket1>,<socket2>,<domain>,<type>,format>);
getsockopt, setsockopt $retval = getsockopt(<socket>,<opttype>,<optname>);
setsockopt(<socket>,<opttype>,<optname>);
getsockname, getpeername

## Local computer's socket address
$rawaddr = getsockname(<socket>);
($d1, $d2, @addrbytes) = unpack("SnC4x8", $rawaddr);
$readable = join(".", @addrbytes);

## Remotes computer's socket address
$rawaddr = getpeername(<socket>);
($d1, $d2, @addrbytes) = unpack("SnC4x8", $rawaddr);
$readable = join(".", @addrbytes);

Unix System V IPC Operations

The below are processes that intercommunicate via IPC calls, they use the following insteade of sockets

A semaphore is a method of ensuring that only one process can run a particular segment of code or access a particular chunk of shared memory storage at any given time.

msgget create a message queue ID to represent a particular message
msgsnd send a message to a message queue
msgrcv obtain a message from the queue
msgctl enable you to set options for message queues and send commands that affect them
shmget create the shared memeory
shmwrite to send data to a particular segment of shared memory
shmread to obtain data from a segment of shared ememory
shmctl enables you to set options for shared memory segments and send commands that affect them
semget create a semaphore
semop perform a semaphore option
semctl enables you to set options for semaphores
Examples
msgget $msgid = msgget (<key>,<flag>);
msgsnd msgsnd(<msgid>,<message>,<flags>);
msgrcv msgrcv (<msgid>,<message>,<size>,<mesgtype>,<flags>);
msgctl msgctl (<msgid>,<msgcmd>,<msgarg>);
shmget $shmid = shmget (<key>,<size>,<flag>);
shmwrite shmwrite (<shmid>,<text>,<pos>,<size>);
shmread shmread (<shmid>,$retval,<pos>,<size>);   ## $retval is the data returned
shmctl shmctl (<shmid>,<shmcmd>,<shmarg>);
semget $semid = semget (<key>,<num>,<flag>);
semop semop (<semid>,<semstructs>);             ## semstructs is a character string consisting of an
                                          ## array of semphore structures
semctl semctl (<semid>,<semcmd>,<semarg>);