Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2016, Citrix Systems, Inc.
3 : : *
4 : : * All rights reserved.
5 : : *
6 : : * Redistribution and use in source and binary forms, with or without
7 : : * modification, are permitted provided that the following conditions are met:
8 : : *
9 : : * 1. Redistributions of source code must retain the above copyright
10 : : * notice, this list of conditions and the following disclaimer.
11 : : * 2. Redistributions in binary form must reproduce the above copyright
12 : : * notice, this list of conditions and the following disclaimer in the
13 : : * documentation and/or other materials provided with the distribution.
14 : : * 3. Neither the name of the copyright holder nor the names of its
15 : : * contributors may be used to endorse or promote products derived from
16 : : * this software without specific prior written permission.
17 : : *
18 : : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 : : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 : : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 : : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
22 : : * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 : : * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 : : * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 : : * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 : : * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 : : * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 : : * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 : : */
30 : :
31 : : #ifndef _TAPDISK_UTILS_H_
32 : : #define _TAPDISK_UTILS_H_
33 : :
34 : : #include <inttypes.h>
35 : : #include <sys/time.h>
36 : : #include <sys/types.h>
37 : : #include <sys/stat.h>
38 : : #include <fcntl.h>
39 : :
40 : : #define MAX_NAME_LEN 1000
41 : : #define TD_SYSLOG_IDENT_MAX 32
42 : : #define TD_SYSLOG_STRTIME_LEN 15
43 : :
44 : : int tapdisk_syslog_facility(const char *);
45 : : char* tapdisk_syslog_ident(const char *);
46 : : size_t tapdisk_syslog_strftime(char *, size_t, const struct timeval *);
47 : : size_t tapdisk_syslog_strftv(char *, size_t, const struct timeval *);
48 : : int tapdisk_set_resource_limits(void);
49 : : int tapdisk_namedup(char **, const char *);
50 : : int tapdisk_parse_disk_type(const char *, char **, int *);
51 : : int tapdisk_get_image_size(int, uint64_t *, uint32_t *);
52 : : int tapdisk_linux_version(void);
53 : : uint64_t ntohll(uint64_t);
54 : : #define htonll ntohll
55 : :
56 : :
57 : : /**
58 : : * Simplified version of snprintf that returns 0 if everything has gone OK and
59 : : * +errno if not (including the buffer not being large enough to hold the
60 : : * string).
61 : : */
62 : : int
63 : : tapdisk_snprintf(char *buf, int * const off, int * const size,
64 : : unsigned int depth, const char *format, ...);
65 : :
66 : : struct shm
67 : : {
68 : : char *path;
69 : : int fd;
70 : : void *mem;
71 : : unsigned size;
72 : : };
73 : :
74 : : /**
75 : : * Initialises a shm structure.
76 : : */
77 : : void
78 : : shm_init(struct shm *shm);
79 : :
80 : : /**
81 : : * Creates the file in /dev/shm. The caller must populate the path and size
82 : : * members of the shm structure passed to this function. Upon successful
83 : : * completion of this function, the caller can use the shm->mem to write up to
84 : : * shm.size bytes.
85 : : *
86 : : * Returns 0 in success, +errno on failure.
87 : : *
88 : : * XXX NB if the file is externally written to, the file size will change so
89 : : * the caller must cope with it (e.g. manually call ftruncate(2)).
90 : : */
91 : : int
92 : : shm_create(struct shm *shm);
93 : :
94 : : /**
95 : : * Destroys the file in /dev/shm. The caller is responsible for deallocating
96 : : * the path member in struct shm.
97 : : *
98 : : * Returns 0 in success, +errno on failure.
99 : : */
100 : : int
101 : : shm_destroy(struct shm *shm);
102 : :
103 : : static inline uint64_t timeval_to_us(struct timeval *tv)
104 : : {
105 : 2 : return (uint64_t)tv->tv_sec * 1000000 + tv->tv_usec;
106 : : }
107 : :
108 : : #endif
|