virutil.h 5.3 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
#pragma once
24

25 26 27
#include "internal.h"
#include <unistd.h>
#include <sys/types.h>
28

29 30 31 32 33 34
#ifndef MIN
# define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
#ifndef MAX
# define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif
35

J
Ján Tomko 已提交
36

37
int virSetBlocking(int fd, bool blocking) ATTRIBUTE_RETURN_CHECK;
38
int virSetNonBlock(int fd) ATTRIBUTE_RETURN_CHECK;
E
Eric Blake 已提交
39
int virSetInherit(int fd, bool inherit) ATTRIBUTE_RETURN_CHECK;
40
int virSetCloseExec(int fd) ATTRIBUTE_RETURN_CHECK;
41
int virSetSockReuseAddr(int fd, bool fatal) ATTRIBUTE_RETURN_CHECK;
42

43 44 45
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,
46
                         bool clearExistingCaps);
L
Laine Stump 已提交
47

J
John Ferlan 已提交
48 49
void virWaitForDevices(void);

50 51 52 53
int virScaleInteger(unsigned long long *value, const char *suffix,
                    unsigned long long scale, unsigned long long limit)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;

54 55
int virHexToBin(unsigned char c);

56 57
int virParseVersionString(const char *str, unsigned long *version,
                          bool allowMissing);
58

D
Daniel P. Berrange 已提交
59 60
char *virFormatIntDecimal(char *buf, size_t buflen, int val)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
61

62 63 64 65
unsigned long long
virFormatIntPretty(unsigned long long val,
                   const char **unit);

66
int virDiskNameParse(const char *name, int *disk, int *partition);
67
int virDiskNameToIndex(const char* str);
68
char *virIndexToDiskName(int idx, const char *prefix);
69

70
/* No-op workarounds for functionality missing in mingw.  */
71
#ifndef HAVE_GETUID
72 73
static inline int getuid(void)
{ return 0; }
74
#endif
D
Daniel P. Berrange 已提交
75

76
#ifndef HAVE_GETEUID
77 78
static inline int geteuid(void)
{ return 0; }
79
#endif
80

81
#ifndef HAVE_GETGID
82 83
static inline int getgid(void)
{ return 0; }
84
#endif
D
Daniel P. Berrange 已提交
85

86
#ifndef HAVE_GETEGID
87 88
static inline int getegid(void)
{ return 0; }
89
#endif
90

91 92
#ifdef FUNC_PTHREAD_SIGMASK_BROKEN
# undef pthread_sigmask
93 94 95 96 97 98 99 100 101
static inline int pthread_sigmask(int how,
                                  const void *set,
                                  void *old)
{
    (void) how;
    (void) set;
    (void) old;
    return 0;
}
102
#endif
103

104
char *virGetHostname(void);
105
char *virGetHostnameQuiet(void);
106

107
char *virGetUserDirectory(void);
D
Dan Walsh 已提交
108
char *virGetUserDirectoryByUID(uid_t uid);
109 110 111
char *virGetUserConfigDirectory(void);
char *virGetUserCacheDirectory(void);
char *virGetUserRuntimeDirectory(void);
112
char *virGetUserShell(uid_t uid);
113 114
char *virGetUserName(uid_t uid) ATTRIBUTE_NOINLINE;
char *virGetGroupName(gid_t gid) ATTRIBUTE_NOINLINE;
E
Eric Blake 已提交
115 116
int virGetGroupList(uid_t uid, gid_t group, gid_t **groups)
    ATTRIBUTE_NONNULL(3);
117
int virGetUserID(const char *name,
118
                 uid_t *uid) ATTRIBUTE_RETURN_CHECK;
119
int virGetGroupID(const char *name,
120
                  gid_t *gid) ATTRIBUTE_RETURN_CHECK;
121

122 123
bool virDoesUserExist(const char *name);
bool virDoesGroupExist(const char *name);
124 125


126
bool virIsDevMapperDevice(const char *dev_name) ATTRIBUTE_NONNULL(1);
127

O
Osier Yang 已提交
128 129
bool virValidateWWN(const char *wwn);

130 131 132 133 134 135 136 137 138
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);
139 140
char *virGetUnprivSGIOSysfsPath(const char *path,
                                const char *sysfs_dir);
141

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

144 145 146 147
const char *virGetEnvBlockSUID(const char *name);
const char *virGetEnvAllowSUID(const char *name);
bool virIsSUID(void);

148 149 150 151

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

152
char *virGetUNIXSocketPath(int fd);
153

154 155
long virGetSystemPageSize(void) ATTRIBUTE_NOINLINE;
long virGetSystemPageSizeKB(void) ATTRIBUTE_NOINLINE;
156

157 158
unsigned long long virMemoryLimitTruncate(unsigned long long value);
bool virMemoryLimitIsSet(unsigned long long value);
159
unsigned long long virMemoryMaxValue(bool ulong) ATTRIBUTE_NOINLINE;
160

161 162
bool virHostHasIOMMU(void);

163
char *virHostGetDRMRenderNode(void) ATTRIBUTE_NOINLINE;
164

165 166 167 168 169 170 171 172
/**
 * 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.
 */
173
#define VIR_ASSIGN_IS_OVERFLOW(lvalue, rvalue) \
174
    (((lvalue) = (rvalue)) != (rvalue))