vircommand.h 7.6 KB
Newer Older
1
/*
2
 * vircommand.h: Child command execution
3
 *
4
 * Copyright (C) 2010-2014 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
19 20 21
 *
 */

22 23
#ifndef LIBVIRT_VIRCOMMAND_H
# define LIBVIRT_VIRCOMMAND_H
24 25

# include "internal.h"
26
# include "virbuffer.h"
27
# include "virautoclean.h"
28 29 30 31

typedef struct _virCommand virCommand;
typedef virCommand *virCommandPtr;

32
/* This will execute in the context of the first child
E
Eric Blake 已提交
33 34
 * after fork() but before execve().  As such, it is unsafe to
 * call any function that is not async-signal-safe.  */
35 36
typedef int (*virExecHook)(void *data);

E
Eric Blake 已提交
37
pid_t virFork(void) ATTRIBUTE_RETURN_CHECK;
38 39 40

int virRun(const char *const*argv, int *status) ATTRIBUTE_RETURN_CHECK;

41 42 43 44 45 46 47
virCommandPtr virCommandNew(const char *binary) ATTRIBUTE_NONNULL(1);

virCommandPtr virCommandNewArgs(const char *const*args) ATTRIBUTE_NONNULL(1);

virCommandPtr virCommandNewArgList(const char *binary, ...)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_SENTINEL;

48 49 50
virCommandPtr virCommandNewVAList(const char *binary, va_list list)
    ATTRIBUTE_NONNULL(1);

51 52 53 54
/* All error report from these setup APIs is
 * delayed until the Run/RunAsync methods
 */

55
typedef enum {
56 57
    /* Close the FD in the parent */
    VIR_COMMAND_PASS_FD_CLOSE_PARENT = (1 << 0),
58
} virCommandPassFDFlags;
59

60 61
void virCommandPassFD(virCommandPtr cmd,
                      int fd,
62
                      unsigned int flags) ATTRIBUTE_NOINLINE;
63

64 65
void virCommandPassListenFDs(virCommandPtr cmd);

66 67 68
int virCommandPassFDGetFDIndex(virCommandPtr cmd,
                               int fd);

69 70 71
void virCommandSetPidFile(virCommandPtr cmd,
                          const char *pidfile) ATTRIBUTE_NONNULL(2);

72 73 74 75
gid_t virCommandGetGID(virCommandPtr cmd) ATTRIBUTE_NONNULL(1);

uid_t virCommandGetUID(virCommandPtr cmd) ATTRIBUTE_NONNULL(1);

76 77 78 79
void virCommandSetGID(virCommandPtr cmd, gid_t gid);

void virCommandSetUID(virCommandPtr cmd, uid_t uid);

80 81 82
void virCommandSetMaxMemLock(virCommandPtr cmd, unsigned long long bytes);
void virCommandSetMaxProcesses(virCommandPtr cmd, unsigned int procs);
void virCommandSetMaxFiles(virCommandPtr cmd, unsigned int files);
83
void virCommandSetMaxCoreSize(virCommandPtr cmd, unsigned long long bytes);
84
void virCommandSetUmask(virCommandPtr cmd, int umask);
85

86 87 88 89 90
void virCommandClearCaps(virCommandPtr cmd);

void virCommandAllowCap(virCommandPtr cmd,
                        int capability);

91 92 93 94 95 96
void virCommandSetSELinuxLabel(virCommandPtr cmd,
                               const char *label);

void virCommandSetAppArmorProfile(virCommandPtr cmd,
                                  const char *profile);

97 98 99 100
void virCommandDaemonize(virCommandPtr cmd);

void virCommandNonblockingFDs(virCommandPtr cmd);

101 102
void virCommandRawStatus(virCommandPtr cmd);

E
Eric Blake 已提交
103 104
void virCommandAddEnvFormat(virCommandPtr cmd, const char *format, ...)
    ATTRIBUTE_NONNULL(2) ATTRIBUTE_FMT_PRINTF(2, 3);
105

106 107 108 109 110 111
void virCommandAddEnvPair(virCommandPtr cmd,
                          const char *name,
                          const char *value) ATTRIBUTE_NONNULL(2);

void virCommandAddEnvString(virCommandPtr cmd,
                            const char *str) ATTRIBUTE_NONNULL(2);
112 113 114 115

void virCommandAddEnvBuffer(virCommandPtr cmd,
                            virBufferPtr buf);

116 117 118 119 120 121
void virCommandAddEnvPassBlockSUID(virCommandPtr cmd,
                                   const char *name,
                                   const char *defvalue) ATTRIBUTE_NONNULL(2);

void virCommandAddEnvPassAllowSUID(virCommandPtr cmd,
                                   const char *name) ATTRIBUTE_NONNULL(2);
E
Eric Blake 已提交
122

