sem/
ffi.rs

1// @adjivas - github.com/adjivas. See the LICENSE
2// file at the top-level directory of this distribution and at
3// https://github.com/adjivas/sem
4//
5// This file may not be copied, modified, or distributed
6// except according to those terms.
7
8#![allow(dead_code)]
9
10/// The `Ipc` enum is a POSIX Standard
11/// for System V.
12
13#[repr(C)]
14#[derive(Copy, Clone)]
15pub enum Ipc {
16    CREAT  = 0o0001000,
17    NOWAIT = 2048,
18    EXCL   = 0o0002000,
19    RMID   = 0o0000000,
20    SET    = 0o0000001,
21    STAT   = 0o0000002,
22    INFO   = 0o0000003,
23}
24
25#[repr(C)]
26#[derive(Copy, Clone)]
27#[cfg(target_os = "linux")]
28pub enum Sem {
29  GETPID  = 11,
30  GETVAL  = 12,
31  GETALL  = 13,
32  GETZCNT = 15,
33  SETVAL  = 16,
34  SETALL  = 17,
35}
36
37#[repr(C)]
38#[derive(Copy, Clone)]
39#[cfg(target_os = "macos")]
40pub enum Sem {
41  GETPID  = 4,
42  GETVAL  = 5,
43  GETALL  = 6,
44  GETZCNT = 7,
45  SETVAL  = 8,
46  SETALL  = 9,
47}
48
49/// The `TOK_*` const are default values
50/// for macros.
51
52pub const TOK_PATHNAME: &'static [u8; 4] = b"/tmp";
53pub const TOK_PROJ_ID: u32 = 0;
54pub const SEM_NUM: i32 = 0;
55pub const NSEMS: i32 = 1;
56pub const NSOPS: u64 = 1;
57
58/// The `C` extern is list of libc functions required
59/// by the project.
60
61#[cfg(any(unix))]
62extern "C" {
63    pub fn ftok(path: *mut i8, id: i32) -> i64;
64    pub fn semget(key: u32, nsems: i32, semflg: i32) -> i32;
65    pub fn semop(semid: i32, sops: *mut SemBuf, nsops: u64) -> i32;
66    pub fn semctl(semid: i32, semnum: i32, cmd: i32, arg: i32) -> i32;
67}
68
69#[repr(C)]
70pub struct SemBuf {
71    pub sem_num: u16, /* semaphore. # */
72    pub sem_op: i16, /* semaphore operation. */
73    pub sem_flg: i16, /* operation flags. */
74}
75
76#[repr(C)]
77pub struct SemUn {
78    pub val: i32, /* value for SETVAL. */
79    pub buf: SemIdDs, /* buffer for IPC_STAT & IPC_SET. */
80    pub array: i16, /* array for GETALL & SETALL. */
81}
82
83#[repr(C)]
84pub struct SemIdDs {
85    pub sem_perm: IpcPerm, /* Ownership and permissions. */
86    pub sem_otime: i64, /* Last semop time. */
87    pub sem_ctime: i64, /* Last change time. */
88    pub sem_nsems: u16, /* No. of semaphores in set. */
89}
90
91#[repr(C)]
92pub struct IpcPerm {
93    pub key: u32,
94    pub uid: u16, /* owner euid and egid */
95    pub gid: u16,
96    pub cuid: u16, /* creator euid and egid */
97    pub cgid: u16,
98    pub mode: u16, /* access modes see mode flags below */
99    pub seq: u16, /* slot usage sequence number */
100}