POSIX Interfaces

written by: Humberto Mitchson; article published: year 2007, month 11;



In: Categories » Computers and technology » Linux » POSIX Interfaces

POSIX defines several typedefs defined in the header file <sys/types.h> and used for many arguments and return values. These typedefs are important because the standard C types can vary from machine to machine and are loosely defined by the C standard. The C language is more useful on a wide range of hardware because of this loose definition—a 16-bit machine does not have the same native word size as a 64-bit machine, and a low-level programming language should not pretend it does—but POSIX needs more guarantees, and so requires that the C library's <sys/types.h> header file define a set of consistent types for each machine that implements POSIX. Each of these typedefs can be easily distinguished from a native C type because it ends in _t.

The subset used for interfaces is:

dev_t An arithmetic type holding the major and minor numbers corresponding to device special files, normally found in the /dev subdirectory. In Linux, a dev_t can be manipulated using the major(), minor(), and makedev() macros found in <sys/sysmacros. h>. It is normally used only for system programming.
uid_t, gid_t Integer types holding a unique user ID number or group ID number, respectively.
pid_t An integer type providing a unique value for a process on a system.
id_t An integer type capable of holding, without truncation, any pid_t, uid_t, or gid_t.
off_t A signed integer type measuring a file size in bytes.
size_t An unsigned integer type measuring an in-memory object, such as a character string, array, or buffer.
ssize_t A signed integer type that holds a count of bytes (positive) or an error return code (negative).
time_t An integer (on all normal systems) or real floating point (so that VMS can be considered a POSIX operating system) type giving the time in seconds.


The type descriptions are intentionally vague. There is no guarantee that the types will be the same on two different Linux platforms, or even two different environments running on the same platform. It is quite likely that a 64-bit machine that supports both 64-bit and 32-bit environments will have different values in each environment for some of these types. Also, these types may change in future versions of Linux, within the scope allowed by POSIX.

Discovering Run-Time Capabilities

Many system capabilities have limits, others are optional, and some may have information associated with them. A limit on the length of the string of arguments passed to a new program protects the system from arbitrary demands for memory that could otherwise bring the system to a standstill. Not all POSIX systems implement job control. A program may wish to know the most recent version of the POSIX standard the currently running system claims to implement.

The sysconf() function provides this type of system-specific information that may differ from system to system for a single executable, information that cannot be known at the time the executable is compiled.

#include <unistd.h>    
long sysconf(int); 

The integer argument to sysconf() is one of a set of macros prefixed with _SC_. Here are the ones that are most likely to be useful to you:

_SC_CLK_TCK Return the number of kernel internal clock ticks per second, as made visible to programs. Note that the kernel may have one or more clocks that run at a higher rate; _SC_CLK_TCK provides the accounting clock tick unit used to report information from the kernel and is not an indicator of system latency.
_SC_STREAM_MAX Return the maximum number of C standard I/O streams that a process can have open at once.
_SC_ARG_MAX Return the maximum length, in bytes, of the command-line arguments and environment variables used by any of the exec() functions. If this limit is exceeded, E2BIG is returned by the exec() call.
_SC_OPEN_MAX Returns the maximum number of files that a process can have open at once; it is the same as the RLIMIT_NOFILE soft limit that can be queried by geTRlimit() and set by setrlimit(). This is the only sysconf() value that can change value during the execution of a program; when setrlimit() is called to change the RLIMIT_NOFILE soft limit, _SC_OPEN_MAX follows the new soft limit.
_SC_PAGESIZE or _SC_PAGE_SIZE Returns the size of a single page in bytes. On systems that can support multiple page sizes, returns the size of a single normal page as allocated to resolve a normal user-space request for memory, which is considered the native page size for the system.
_SC_LINE_MAX Returns the length in bytes of the maximum line length that text-processing utilities on the system are required to handle, including the trailing newline character. Note that many of the GNU utilities used on Linux systems actually have no hard-coded maximum length and can take arbitrarily long input lines. However, a portable program must not provide text-processing utilities with text with line lengths longer than _SC_LINE_MAX; many Unix systems have utilities with fixed maximum line lengths, and exceeding this line length may produce undefined output.
_SC_NGROUPS_MAX Returns the number of supplemental groups a process can have.


