Error "Bad address" when reading from message queue on Linux
c, linux, message-queue, unix
Solution
After adding the `perror` it appears that you have got the error stating `"Bad Address"` (EFAULT) which means that "The address pointed to by msgp (buffer) isn't accessible". From the code it appears that there has been no memory allocated to `TimeMessage *t` so you can either allocate memory or just change it to `TimeMessage t` and pass `&t` instead of `t` to `msgrcv`. Also size should be `sizeof t` (assuming the change from `*t` to `t`, or `sizeof(TimeMessage)` for `*t`) instead of `sizeof(struct tm)` (& obviously you would change `printf` statement accordingly) Hope this helps!
Problem
I have assignment when I need to write simple time server and a client using Linux message queue. The server opens a message queue and the client sends a request with his PID (message with type 1) and the server reads that message and sends a message with type of PID (taken from the message read). I put all the code below because I don't know where I made the mistake. I'm not Linux programming expert. Don't even know if I written server correct. File that is included by server and client (I need to write it in this way). ``` #include <stdio.h> #include <stdlib.h> #include <time.h> #include <sys/msg.h> #include <sys/ipc.h> #include <sys/types.h> #include <string.h> #include <signal.h> #define QUEUE 100 #define PERM_ALL 0777 typedef struct my_msgbuf { long mtype; int pid; } ClientMessage; typedef struct my_msgbuf2 { long mtype; struct tm time; } TimeMessage; ``` Server ``` int m_queue; void cleanup(int signum) { if (msgctl(m_queue, IPC_RMID, NULL) == -1) { printf("Something happen on clean up\n"); exit(1); } exit(signum); } int main() { ClientMessage pid_message; TimeMessage t; time_t clock; struct tm *cur_time; if ((m_queue = msgget(QUEUE, PERM_ALL | IPC_CREAT)) == -1) { printf("Can't create and open message queue\n"); exit(1); } printf("created message queue = %d\n", m_queue); fflush(stdout); //t = malloc(sizeof(TimeMessage)); signal(SIGINT, cleanup); while (1) { if (msgrcv(m_queue, &pid_message, sizeof(pid_message.pid), 1, 0) == -1) { break; } else { t.mtype = pid_message.pid; clock = time(NULL); cur_time = localtime(&clock); memcpy(&t.time, cur_time, sizeof(struct tm)); msgsnd(m_queue, &t, sizeof(struct tm), 0); } } cleanup(0); } ``` Client ``` int main() { int m_queue; TimeMessage *t; ClientMessage client; if ((m_queue = msgget(QUEUE, PERM_ALL)) == -1) { perror("Error in opening queue"); exit(1); } client.mtype = 1; client.pid = getpid(); while (1) { if (msgsnd(m_queue, &client, sizeof(client.pid), 0) == -1) { perror("Error sending to queue"); exit(1); } else { if (msgrcv(m_queue, t, sizeof(struct tm), client.pid, 0) == -1) { perror("Error reading from queue"); exit(1); } printf("time: %d:%d:%d\n", t->time.tm_hour, t->time.tm_min, t->time.tm_sec); } } return 0; } ``` Both program compile without errors but client return "Error reading from queue" msgrcv is returning -1.