-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path28.c
More file actions
68 lines (58 loc) · 1.88 KB
/
28.c
File metadata and controls
68 lines (58 loc) · 1.88 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
/*
============================================================================
Name : 28
Author : Piyush Singh
Description : Write a program to change the exiting message queue permission. (use msqid_ds structure)
Date: 26th Sep, 2025.
============================================================================
*/
// Program: Change permissions of a System V message queue
// On macOS, run with sudo and ensure msgq.key exists from earlier questions.
#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
int main()
{
key_t key;
int msgid;
struct msqid_ds info;
// Step 1: Generate key from a real file
key = ftok("msgKeyFile", 65); // Ensure this file exists
if (key == -1)
{
perror("❌ ftok failed");
exit(1);
}
// Step 2: Access existing message queue
msgid = msgget(key, 0666);
if (msgid == -1)
{
perror("❌ msgget failed");
exit(1);
}
// Step 3: Get current attributes
if (msgctl(msgid, IPC_STAT, &info) == -1)
{
perror("❌ msgctl IPC_STAT failed");
exit(1);
}
// Step 4: Modify access permissions (e.g., 0644)
info.msg_perm.mode = 0644;
// Step 5: Apply changes
if (msgctl(msgid, IPC_SET, &info) == -1)
{
perror("❌ msgctl IPC_SET failed");
exit(1);
}
printf("✅ Message queue permissions updated to: %o\n", info.msg_perm.mode);
return 0;
}
/*
Output:
╰─ ./26 ─╯
✅ Message sent to queue (ID: 65540): 📨 Hello from macOS sender!
╰─ ./28 ─╯
✅ Message queue permissions updated to: 644
*/