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
use std::os::raw::{c_char, c_int, c_long, c_uint, c_void};
use std::ptr::null_mut;

/// LXC environment policy
///
/// ---
/// **version:** 1.0.0
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub enum lxc_attach_env_policy_t {
    /// Retain the environment
    ///
    /// ---
    /// **version:** 1.0.0
    LXC_ATTACH_KEEP_ENV,
    /// Clear the environment
    ///
    /// ---
    /// **version:** 1.0.0
    LXC_ATTACH_CLEAR_ENV,
}

// Options on by default
/// Move to cgroup
///
/// ---
/// **version:** 1.0.0
pub const LXC_ATTACH_MOVE_TO_CGROUP: u32 = 0x00000001;
/// Drop capabilities
///
/// ---
/// **version:** 1.0.0
pub const LXC_ATTACH_DROP_CAPABILITIES: u32 = 0x00000002;
/// Set personality
///
/// ---
/// **version:** 1.0.0
pub const LXC_ATTACH_SET_PERSONALITY: u32 = 0x00000004;
/// Execute under a Linux Security Module
///
/// ---
/// **version:** 1.0.0
pub const LXC_ATTACH_LSM_EXEC: u32 = 0x00000008;

// Options off by default
/// Remount /proc filesystem
///
/// ---
/// **version:** 1.0.0
pub const LXC_ATTACH_REMOUNT_PROC_SYS: u32 = 0x00010000;
/// FIXME: unknown
///
/// ---
/// **version:** 1.0.0
pub const LXC_ATTACH_LSM_NOW: u32 = 0x00020000;

/// Mask of flags to apply by default
///
/// ---
/// **version:** 1.0.0
pub const LXC_ATTACH_DEFAULT: u32 = 0x0000FFFF;

/// All Linux Security Module flags
///
/// ---
/// **version:** 1.0.0
pub const LXC_ATTACH_LSM: u32 = LXC_ATTACH_LSM_EXEC | LXC_ATTACH_LSM_NOW;

/// LXC attach function type.
///
/// Function to run in container
///
/// ---
/// **Parameters**
///
/// **payload** [lxc_attach_command_t] to run.
///
/// ---
/// **Returns**
///
/// Function should return `0` on success, and any other value to denote
/// failure.
///
/// ---
/// **version:** 1.0.0
pub type lxc_attach_exec_t = extern "C" fn(payload: *mut c_void) -> c_int;

#[repr(C)]
#[derive(Debug, Copy, Clone)]
/// LXC attach options for \ref lxc_container `attach()`.
///
/// ---
/// **version:** 1.0.0
pub struct lxc_attach_options_t {
    /// Any combination of LXC_ATTACH_* flags
    ///
    /// ---
    /// **version:** 1.0.0
    pub attach_flags: c_uint,
    /// The namespaces to attach to (CLONE_NEW... flags)
    ///
    /// ---
    /// **version:** 1.0.0
    pub namespaces: c_int,
    /// Initial personality (`-1` to autodetect).
    ///
    /// ---
    /// **warning:** This may be ignored if lxc is compiled without personality
    /// support)
    ///
    /// ---
    /// **version:** 1.0.0
    pub personality: c_long,
    /// Inital current directory, use `NULL` to use cwd.
    /// If the current directory does not exist in the container, the
    /// root directory will be used instead because of kernel defaults.
    ///
    /// ---
    /// **version:** 1.0.0
    pub initial_cwd: *mut c_char,
    /// The user-id to run as.
    ///
    /// ---
    /// **note:** Set to `-1` for default behaviour (init uid for userns
    /// containers or `0` (super-user) if detection fails).
    ///
    /// ---
    /// **version:** 1.0.0
    pub uid: c_uint,
    ///  The group-id to run as.
    ///
    /// ---
    /// **note:** Set to `-1` for default behaviour (init gid for userns
    /// containers or `0` (super-user) if detection fails).
    ///
    /// ---
    /// **version:** 1.0.0
    pub gid: c_uint,
    /// Environment policy
    ///
    /// ---
    /// **version:** 1.0.0
    pub env_policy: lxc_attach_env_policy_t,
    /// Extra environment variables to set in the container environment
    ///
    /// ---
    /// **version:** 1.0.0
    pub extra_env_vars: *mut *mut c_char,
    /// Names of environment variables in existing environment to retain
    /// in container environment.
    ///
    /// ---
    /// **version:** 1.0.0
    pub extra_keep_env: *mut *mut c_char,

    /// stdin file descriptor
    ///
    /// ---
    /// **version:** 1.0.0
    pub stdin_fd: c_int,
    /// stdout file descriptor
    ///
    /// ---
    /// **version:** 1.0.0
    pub stdout_fd: c_int,
    /// stderr file descriptor
    ///
    /// ---
    /// **version:** 1.0.0
    pub stderr_fd: c_int,
}

/// Default attach options to use
///
/// ---
/// **version:** 1.0.0
impl std::default::Default for lxc_attach_options_t {
    fn default() -> Self {
        lxc_attach_options_t {
            attach_flags: LXC_ATTACH_DEFAULT,
            namespaces: -1,
            personality: -1,
            initial_cwd: null_mut(),
            uid: c_uint::MAX,
            gid: c_uint::MAX,
            env_policy: lxc_attach_env_policy_t::LXC_ATTACH_KEEP_ENV,
            extra_env_vars: null_mut(),
            extra_keep_env: null_mut(),
            stdin_fd: 0,
            stdout_fd: 1,
            stderr_fd: 2,
        }
    }
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
/// Representation of a command to run in a container.
///
/// ---
/// **version:** 1.0.0
pub struct lxc_attach_command_t {
    /// The program to run (passed to execvp)
    ///
    /// ---
    /// **version:** 1.0.0
    pub program: *mut c_char,
    /// The argv pointer of that program, including the program itself in
    /// argv\[0\]
    ///
    /// ---
    /// **version:** 1.0.0
    pub argv: *mut *mut c_char,
}

extern "C" {
    /// Run a command in the container.
    ///
    /// ---
    /// **Parameters**
    ///
    /// **payload** [lxc_attach_command_t] to run.
    ///
    /// ---
    /// **Returns**
    ///
    /// `-1` on error, exit code of lxc_attach_command_t program on success.
    ///
    /// ---
    /// **version:** 1.0.0
    pub fn lxc_attach_run_command(payload: *mut c_void) -> c_int;

    /// Run a shell command in the container.
    ///
    /// ---
    /// **Parameters**
    ///
    /// **payload** Not used.
    ///
    /// ---
    /// **Returns**
    ///
    /// Exit code of shell.
    ///
    /// ---
    /// **version:** 1.0.0
    pub fn lxc_attach_run_shell(payload: *mut c_void) -> c_int;
}