Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I receive data sent by sigqueue() syscall

I have 2 process sigserver and sigclient. sigserver waits for a signal to come and sigclient sends data (int+char) to sigserver.

sigserver.c

void sighand(int signo, siginfo_t *info, void *extra)
{
       void *ptr_val = info->si_value.sival_ptr;
       int int_val = info->si_value.sival_int;
       printf("Signal: %d, value: [%d] %s\n", signo, int_val, (char*)ptr_val);
}

int main()
{
        struct sigaction action;

        action.sa_flags = SA_SIGINFO;
        action.sa_sigaction = &sighand;

        if (sigaction(SIGUSR2, &action, NULL) == -1) {
                perror("sigusr: sigaction");
                return 0;
        }

        printf("Signal handler installed, waiting for signal\n");

        while(1) {sleep(2);}

        return 0;
}

sigclient.c

int main(int argc, char *argv[])
{
        union sigval value;
        int pid = atoi(argv[1]);

        value.sival_int = atoi(argv[2]);
        value.sival_ptr = (void*) strdup(argv[3]);
        if(sigqueue(pid, SIGUSR2, value) == 0) {
                printf("signal sent successfully!!\n");
        } else {
                perror("SIGSENT-ERROR:");
        }
        return 0;
}

now when I run client with below command ./client server_pid 15 teststring

server generates core dump.

My question is, how can a process read string sent by another process(not child).

like image 551
JohnG Avatar asked Aug 31 '13 13:08

JohnG


1 Answers

The address which is sent from one process to the other via the value of sival_ptr is not valid in the receiving process, as the two processes do not share the same address space.

Due to this undefined behaviour is invoked on the receiver side when accessing sival_ptr.

The logic used in the example shown would only work if receiver and sender were the same process and thus use the same address space.

Note: In contrast to sival_ptr the value of sival_int could be used on the receiver side, as its value is the data.


To send data more complex than a single integer from one process to another use different IPC mechanic, like shared memory, sockets, pipes, ...

like image 186
alk Avatar answered Nov 01 '22 20:11

alk