forked from rustcoreutils/posixutils-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipcs.rs
283 lines (228 loc) · 7.88 KB
/
ipcs.rs
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//
// Copyright (c) 2024 Jeff Garzik
//
// This file is part of the posixutils-rs project covered under
// the MIT License. For the full license text, please see the LICENSE
// file in the root directory of this project.
// SPDX-License-Identifier: MIT
//
use chrono::Local;
use clap::Parser;
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
/// ipcs - report XSI interprocess communication facilities status
#[derive(Parser)]
#[command(version, about)]
struct Args {
/// Write information about active message queues.
#[arg(short = 'q', long = "queues")]
message_queues: bool,
/// Write information about active shared memory segments.
#[arg(short = 'm', long = "shmems")]
shared_memory: bool,
/// Write information about active semaphore sets.
#[arg(short, long)]
semaphores: bool,
/// Use all print options.
#[arg(short, long)]
all: bool,
/// Write information on maximum allowable size.
#[arg(short = 'b', long = "max-size")]
max_size: bool,
/// Write creator's user name and group name.
#[arg(short, long)]
creator: bool,
/// Write information on outstanding usage.
#[arg(short, long)]
outstanding: bool,
/// Write process number information.
#[arg(short, long)]
pid: bool,
/// Write time information.
#[arg(short, long)]
time: bool,
}
fn enable_all_options(args: &mut Args) {
args.max_size = true;
args.creator = true;
args.outstanding = true;
args.pid = true;
args.time = true;
}
fn enable_all_facilities(args: &mut Args) {
args.message_queues = true;
args.shared_memory = true;
args.semaphores = true;
}
fn display_message_queues(_args: &Args) {
#[cfg(not(target_os = "macos"))]
use std::ffi::CStr;
println!("{}", gettext("Message Queues:"));
println!("T ID KEY MODE OWNER GROUP");
#[cfg(not(target_os = "macos"))]
{
use libc::{msgctl, msqid_ds, IPC_STAT};
let mut msqid: i32 = 0;
let mut msg_ds: msqid_ds = unsafe { std::mem::zeroed() };
loop {
if unsafe { msgctl(msqid, IPC_STAT, &mut msg_ds) } == -1 {
break;
}
let key = msg_ds.msg_perm.__key; // Ensure the correct field name for your system
let mode = msg_ds.msg_perm.mode;
let uid = msg_ds.msg_perm.uid;
let gid = msg_ds.msg_perm.gid;
let owner = unsafe {
CStr::from_ptr(libc::getpwuid(uid).as_ref().unwrap().pw_name)
.to_str()
.unwrap()
};
let group = unsafe {
CStr::from_ptr(libc::getgrgid(gid).as_ref().unwrap().gr_name)
.to_str()
.unwrap()
};
let mode_str = format!(
"{}{}{}",
if mode & 0o400 != 0 { "r" } else { "-" },
if mode & 0o200 != 0 { "w" } else { "-" },
if mode & 0o100 != 0 { "a" } else { "-" }
);
println!(
"q {:<5} 0x{:08x} {:<10} {:<8} {:<8}",
msqid, key, mode_str, owner, group
);
msqid += 1;
}
}
#[cfg(target_os = "macos")]
{
println!("{}", gettext("Message Queue facility not in system."));
}
}
fn display_shared_memory(_args: &Args) {
use libc::{shmctl, shmid_ds, IPC_STAT};
use std::ffi::CStr;
#[cfg(target_os = "macos")]
const SHM_INFO: libc::c_int = 14; // SHM_INFO is typically 14 in Linux but this is not standard
#[cfg(not(target_os = "macos"))]
const SHM_INFO: libc::c_int = 14;
let mut shmbuf: shmid_ds = unsafe { std::mem::zeroed() };
let maxid = unsafe { shmctl(0, SHM_INFO, &mut shmbuf) };
if maxid < 0 {
println!("{}", gettext("Shared Memory facility not in system."));
return;
}
println!("{}", gettext("Shared Memory:"));
println!("T ID KEY MODE OWNER GROUP");
for shmid in 0..=maxid {
if unsafe { shmctl(shmid, IPC_STAT, &mut shmbuf) } == -1 {
continue;
}
#[cfg(target_os = "macos")]
let key = shmbuf.shm_perm._key; // Check for the correct field name on your system
#[cfg(not(target_os = "macos"))]
let key = shmbuf.shm_perm.__key; // Check for the correct field name on your system
let mode = shmbuf.shm_perm.mode;
let uid = shmbuf.shm_perm.uid;
let gid = shmbuf.shm_perm.gid;
let owner = unsafe {
CStr::from_ptr(libc::getpwuid(uid).as_ref().unwrap().pw_name)
.to_str()
.unwrap()
};
let group = unsafe {
CStr::from_ptr(libc::getgrgid(gid).as_ref().unwrap().gr_name)
.to_str()
.unwrap()
};
let mode_str = format!(
"{}{}{}",
if mode & 0o400 != 0 { "r" } else { "-" },
if mode & 0o200 != 0 { "w" } else { "-" },
if mode & 0o100 != 0 { "a" } else { "-" }
);
println!(
"m {:<5} 0x{:08x} {:<10} {:<8} {:<8}",
shmid, key, mode_str, owner, group
);
}
}
fn display_semaphores(_args: &Args) {
use libc::{semctl, semid_ds, IPC_STAT};
use std::ffi::CStr;
let mut semid: i32 = 0;
let mut sem_ds: semid_ds = unsafe { std::mem::zeroed() };
println!("{}", gettext("Semaphores:"));
println!("T ID KEY MODE OWNER GROUP NSEMS");
loop {
if unsafe { semctl(semid, 0, IPC_STAT, &mut sem_ds) } == -1 {
break;
}
#[cfg(not(target_os = "macos"))]
let key = sem_ds.sem_perm.__key; // Check for the correct field name on your system
#[cfg(target_os = "macos")]
let key = sem_ds.sem_perm._key; // Check for the correct field name on your system
let mode = sem_ds.sem_perm.mode;
let uid = sem_ds.sem_perm.uid;
let gid = sem_ds.sem_perm.gid;
let owner = unsafe {
CStr::from_ptr(libc::getpwuid(uid).as_ref().unwrap().pw_name)
.to_str()
.unwrap()
};
let group = unsafe {
CStr::from_ptr(libc::getgrgid(gid).as_ref().unwrap().gr_name)
.to_str()
.unwrap()
};
let mode_str = format!(
"{}{}{}",
if mode & 0o400 != 0 { "r" } else { "-" },
if mode & 0o200 != 0 { "w" } else { "-" },
if mode & 0o100 != 0 { "a" } else { "-" }
);
println!(
"s {:<5} 0x{:08x} {:<10} {:<8} {:<8} {:<5}",
semid, key, mode_str, owner, group, sem_ds.sem_nsems
);
semid += 1;
}
}
fn get_current_date() -> String {
// Retrieve the current date and time in a human-readable format
let now = Local::now();
now.format("%Y-%m-%d %H:%M:%S").to_string()
}
fn display_ipc_status(args: &Args) {
println!(
"{}",
gettext!("IPC status from {} as of {}", "source", get_current_date())
);
if args.message_queues {
display_message_queues(args);
}
if args.shared_memory {
display_shared_memory(args);
}
if args.semaphores {
display_semaphores(args);
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
setlocale(LocaleCategory::LcAll, "");
textdomain(env!("PROJECT_NAME"))?;
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
let mut args = Args::parse();
// Validate arguments and determine what to display
if args.all {
// Enable all options
enable_all_options(&mut args);
}
// Default to showing all facilities if none of -q, -m, or -s are specified
if !(args.message_queues || args.shared_memory || args.semaphores) {
enable_all_facilities(&mut args);
}
// Display IPC status
display_ipc_status(&args);
Ok(())
}