Finding and Setting Basic System Information

There are a few pieces of information about the system on which a program is running that can be useful. The operating system name and version, for example, can be used to change what features system programs provide. The uname() system call allow a program to discover this run-time information.

#include <sys/utsname.h>    
int uname(struct utsname * unameBuf);  

The function returns nonzero on error, which occurs only if unameBuf is invalid. Otherwise, the structure it points to is filled in with NULL terminated strings describing the system the program is running on

Members of struct utsname

Member Description
sysname The name of the operating system running (Linux).
release The version number of the kernel that is running. This is the full version, such as 2.6.2. This number can be easily changed by whoever builds a kernel, and it is common for more than these three numbers to appear. Many distributions use an additional number to describe what patches they have applied, leading to release numbers like 2.4.17-23.
version Under Linux, this contains a time stamp describing when the kernel was built.
machine A short string specifying the type of microprocessor on which the operating system is running. This could be i686 for a Pentium Pro or later processor, alpha for an Alpha-class processor, or ppc64 for a 64-bit PowerPC processor.
nodename The host name of the machine, which is often the machine's primary Internet host name.
domainname The NIS(or YP)domain the machine is part of, if any.


The nodename member is what is commonly called the system host name (it is what the hostname command displays), but it should not be confused with an Internet host name. While these are the same on many systems, they are not necessarily the same thing. A system with multiple Internet addresses has multiple Internet host names, but only a single node name, so there is not a one-to-one equivalence.

A more common situation is home computers on broadband Internet connections. They normally have Internet host names something like host127-56.raleigh.myisp.com, and their Internet host names change whenever they have disconnected their broadband modem for an extended period of timePeople who own those machines give them node names hat better suit their personalities, along the lines of loren or eleanor, which are not proper Internet addresses at all. If they have multiple machines behind a home gateway device, all of those machines will share a single Internet address (and a single Internet host name), but may have names like linux.mynetwork.org and freebsd.mynetwork.org, which are still not Internet host names. For all of these reasons, assuming that the system's node name is a valid Internet host name for the machine is not a good idea.

Most, but not all, home Internet services assign dynamic IP addresses rather than static ones.

The system's node name is set using the sethostname() system call, and its NIS (YP) domain name is set by the setdomainname() system call.

Despite the misleading name, this system call sets the node name, not the machine's Internet host name.

Network Information Service, or NIS, is a mechanism for machines on a network to share information such as user names and passwords. It used to be called Yellow Pages, or YP, but was renamed. The NIS domain name is part of this mechanism, which is implemented outside of the kernel with the exception of the domain name being stored in struct utsname.

#include <unistd.h>    
int sethostname(const char * name, size_t len);  
int setdomainname(const char * name, size_t len);  

Both of these system calls take a pointer to a string (not necessarily NULL terminated) containing the appropriate name and the length of the string.

legal disclaimer

1) Our website is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringements, please read the Terms of service and contact us to investigate the problem.
2) The E-articles directory team is not responsible for inaccuracies, falsehoods, or any other types of misinformation this tutorial may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here. Please read the Terms of service

Useful tools and features

Translate this article to...    Send this article to you or to a friend

Link to this article from your page   
If you like this article (tutorial), please link to it from your web page using the information above. Linking to this page, this is the only way to help us improve our service, the same time providing your visitors with a way to improve their online experience.

related articles

1. Port Forwarding with SSH
Problem: Since many programs use services that send clear-text data over the network, it is desirable to find something that can be used to encrypt the network traffic for these services while minimizing any change to end users. SSH provides this functionality with port forwarding. Port forwarding allows a user to create an encrypted session from a client to a remote server for any TCP-based service by tunneling the service through SSH. Of course, this requires that the user have an account on the remote server and tha...

