Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

380 Signals Chapter 10


It operates in a similar manner to thepsignalfunction. Although this function has
access to moreinformation than just the signal number,platforms vary in exactly what
additional information is printed.
If you only need the string description of the signal and don’t necessarily want to
write it to standarderror (you might want to write it to a log file, for example), you can
use thestrsignalfunction. This function is similar tostrerror(also described in
Section 1.7).

#include <string.h>
char *strsignal(intsigno);
Returns: a pointer to a string describing the signal

Given a signal number,strsignalwill return a string that describes the signal. This
string can be used by applications to print error messages about signals received.

All the platforms discussed in this book provide thepsignalandstrsignalfunctions, but
differences do occur.OnSolaris 10,strsignalwill return a null pointer if the signal number
is invalid, whereas FreeBSD 8.0, Linux 3.2.0, and Mac OS X 10.6.8 return a string indicating
that the signal number is unrecognized.
Only Linux 3.2.0 and Solaris 10 support thepsiginfofunction.

Solaris provides a couple of functions to map a signal number to a signal name, and
vice versa.

#include <signal.h>
int sig2str(intsigno,char *str);
int str2sig(const char *str,int *signop);
Both return: 0 if OK,−1 on error

These functions areuseful when writing interactive programs that need to accept and
print signal names and numbers.
Thesig2strfunction translates the given signal number into a string and stores
the result in the memory pointed to bystr.The caller must ensurethat the memory is
large enough to hold the longest string, including the terminating null byte. Solaris
provides the constantSIG2STR_MAXin<signal.h>to define the maximum string
length. The string consists of the signal name without the ‘‘SIG’’prefix. For example,
translatingSIGKILLwould result in the string ‘‘KILL’’ being stored in thestrmemory
buffer.
Thestr2sigfunction translates the given name into a signal number.The signal
number is stored in the integer pointed to bysignop.The name can be either the signal
name without the ‘‘SIG’’prefix or a string representation of the decimal signal number
(i.e., ‘‘9’’).
Note thatsig2strandstr2sigdepart from common practice and don’t set
errnowhen they fail.
Free download pdf