debuggers.hg

view tools/blktap2/drivers/tapdisk-utils.c @ 22848:6341fe0f4e5a

Added tag 4.1.0-rc2 for changeset 9dca60d88c63
author Keir Fraser <keir@xen.org>
date Tue Jan 25 14:06:55 2011 +0000 (2011-01-25)
parents 9009f9f76441
children
line source
1 /*
2 * Copyright (c) 2008, XenSource Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of XenSource Inc. nor the names of its contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
20 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include <errno.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <sys/stat.h>
33 #include <sys/mman.h>
34 #include <sys/ioctl.h>
35 #include <sys/resource.h>
36 #include <sys/utsname.h>
37 #ifdef __linux__
38 #include <linux/version.h>
39 #endif
41 #include "blk.h"
42 #include "tapdisk.h"
43 #include "blktaplib.h"
44 #include "tapdisk-log.h"
45 #include "tapdisk-utils.h"
47 void
48 tapdisk_start_logging(const char *name)
49 {
50 static char buf[128];
52 snprintf(buf, sizeof(buf), "%s[%d]", name, getpid());
53 openlog(buf, LOG_CONS | LOG_ODELAY, LOG_DAEMON);
54 open_tlog("/tmp/tapdisk.log", (64 << 10), TLOG_WARN, 0);
55 }
57 void
58 tapdisk_stop_logging(void)
59 {
60 closelog();
61 close_tlog();
62 }
64 int
65 tapdisk_set_resource_limits(void)
66 {
67 int err;
68 struct rlimit rlim;
70 rlim.rlim_cur = RLIM_INFINITY;
71 rlim.rlim_max = RLIM_INFINITY;
73 err = setrlimit(RLIMIT_MEMLOCK, &rlim);
74 if (err == -1) {
75 EPRINTF("RLIMIT_MEMLOCK failed: %d\n", errno);
76 return -errno;
77 }
79 err = mlockall(MCL_CURRENT | MCL_FUTURE);
80 if (err == -1) {
81 EPRINTF("mlockall failed: %d\n", errno);
82 return -errno;
83 }
85 #define CORE_DUMP
86 #if defined(CORE_DUMP)
87 err = setrlimit(RLIMIT_CORE, &rlim);
88 if (err == -1)
89 EPRINTF("RLIMIT_CORE failed: %d\n", errno);
90 #endif
92 return 0;
93 }
95 int
96 tapdisk_namedup(char **dup, const char *name)
97 {
98 *dup = NULL;
100 if (strnlen(name, MAX_NAME_LEN) >= MAX_NAME_LEN)
101 return -ENAMETOOLONG;
103 *dup = strdup(name);
104 if (!*dup)
105 return -ENOMEM;
107 return 0;
108 }
110 /*Get Image size, secsize*/
111 int
112 tapdisk_get_image_size(int fd, uint64_t *_sectors, uint32_t *_sector_size)
113 {
114 int ret;
115 struct stat stat;
116 uint64_t sectors;
117 uint64_t sector_size;
119 sectors = 0;
120 sector_size = 0;
121 *_sectors = 0;
122 *_sector_size = 0;
124 if (fstat(fd, &stat)) {
125 DPRINTF("ERROR: fstat failed, Couldn't stat image");
126 return -EINVAL;
127 }
129 if (S_ISBLK(stat.st_mode)) {
130 /*Accessing block device directly*/
131 if (blk_getimagesize(fd, &sectors) != 0)
132 return -EINVAL;
134 /*Get the sector size*/
135 if (blk_getsectorsize(fd, &sector_size) != 0)
136 sector_size = DEFAULT_SECTOR_SIZE;
137 } else {
138 /*Local file? try fstat instead*/
139 sectors = (stat.st_size >> SECTOR_SHIFT);
140 sector_size = DEFAULT_SECTOR_SIZE;
141 }
143 if (sectors == 0) {
144 sectors = 16836057ULL;
145 sector_size = DEFAULT_SECTOR_SIZE;
146 }
148 return 0;
149 }
151 #ifdef __linux__
153 int tapdisk_linux_version(void)
154 {
155 struct utsname uts;
156 unsigned int version, patchlevel, sublevel;
157 int n, err;
159 err = uname(&uts);
160 if (err)
161 return -errno;
163 n = sscanf(uts.release, "%u.%u.%u", &version, &patchlevel, &sublevel);
164 if (n != 3)
165 return -ENOSYS;
167 return KERNEL_VERSION(version, patchlevel, sublevel);
168 }
170 #else
172 int tapdisk_linux_version(void)
173 {
174 return -ENOSYS;
175 }
177 #endif