Function remove_queue_c

Source
pub fn remove_queue_c(name: &CStr) -> Result<(), Error>
Expand description

Delete a posix message queue, without inspecting name or allocating.

This function is on NetBSD necessary to remove queues with names that doesn’t start with a ‘/’.

§Errors

  • Queue doesn’t exist (ENOENT) => ErrorKind::NotFound
  • Not permitted to delete the queue (EACCES) => ErrorKind::PermissionDenied
  • Posix message queues are disabled (ENOSYS) => ErrorKind::Other
  • More than one ‘/’ in name (EACCESS) => ErrorKind::PermissionDenied
  • Name is empty (EINVAL) => ErrorKind::InvalidInput
  • Name is invalid (ENOENT, EACCESS or EINVAL) => ErrorKind::NotFound ErrorKind::PermissionDenied or ErrorKind::InvalidInput
  • Name is too long (ENAMETOOLONG) => ErrorKind::Other
  • Possibly other
Examples found in repository?
examples/sort.rs (line 54)
10fn main() {
11    let args = args_os()
12        .skip(1)
13        .map(|osstring| osstring.into_vec() )
14        .collect::<Vec<Vec<u8>>>();
15    if args.is_empty() {
16        return; // nothing to do
17    }
18
19    let mut recv_buf = vec![0; args.iter().map(Vec::len).max().unwrap()];
20
21    let name = CStr::from_bytes_with_nul(b"/sort\0").unwrap();
22
23    // create queue with the necessary permissions and open it
24    let mq = posixmq::OpenOptions::readwrite()
25        .nonblocking() // use WouldBlock to detect that the queue is empty
26        .create_new()
27        .mode(0o000) // only affects future attempts at opening it
28        .capacity(args.len())
29        .max_msg_len(recv_buf.len())
30        .open_c(name)
31        .expect("opening queue failed");
32
33    // write arguments to the queue
34    for arg in args {
35        mq.send(arg.len() as u32, &arg).expect("sending failed");
36    }
37
38    // read until the queue is empty
39    let stdout = stdout();
40    let mut stdout = stdout.lock();
41    loop {
42        match mq.recv(&mut recv_buf) {
43            Err(ref e) if e.kind() == ErrorKind::WouldBlock => break,
44            Err(e) => panic!("receiving failed: {}", e),
45            Ok((_priority, len)) => {
46                stdout.write_all(&recv_buf[..len])
47                    .and_then(|()| stdout.write_all(b"\n") )
48                    .expect("writing to stdout failed");
49            }
50        }
51    }
52
53    // clean up
54    posixmq::remove_queue_c(name).expect("deleting queue failed")
55}
More examples
Hide additional examples
examples/limits.rs (line 88)
28fn main() {
29    println!("{}:", std::env::consts::OS);
30
31    let mq = posixmq::OpenOptions::readwrite()
32        .create_new()
33        .open("/default_capacities")
34        .expect("Cannot create new posix message queue /default_capacities");
35    posixmq::remove_queue("/default_capacities")
36        .expect("Cannot delete posix message queue /default_capacities");
37
38    let attrs = mq.attributes().expect("Cannot get attributes for queue");
39    println!("default queue capacity:         {}", attrs.capacity);
40    println!("default maximum message length: {}", attrs.max_msg_len);
41
42    println!("max message priority:           {}", btry(0xff_ff_ff_ff, |priority| {
43        mq.send(priority as u32, b"b")
44            .map(|_| mq.recv(&mut vec![0; attrs.max_msg_len]).unwrap() )
45            .is_ok()
46    }));
47    println!("allows empty messages:          {}", mq.send(0, b"").is_ok());
48    drop(mq);
49
50    println!("max queue capacity:             {}", btry(1_000_000, |capacity| {
51        posixmq::OpenOptions::readwrite()
52            .capacity(capacity)
53            .max_msg_len(1)
54            .create_new()
55            .open("/max_capacity")
56            .map(|_| posixmq::remove_queue("/max_capacity").unwrap() )
57            .is_ok()
58    }));
59
60    println!("max message length:             {}", btry(1_000_000, |length| {
61        posixmq::OpenOptions::readwrite()
62            .max_msg_len(length)
63            .capacity(1)
64            .create_new()
65            .open("/max_length")
66            .map(|_| posixmq::remove_queue("/max_length").unwrap() )
67            .is_ok()
68    }));
69
70    println!("max equal:                      {}", btry(1_000_000, |equal| {
71        posixmq::OpenOptions::readwrite()
72            .max_msg_len(equal)
73            .capacity(equal)
74            .create_new()
75            .open("/max_equal")
76            .map(|_| posixmq::remove_queue("/max_equal").unwrap() )
77            .is_ok()
78    }));
79
80    println!("allows just \"/\":                {}", {
81        let result = posixmq::PosixMq::create("/");
82        let _ = posixmq::remove_queue("/");
83        result.is_ok()
84    });
85    println!("enforces name rules:            {}", {
86        let noslash = std::ffi::CStr::from_bytes_with_nul(b"noslash\0").unwrap();
87        let result = posixmq::OpenOptions::readwrite().create().open_c(noslash);
88        let _ = posixmq::remove_queue_c(noslash);
89        result.is_err()
90    });
91}