virutil.h 8.0 KB
Newer Older
1
/*
2
 * virutil.h: common, generic utility functions
3
 *
4
 * Copyright (C) 2010-2014 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
# include "internal.h"
# include <unistd.h>
# include <sys/types.h>
31

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

J
Ján Tomko 已提交
39

40
int virSetBlocking(int fd, bool blocking) ATTRIBUTE_RETURN_CHECK;
41
int virSetNonBlock(int fd) ATTRIBUTE_RETURN_CHECK;
E
Eric Blake 已提交
42
int virSetInherit(int fd, bool inherit) ATTRIBUTE_RETURN_CHECK;
43
int virSetCloseExec(int fd) ATTRIBUTE_RETURN_CHECK;
44
int virSetSockReuseAddr(int fd, bool fatal) 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 virDiskNameParse(const char *name, int *disk, int *partition);
71
int virDiskNameToIndex(const char* str);
72
char *virIndexToDiskName(int idx, const char *prefix);
73 74 75 76 77 78 79 80 81

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

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

82
# define VIR_ENUM_IMPL(name, lastVal, ...)                               \
83
    static const char *const name ## TypeList[] = { __VA_ARGS__ };      \
E
Eric Blake 已提交
84
    verify(ARRAY_CARDINALITY(name ## TypeList) == lastVal);             \
85 86 87 88 89 90 91 92 93 94 95
    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);                                 \
    }

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

100
/* No-op workarounds for functionality missing in mingw.  */
101
# ifndef HAVE_GETUID
102 103
static inline int getuid(void)
{ return 0; }
104
# endif
D
Daniel P. Berrange 已提交
105

106
# ifndef HAVE_GETEUID
107 108
static inline int geteuid(void)
{ return 0; }
109 110
# endif

111
# ifndef HAVE_GETGID
112 113
static inline int getgid(void)
{ return 0; }
114
# endif
D
Daniel P. Berrange 已提交
115

116
# ifndef HAVE_GETEGID
117 118
static inline int getegid(void)
{ return 0; }
119 120 121 122 123 124 125 126 127 128 129 130 131
# endif

# ifdef FUNC_PTHREAD_SIGMASK_BROKEN
#  undef pthread_sigmask
static inline int pthread_sigmask(int how,
                                  const void *set,
                                  void *old)
{
    (void) how;
    (void) set;
    (void) old;
    return 0;
}
132 133
# endif

134
char *virGetHostname(void);
135
char *virGetHostnameQuiet(void);
136

137
char *virGetUserDirectory(void);
D
Dan Walsh 已提交
138
char *virGetUserDirectoryByUID(uid_t uid);
139 140 141
char *virGetUserConfigDirectory(void);
char *virGetUserCacheDirectory(void);
char *virGetUserRuntimeDirectory(void);
142
char *virGetUserShell(uid_t uid);
143
char *virGetUserName(uid_t uid);
144
char *virGetGroupName(gid_t gid);
E
Eric Blake 已提交
145 146
int virGetGroupList(uid_t uid, gid_t group, gid_t **groups)
    ATTRIBUTE_NONNULL(3);
147
int virGetUserID(const char *name,
148
                 uid_t *uid) ATTRIBUTE_RETURN_CHECK;
149
int virGetGroupID(const char *name,
150
                  gid_t *gid) ATTRIBUTE_RETURN_CHECK;
151

152
bool virIsDevMapperDevice(const char *dev_name) ATTRIBUTE_NONNULL(1);
153

O
Osier Yang 已提交
154 155
bool virValidateWWN(const char *wwn);

156 157 158 159 160 161 162 163 164
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);
165 166
char *virGetUnprivSGIOSysfsPath(const char *path,
                                const char *sysfs_dir);
167 168 169 170
int virReadSCSIUniqueId(const char *sysfs_prefix,
                        int host,
                        int *result)
    ATTRIBUTE_NONNULL(3);
171 172 173 174
char *
virFindSCSIHostByPCI(const char *sysfs_prefix,
                     const char *parentaddr,
                     unsigned int unique_id);
175 176 177 178
int
virGetSCSIHostNumber(const char *adapter_name,
                     unsigned int *result)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
179 180 181 182 183 184
char *
virGetSCSIHostNameByParentaddr(unsigned int domain,
                               unsigned int bus,
                               unsigned int slot,
                               unsigned int function,
                               unsigned int unique_id);
185

186

187

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

190 191 192 193
const char *virGetEnvBlockSUID(const char *name);
const char *virGetEnvAllowSUID(const char *name);
bool virIsSUID(void);

194 195 196 197

time_t virGetSelfLastChanged(void);
void virUpdateSelfLastChanged(const char *path);

J
Ján Tomko 已提交
198 199 200 201 202 203 204 205
typedef enum {
    VIR_TRISTATE_BOOL_ABSENT = 0,
    VIR_TRISTATE_BOOL_YES,
    VIR_TRISTATE_BOOL_NO,

    VIR_TRISTATE_BOOL_LAST
} virTristateBool;

J
Ján Tomko 已提交
206 207 208 209 210 211 212 213
typedef enum {
    VIR_TRISTATE_SWITCH_ABSENT = 0,
    VIR_TRISTATE_SWITCH_ON,
    VIR_TRISTATE_SWITCH_OFF,

    VIR_TRISTATE_SWITCH_LAST
} virTristateSwitch;

J
Ján Tomko 已提交
214
VIR_ENUM_DECL(virTristateBool)
J
Ján Tomko 已提交
215
VIR_ENUM_DECL(virTristateSwitch)
J
Ján Tomko 已提交
216

217 218 219 220 221 222
/* the two enums must be in sync to be able to use helpers interchangeably in
 * some special cases */
verify((int)VIR_TRISTATE_BOOL_YES == (int)VIR_TRISTATE_SWITCH_ON);
verify((int)VIR_TRISTATE_BOOL_NO == (int)VIR_TRISTATE_SWITCH_OFF);
verify((int)VIR_TRISTATE_BOOL_ABSENT == (int)VIR_TRISTATE_SWITCH_ABSENT);

223 224
unsigned int virGetListenFDs(void);

225 226 227
long virGetSystemPageSize(void);
long virGetSystemPageSizeKB(void);

228 229
unsigned long long virMemoryLimitTruncate(unsigned long long value);
bool virMemoryLimitIsSet(unsigned long long value);
230
unsigned long long virMemoryMaxValue(bool ulong);
231

232 233 234 235 236 237 238 239 240 241 242
/**
 * VIR_ASSIGN_IS_OVERFLOW:
 * @rvalue: value that is checked (evaluated twice)
 * @lvalue: value that the check is against (used in typeof())
 *
 * This macro assigns @lvalue to @rvalue and evaluates as true if the value of
 * @rvalue did not fit into the @lvalue.
 */
# define VIR_ASSIGN_IS_OVERFLOW(lvalue, rvalue)                                \
    (((lvalue) = (rvalue)) != (rvalue))

243
#endif /* __VIR_UTIL_H__ */