virutil.h 6.3 KB
Newer Older
1
/*
2
 * virutil.h: common, generic utility functions
3
 *
4
 * Copyright (C) 2010-2013 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * Copyright (C) 2006, 2007 Binary Karma
 * Copyright (C) 2006 Shuveb Hussain
 *
 * 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
19
 * License along with this library.  If not, see
O
Osier Yang 已提交
20
 * <http://www.gnu.org/licenses/>.
21 22 23 24
 *
 * File created Jul 18, 2007 - Shuveb Hussain <shuveb@binarykarma.com>
 */

25
#ifndef __VIR_UTIL_H__
26
# define __VIR_UTIL_H__
27

28 29 30 31 32
# include "verify.h"
# include "internal.h"
# include <unistd.h>
# include <sys/select.h>
# include <sys/types.h>
33

34 35 36
# ifndef MIN
#  define MIN(a, b) ((a) < (b) ? (a) : (b))
# endif
L
Laine Stump 已提交
37 38 39
# ifndef MAX
#  define MAX(a, b) ((a) > (b) ? (a) : (b))
# endif
40

41
int virSetBlocking(int fd, bool blocking) ATTRIBUTE_RETURN_CHECK;
42
int virSetNonBlock(int fd) ATTRIBUTE_RETURN_CHECK;
E
Eric Blake 已提交
43
int virSetInherit(int fd, bool inherit) ATTRIBUTE_RETURN_CHECK;
44
int virSetCloseExec(int fd) ATTRIBUTE_RETURN_CHECK;
45

46 47
int virPipeReadUntilEOF(int outfd, int errfd,
                        char **outbuf, char **errbuf);
48

49 50 51
int virSetUIDGID(uid_t uid, gid_t gid, gid_t *groups, int ngroups);
int virSetUIDGIDWithCaps(uid_t uid, gid_t gid, gid_t *groups, int ngroups,
                         unsigned long long capBits,
52
                         bool clearExistingCaps);
L
Laine Stump 已提交
53

54 55 56 57
int virScaleInteger(unsigned long long *value, const char *suffix,
                    unsigned long long scale, unsigned long long limit)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;

58 59
int virHexToBin(unsigned char c);

60
int virParseNumber(const char **str);
61 62
int virParseVersionString(const char *str, unsigned long *version,
                          bool allowMissing);
63

64
int virDoubleToStr(char **strp, double number)
D
Daniel P. Berrange 已提交
65 66 67 68
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;

char *virFormatIntDecimal(char *buf, size_t buflen, int val)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
69

70
int virDiskNameToIndex(const char* str);
71
char *virIndexToDiskName(int idx, const char *prefix);
72 73 74 75 76 77 78 79 80

int virEnumFromString(const char *const*types,
                      unsigned int ntypes,
                      const char *type);

const char *virEnumToString(const char *const*types,
                            unsigned int ntypes,
                            int type);

81
# define VIR_ENUM_IMPL(name, lastVal, ...)                               \
82
    static const char *const name ## TypeList[] = { __VA_ARGS__ };      \
E
Eric Blake 已提交
83
    verify(ARRAY_CARDINALITY(name ## TypeList) == lastVal);             \
84 85 86 87 88 89 90 91 92 93 94
    const char *name ## TypeToString(int type) {                        \
        return virEnumToString(name ## TypeList,                        \
                               ARRAY_CARDINALITY(name ## TypeList),     \
                               type);                                   \
    }                                                                   \
    int name ## TypeFromString(const char *type) {                      \
        return virEnumFromString(name ## TypeList,                      \
                                 ARRAY_CARDINALITY(name ## TypeList),   \
                                 type);                                 \
    }

95
# define VIR_ENUM_DECL(name)                             \
96 97 98
    const char *name ## TypeToString(int type);         \
    int name ## TypeFromString(const char*type);

99
# ifndef HAVE_GETUID
D
Daniel P. Berrange 已提交
100
static inline int getuid (void) { return 0; }
101
# endif
D
Daniel P. Berrange 已提交
102

103 104 105 106
# ifndef HAVE_GETEUID
static inline int geteuid (void) { return 0; }
# endif

107
# ifndef HAVE_GETGID
D
Daniel P. Berrange 已提交
108
static inline int getgid (void) { return 0; }
109
# endif
D
Daniel P. Berrange 已提交
110

111 112 113 114
# ifndef HAVE_GETEGID
static inline int getegid (void) { return 0; }
# endif

115
char *virGetHostname(void);
116

117
char *virGetUserDirectory(void);
D
Dan Walsh 已提交
118
char *virGetUserDirectoryByUID(uid_t uid);
119 120 121
char *virGetUserConfigDirectory(void);
char *virGetUserCacheDirectory(void);
char *virGetUserRuntimeDirectory(void);
122
char *virGetUserName(uid_t uid);
123
char *virGetGroupName(gid_t gid);
E
Eric Blake 已提交
124 125
int virGetGroupList(uid_t uid, gid_t group, gid_t **groups)
    ATTRIBUTE_NONNULL(3);
126
int virGetUserID(const char *name,
127
                 uid_t *uid) ATTRIBUTE_RETURN_CHECK;
128
int virGetGroupID(const char *name,
129
                  gid_t *gid) ATTRIBUTE_RETURN_CHECK;
130

131
bool virIsDevMapperDevice(const char *dev_name) ATTRIBUTE_NONNULL(1);
132

O
Osier Yang 已提交
133 134
bool virValidateWWN(const char *wwn);

135
bool virStrIsPrint(const char *str);
136 137 138 139 140 141 142 143 144 145

int virGetDeviceID(const char *path,
                   int *maj,
                   int *min);
int virSetDeviceUnprivSGIO(const char *path,
                           const char *sysfs_dir,
                           int unpriv_sgio);
int virGetDeviceUnprivSGIO(const char *path,
                           const char *sysfs_dir,
                           int *unpriv_sgio);
146 147
char *virGetUnprivSGIOSysfsPath(const char *path,
                                const char *sysfs_dir);
148 149 150 151 152
int virReadFCHost(const char *sysfs_prefix,
                  int host,
                  const char *entry,
                  char **result)
    ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4);
153

154 155
bool virIsCapableFCHost(const char *sysfs_prefix, int host);
bool virIsCapableVport(const char *sysfs_prefix, int host);
O
Osier Yang 已提交
156

157 158 159 160 161 162 163 164
enum {
    VPORT_CREATE,
    VPORT_DELETE,
};

int virManageVport(const int parent_host,
                   const char *wwpn,
                   const char *wwnn,
165
                   int operation)
166 167
    ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);

168 169 170 171 172
char *virGetFCHostNameByWWN(const char *sysfs_prefix,
                            const char *wwnn,
                            const char *wwpn)
    ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);

173 174
char *virFindFCHostCapableVport(const char *sysfs_prefix);

175
int virCompareLimitUlong(unsigned long long a, unsigned long long b);
176

M
Martin Kletzander 已提交
177 178
int virParseOwnershipIds(const char *label, uid_t *uidPtr, gid_t *gidPtr);

179 180 181 182
const char *virGetEnvBlockSUID(const char *name);
const char *virGetEnvAllowSUID(const char *name);
bool virIsSUID(void);

183
#endif /* __VIR_UTIL_H__ */