C Programming

C: sigaction function usage

A sigaction() is a function that allows to call/observe or examine a specific action associated with a particular signal. It is thought to consider a signal and sigaction function on the same page. But in reality, it has not occurred. The signal() function does not block other signals when the current handler’s execution is under process. At the same time, the sigaction function can block other signals until the current handler has returned.

Syntax

#include <signal.h>
# int sigaction(int sig, const struct sigaction *__restrict__ new, struct sigaction *__restrict__ old);

The sigaction function examines the action associated with a particular signal. We are now talking about the arguments in the parameter of the sigaction function. The first one is int ‘sig,’ as the data type reveals that it is a number that refers to the signal that is recognized already. The sigaction function sets the action and associates it with the signal. The sig argument that is used must be in the <signal.h> library.

Constant struct sigaction is a pointer that is first referred to as NULL mostly. If so, then the sigaction function examines the action that is defined currently to handle sig. This will not modify or cancel this action. And if the new pointer is not referred to as NULL value, it will point to a sigaction structure. The action identified specifically in the sigaction structure then becomes the new action associated with sig.

The third argument is struct sigaction *old it points out the memory address at which the sigaction function stores the sigaction structure. The sigaction function uses this memory address to store the structure that describes the activity currently associated with sig. The old value can be a NULL value; in that case, the sigaction structure is not stored as information.

Sigaction Structure

Now we will talk about the sigaction structure.

The members that are present in the structure are mentioned here.

Void (*) (int) sa_handler

It is a pointer of the function and handles the signal coming towards it. The value of this feature can show the default action or indicate the signal to be ignored.

Sigset_t mask

We cannot use the mechanism of blocking any signal; if sa_mask has used the blocking signals, these signals will be ignored. The sigaction() function does not return any error.

Int sa_flags

There are a lot of flags that affect the signal’s behavior. Some of them are explained here.

_Sa_ignore

This is the only output, and any application cannot specify it.

Sa-restart

It tells the system to get the library restart functions if they are interrupted by a signal. The functions on which this restart function is applied are those that are defined as to be interrupted by the signal and then set errno to EINTR that accept(), pause(), close(), and big pause() are those functions that are restartable if a signal interrupts them.

SA_SIGINFO

This feature tells the system to use the signal action specified by sa_sigaction instead of sa_handler. When the flag is off, and the action is affected by the signal, then the signal handler function specified by sa_handler is invoked as a void function (int signo); here, signo contains the signal number generated the system.

SA_SIGACTION

It is a pointer that points to the assigned function to handle the signal. This function contains three parameters. The first includes a type integer containing the function’s signal to get invoked. The second argument is a pointer type siginfo_t; it contains additional information about the signal source. The third one is the ‘pointer to void.’

Implementation of sigaction function

We have implemented the sigaction codes in Linux operating system. Use a text editor to write the source code in the C programming language; for the output of the resultant value, use the Ubuntu terminal by compiling through a GCC compiler. That compile and then execute the code.

Example 1

This example contains a list of libraries and . In this example, a signal mask is used and set with sigaction() to no calling for the other handler, especially when a single handler is in use. The signal handler function contains the signo as an argument.

The process id is displayed in the main program to ensure the working. Sigaction is a structure that describes a signal handler. This is the reason a signal handler is contained by it. A signal mask is used that shows that there should be blockage of a signal while the function of signal handling is in execution. Now specify the signal handler function when one specified signal has occurred.

# struct sigaction act;
# act.sa.handler = $sighandler;

After that, all the signals are added to the signal mask set. All the signals will be blocked when the signal handler functions are running. When our signal handler usually exists, it restores the original signal mask. SA_RESTART will resume the function. A signal handler is added to SIGINT.

After writing the code, compile it using the compiler in the Ubuntu terminal. “Sig.c” is the name of a file.

$ gcc –o sig. c
$./sig

Upon execution, you can see that the process id is displayed first whose signals need to be blocked. The displayed message means that the function will return a positive value.

Example 2

Now consider another example, in which the first part of the example determines if the SIGCHLD signal is currently being ignored. Using a NULL pointer for a new argument, the current signal handler is not changed. The main program contains the if statement to use the sigaction function; if the returned value is -1, it means the child is being ignored. And in the other part, if the returned value is the signal itself. It means that the child is being defaulted.

On execution, you will see that the child defaults.

Example 3

This example contains the same functionality as the second example, but some additional features are also added. Like a new handler is added in this function through sigaction.

Conclusion

The article ‘Sigaction function usage’ contains the function working and a detailed description of its parameters in the arguments. Sigaction plays a vital role in blocking those signals that are not recognized or are from hackers. That’s why most programmers prefer sigaction functions to be used in the programs. SA_FLAG is an essential argument of the sigaction function, so we have explained some important flags with basic functionality. Linux operating system applies for an important role in implementing the sigaction function in the C programming language. This guide aims to explain the sigaction features and the structure designed to target the signals approaching it.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.

Page was generated in 0.050854921340942