-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path25.c
More file actions
106 lines (92 loc) · 3.53 KB
/
25.c
File metadata and controls
106 lines (92 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
============================================================================
Name : 25
Author : Piyush Singh
Description : Write a program to print a message queue's (use msqid_ds and ipc_perm structures)
a. access permission
b. uid, gid
c. time of last message sent and received
d. time of last change in the message queue
d. size of the queue
f. number of messages in the queue
g. maximum number of bytes allowed
h. pid of the msgsnd and msgrcv
Date: 26th Sep, 2025.
============================================================================
*/
//****************Flag****************
/*
I didn’t use info.__msg_cbytes because macOS doesn’t expose that field
in its public headers. While Linux defines msg_cbytes in msqid_ds to show the current
number of bytes in the message queue, macOS omits it due to partial System V IPC support.
Attempting to access __msg_cbytes results in a compiler error because it’s not defined in
/usr/include/sys/msg.h. Instead, I used msg_qnum to get the number of messages in the queue,
which is portable and supported across platforms. For full inspection of queue size in bytes,
I’d switch to a Linux environment where msg_cbytes is available.
*/
// Program: Inspect System V message queue attributes using msqid_ds
#include <stdio.h> // For printf(), perror()
#include <stdlib.h> // For exit()
#include <sys/ipc.h> // For ftok(), key_t
#include <sys/msg.h> // For msgget(), msgctl()
#include <sys/types.h> // For uid_t, gid_t
#include <time.h> // For ctime()
int main()
{
const char *path = "."; // Use current directory
int proj_id = 65; // Project identifier
key_t key;
int msgid;
struct msqid_ds info;
// Step 1: Generate key
key = ftok(path, proj_id);
if (key == -1)
{
perror("❌ ftok failed");
exit(1);
}
// Step 2: Create or access message queue
msgid = msgget(key, IPC_CREAT | 0666);
if (msgid == -1)
{
perror("❌ msgget failed");
exit(1);
}
// Step 3: Retrieve metadata
if (msgctl(msgid, IPC_STAT, &info) == -1)
{
perror("❌ msgctl failed");
exit(1);
}
// Step 4: Print attributes
printf("🔐 Access Permissions: %o\n", info.msg_perm.mode);
printf("👤 UID: %d\n", info.msg_perm.uid);
printf("👥 GID: %d\n", info.msg_perm.gid);
printf("📤 Last msg sent: %s", ctime(&info.msg_stime));
printf("📥 Last msg received: %s", ctime(&info.msg_rtime));
printf("🛠️ Last change time: %s", ctime(&info.msg_ctime));
//printf("📦 Current bytes in queue: %lu\n", info.__msg_cbytes); // macOS uses __msg_cbytes
printf("📨 Number of messages in queue: %lu\n", info.msg_qnum);
printf("📏 Max bytes allowed in queue: %lu\n", info.msg_qbytes);
printf("📨 Number of messages: %lu\n", info.msg_qnum);
printf("📏 Max bytes allowed: %lu\n", info.msg_qbytes);
printf("🧑💻 PID of last msgsnd: %d\n", info.msg_lspid);
printf("🧑💻 PID of last msgrcv: %d\n", info.msg_lrpid);
return 0;
}
/*
Output:
╰─ ./25 ─╯
🔐 Access Permissions: 666
👤 UID: 501
👥 GID: 20
📤 Last msg sent: Thu Jan 1 05:30:00 1970
📥 Last msg received: Thu Jan 1 05:30:00 1970
🛠️ Last change time: Tue Sep 30 23:51:18 2025
📨 Number of messages in queue: 0
📏 Max bytes allowed in queue: 2048
📨 Number of messages: 0
📏 Max bytes allowed: 2048
🧑💻 PID of last msgsnd: 0
🧑💻 PID of last msgrcv: 0
*/