POSIX Interfaces

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

In: Root » Computers and technology » Linux

  Share  
|
  PL  |  NL  |  FR  |  ES  |  PT  |  IT  |  DE  |  DK  |  NO  |  SE  |  FI  |  GR  |  JP  |  CN  |  KR  |  RU  |  AE


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.

Share

Disclaimer

1) E-articles 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 infringement, please read the terms of service and contact us or use the "Report this article" button on this page to investigate the problem.
2) E-articles is not responsible for inaccuracies, falsehoods, or any other types of misinformation this article 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.