2. How to use PuTTY Passphrase Agents
STEP1: Use Pageant to store your private keys in memory To make public key authentication more convenient, the developers of PuTTY created Pageant. Pageant is a program included with PuTTY that will keep your decrypted private keys in memory so you only have to enter your passphrase once rather than every time you authenticate to a server using public key authentication. While this will make your day-to-day use more convenient, please keep in mind that it also poses a slight risk, since other applications (inc...

3. Interactively transfer files from the command line with PSFTP
One method to transfer files from the Windows command line is to use PSFTP. PSFTP creates an interactive SFTP file transfer session where you can use many of the commands available within a normal FTP session. Since PSFTP uses the SFTP protocol, which is only available with servers running protocol SSHv2, you may not be able to run it on every server. PSFTP is run from the command line and provides numerous options. To see the options available run PSFTP with the –h option: ...

4. Using Plink to initiate an SSH session from the command line or a script
Using PuTTY from the command line will create an SSH interactive session. This may not be what we want if for example we need to remain at the Windows command line or we want to issue an SSH command from within a script. In order to satisfy these types of needs, PuTTY provides a tool called Plink. Plink is a command line tool that will allow you to log in to a remote machine using SSH and either create an SSH session or execute a command, all from the command line and without opening another window. Plink comes with many comma...

5. How to Generate a Key Pair Using OpenSSH
Problem: How can a key-pair be created in OpenSSH?STEP1: Generating your public/private key-pairThe ssh-keygen command is utilized to generate your public and private keys. OpenSSH provides authentication methods via a choice of three public key "cryptosystems": RSA1, RSA, and DSA. RSA1 works with SSHv1 while RSA and DSA are for SSHv2. RSA and DSA use different techniques for authenticating and have different capabilities, but for purposes of this guide, either will suffice.To create a key-pair, r...

6. Transfer files from the command line with PSCP
A second method to transfer files from a Windows command line prompt is to use PSCP. Unlike PSFTP, PSCP is not interactive and is designed to transfer files "in one shot" and then exit, much like OpenSSH's scp command. PSCP also allows you to specify wildcards within filenames (PSFTP does not). Additionally, PSCP will work with any SSH server as it is not dependent on SSHv2 being present. Note  PSCP will blindly copy files to the remote server, overwriting any files with the same name, without prompting for veri...

7. Create an SSH session from the command line using PuTTY
There are multiple ways to create an SSH session from the command line using PuTTY. The first way involves using the PuTTY program itself. PuTTY comes with a number of options that can be used to invoke the graphical PuTTY terminal from the command line. A description of these options is available within the PuTTY help file. To run PuTTY from the command line: Note  ...

8. Install SSH Windows Clients to Access Remote Machines Securely
Problem: Many times administrators will find themselves on a Windows machine with no way to access a remote server securely since Microsoft does not yet package an SSH client. There are a number of excellent tools available that provide SSH client connectivity from a Windows platform. A list of these tools is available at http://www.openSSH.com/windows.html. ...

9. How to use OpenSSH Passphrase Agents
Problem: Using public key authentication makes logging in to a server with SSH more secure, but less convenient due to having to type in a longer and more complex passphrase. STEP1: Use ssh-agent and ssh-add to store your private keys in memory To make public key authentication more convenient to use, the OpenSSH developers created the ssh-agent and ssh-add programs. These programs are designed to keep your private keys decrypted in memory for your current session. With ssh-agent, you will not ne...

10. Buffer Overflow
A buffer overflow occurs when a program or process tries to store more data in a temporary data storage area than it was intended to hold. Since buffers are created to contain a finite amount of data, the extra information can overflow into adjacent buffers, corrupting or overwriting the valid data held in them. Buffer overflows are a fertile source of bugs and malicious attacks. They occur when a program attempts to write data past the end of a buffer. A buffer is a contiguous allocated chunk of memory, such as an array ...