123 124
void virCommandAddEnvPassCommon(virCommandPtr cmd);

125 126
void virCommandAddEnvXDG(virCommandPtr cmd, const char *baseDir);

127 128 129
void virCommandAddArg(virCommandPtr cmd,
                      const char *val) ATTRIBUTE_NONNULL(2);

130 131 132
void virCommandAddArgBuffer(virCommandPtr cmd,
                            virBufferPtr buf);

133 134 135 136 137 138 139 140
void virCommandAddArgFormat(virCommandPtr cmd,
                            const char *format, ...)
    ATTRIBUTE_NONNULL(2) ATTRIBUTE_FMT_PRINTF(2, 3);

void virCommandAddArgPair(virCommandPtr cmd,
                          const char *name,
                          const char *val)
    ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);
E
Eric Blake 已提交
141

142 143
void virCommandAddArgSet(virCommandPtr cmd,
                         const char *const*vals) ATTRIBUTE_NONNULL(2);
E
Eric Blake 已提交
144

145 146 147 148 149 150 151 152 153
void virCommandAddArgList(virCommandPtr cmd,
                          ... /* const char *arg, ..., NULL */)
    ATTRIBUTE_SENTINEL;

void virCommandSetWorkingDirectory(virCommandPtr cmd,
                                   const char *pwd) ATTRIBUTE_NONNULL(2);

void virCommandSetInputBuffer(virCommandPtr cmd,
                              const char *inbuf) ATTRIBUTE_NONNULL(2);
E
Eric Blake 已提交
154

155 156
void virCommandSetOutputBuffer(virCommandPtr cmd,
                               char **outbuf) ATTRIBUTE_NONNULL(2);
E
Eric Blake 已提交
157

158 159 160 161 162
void virCommandSetErrorBuffer(virCommandPtr cmd,
                              char **errbuf) ATTRIBUTE_NONNULL(2);

void virCommandSetInputFD(virCommandPtr cmd,
                          int infd);
E
Eric Blake 已提交
163

164 165
void virCommandSetOutputFD(virCommandPtr cmd,
                           int *outfd) ATTRIBUTE_NONNULL(2);
E
Eric Blake 已提交
166

167 168 169 170 171 172 173 174 175 176
void virCommandSetErrorFD(virCommandPtr cmd,
                          int *errfd) ATTRIBUTE_NONNULL(2);

void virCommandSetPreExecHook(virCommandPtr cmd,
                              virExecHook hook,
                              void *opaque) ATTRIBUTE_NONNULL(2);

void virCommandWriteArgLog(virCommandPtr cmd,
                           int logfd);

177
char *virCommandToString(virCommandPtr cmd, bool linebreaks) ATTRIBUTE_RETURN_CHECK;
178

M
Marc Hartmayer 已提交
179
int virCommandExec(virCommandPtr cmd, gid_t *groups, int ngroups) ATTRIBUTE_RETURN_CHECK;
180

181 182 183 184 185 186 187 188 189
int virCommandRun(virCommandPtr cmd,
                  int *exitstatus) ATTRIBUTE_RETURN_CHECK;

int virCommandRunAsync(virCommandPtr cmd,
                       pid_t *pid) ATTRIBUTE_RETURN_CHECK;

int virCommandWait(virCommandPtr cmd,
                   int *exitstatus) ATTRIBUTE_RETURN_CHECK;

190 191 192 193 194 195 196 197
void virCommandRequireHandshake(virCommandPtr cmd);

int virCommandHandshakeWait(virCommandPtr cmd)
    ATTRIBUTE_RETURN_CHECK;

int virCommandHandshakeNotify(virCommandPtr cmd)
    ATTRIBUTE_RETURN_CHECK;

198 199
void virCommandAbort(virCommandPtr cmd);

200 201
void virCommandFree(virCommandPtr cmd);

202
void virCommandDoAsyncIO(virCommandPtr cmd);
203

204 205 206 207 208 209 210 211 212 213 214 215
typedef int (*virCommandRunRegexFunc)(char **const groups,
                                      void *data);
typedef int (*virCommandRunNulFunc)(size_t n_tokens,
                                    char **const groups,
                                    void *data);

int virCommandRunRegex(virCommandPtr cmd,
                       int nregex,
                       const char **regex,
                       int *nvars,
                       virCommandRunRegexFunc func,
                       void *data,
216 217
                       const char *cmd_to_ignore,
                       int *exitstatus);
218 219 220 221 222 223

int virCommandRunNul(virCommandPtr cmd,
                     size_t n_columns,
                     virCommandRunNulFunc func,
                     void *data);

224
VIR_DEFINE_AUTOPTR_FUNC(virCommand, virCommandFree);
225

226
#endif /* LIBVIRT_VIRCOMMAND_H */