virutil.h 5.7 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

J
Ján Tomko 已提交
29

30 31 32 33 34
int virSetBlocking(int fd, bool blocking) G_GNUC_WARN_UNUSED_RESULT;
int virSetNonBlock(int fd) G_GNUC_WARN_UNUSED_RESULT;
int virSetInherit(int fd, bool inherit) G_GNUC_WARN_UNUSED_RESULT;
int virSetCloseExec(int fd) G_GNUC_WARN_UNUSED_RESULT;
int virSetSockReuseAddr(int fd, bool fatal) G_GNUC_WARN_UNUSED_RESULT;
35

36 37 38
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,
39
                         bool clearExistingCaps);
L
Laine Stump 已提交
40

J
John Ferlan 已提交
41 42
void virWaitForDevices(void);

43 44
int virScaleInteger(unsigned long long *value, const char *suffix,
                    unsigned long long scale, unsigned long long limit)
45
    ATTRIBUTE_NONNULL(1) G_GNUC_WARN_UNUSED_RESULT;
46

47 48
int virParseVersionString(const char *str, unsigned long *version,
                          bool allowMissing);
49

D
Daniel P. Berrange 已提交
50
char *virFormatIntDecimal(char *buf, size_t buflen, int val)
51
    ATTRIBUTE_NONNULL(1) G_GNUC_WARN_UNUSED_RESULT;
52

53 54 55 56
unsigned long long
virFormatIntPretty(unsigned long long val,
                   const char **unit);

57
int virDiskNameParse(const char *name, int *disk, int *partition);
58
int virDiskNameToIndex(const char* str);
59
char *virIndexToDiskName(int idx, const char *prefix);
60

61
/* No-op workarounds for functionality missing in mingw.  */
62
#ifndef HAVE_GETUID
63 64
static inline int getuid(void)
{ return 0; }
65
#endif
D
Daniel P. Berrange 已提交
66

67
#ifndef HAVE_GETEUID
68 69
static inline int geteuid(void)
{ return 0; }
70
#endif
71

72
#ifndef HAVE_GETGID
73 74
static inline int getgid(void)
{ return 0; }
75
#endif
D
Daniel P. Berrange 已提交
76

77
#ifndef HAVE_GETEGID
78 79
static inline int getegid(void)
{ return 0; }
80
#endif
81

82 83
#ifdef FUNC_PTHREAD_SIGMASK_BROKEN
# undef pthread_sigmask
84 85 86 87 88 89 90 91 92
static inline int pthread_sigmask(int how,
                                  const void *set,
                                  void *old)
{
    (void) how;
    (void) set;
    (void) old;
    return 0;
}
93
#endif
94

95
char *virGetHostname(void);
96
char *virGetHostnameQuiet(void);
97

98
char *virGetUserDirectory(void);
D
Dan Walsh 已提交
99
char *virGetUserDirectoryByUID(uid_t uid);
100 101 102
char *virGetUserConfigDirectory(void);
char *virGetUserCacheDirectory(void);
char *virGetUserRuntimeDirectory(void);
103
char *virGetUserShell(uid_t uid);
104 105
char *virGetUserName(uid_t uid) G_GNUC_NO_INLINE;
char *virGetGroupName(gid_t gid) G_GNUC_NO_INLINE;
E
Eric Blake 已提交
106 107
int virGetGroupList(uid_t uid, gid_t group, gid_t **groups)
    ATTRIBUTE_NONNULL(3);
108
int virGetUserID(const char *name,
109
                 uid_t *uid) G_GNUC_WARN_UNUSED_RESULT;
110
int virGetGroupID(const char *name,
111
                  gid_t *gid) G_GNUC_WARN_UNUSED_RESULT;
112

113 114
bool virDoesUserExist(const char *name);
bool virDoesGroupExist(const char *name);
115 116


117
bool virIsDevMapperDevice(const char *dev_name) ATTRIBUTE_NONNULL(1);
118

O
Osier Yang 已提交
119 120
bool virValidateWWN(const char *wwn);

121 122 123 124 125 126 127 128 129
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);
130 131
char *virGetUnprivSGIOSysfsPath(const char *path,
                                const char *sysfs_dir);
132

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

135 136 137 138

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

139 140
long virGetSystemPageSize(void) G_GNUC_NO_INLINE;
long virGetSystemPageSizeKB(void) G_GNUC_NO_INLINE;
141

142 143
unsigned long long virMemoryLimitTruncate(unsigned long long value);
bool virMemoryLimitIsSet(unsigned long long value);
144
unsigned long long virMemoryMaxValue(bool ulong) G_GNUC_NO_INLINE;
145

146 147
bool virHostHasIOMMU(void);

148
char *virHostGetDRMRenderNode(void) G_GNUC_NO_INLINE;
149

150 151 152 153 154 155 156 157
/**
 * 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.
 */
158
#define VIR_ASSIGN_IS_OVERFLOW(lvalue, rvalue) \
159
    (((lvalue) = (rvalue)) != (rvalue))
160 161

char *virGetPassword(void);
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

/*
 * virPipe:
 *
 * Open a pair of FDs which can be used to communicate
 * with each other. The FDs will have O_CLOEXEC set.
 * This will report a libvirt error on failure.
 *
 * Returns: -1 on error, 0 on success
 */
int virPipe(int fds[2]);

/*
 * virPipeQuiet:
 *
 * Open a pair of FDs which can be used to communicate
 * with each other. The FDs will have O_CLOEXEC set.
 * This will set errno on failure.
 *
 * Returns: -1 on error, 0 on success
 */
int virPipeQuiet(int fds[2]);

/*
 * virPipe:
 *
 * Open a pair of FDs which can be used to communicate
 * with each other. The FDs will have O_CLOEXEC and
 * O_NONBLOCK set.
 * This will report a libvirt error on failure.
 *
 * Returns: -1 on error, 0 on success
 */
int virPipeNonBlock(int fds[2]);