Commit 03ff3ca30f29f422ebfd10d2bee1393efb4d4f7a

Authored by aliguori
1 parent d9cf1578

Use common objects for qemu-img and qemu-nbd

Right now, we sprinkle #if defined(QEMU_IMG) && defined(QEMU_NBD) all over the
code.  It's ugly and causes us to have to build multiple object files for
linking against qemu and the tools.

This patch introduces a new file, qemu-tool.c which contains enough for
qemu-img, qemu-nbd, and QEMU to all share the same objects.

This also required getting qemu-nbd to be a bit more Windows friendly.  I also
changed the Windows block-raw to use normal IO instead of overlapping IO since
we don't actually do AIO yet on Windows.  I changed the various #if 0's to
 #if WIN32_AIO to make it easier for someone to eventually fix AIO on Windows.

After this patch, there are no longer any #ifdef's related to qemu-img and
qemu-nbd.

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>



git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@5226 c046a42c-6fe2-441c-8c8c-71466251a162
Makefile
... ... @@ -28,6 +28,10 @@ ifdef CONFIG_SOLARIS
28 28 LIBS+=-lsocket -lnsl -lresolv
29 29 endif
30 30  
  31 +ifdef CONFIG_WIN32
  32 +LIBS+=-lwinmm -lws2_32 -liphlpapi
  33 +endif
  34 +
31 35 all: $(TOOLS) $(DOCS) recurse-all
32 36  
33 37 SUBDIR_RULES=$(patsubst %,subdir-%, $(TARGET_DIRS))
... ... @@ -46,9 +50,17 @@ recurse-all: $(SUBDIR_RULES)
46 50 BLOCK_OBJS=cutils.o qemu-malloc.o
47 51 BLOCK_OBJS+=block-cow.o block-qcow.o aes.o block-vmdk.o block-cloop.o
48 52 BLOCK_OBJS+=block-dmg.o block-bochs.o block-vpc.o block-vvfat.o
49   -BLOCK_OBJS+=block-qcow2.o block-parallels.o
50   -ifndef CONFIG_WIN32
51   -BLOCK_OBJS+=block-nbd.o
  53 +BLOCK_OBJS+=block-qcow2.o block-parallels.o block-nbd.o
  54 +BLOCK_OBJS+=nbd.o block.o
  55 +
  56 +ifdef CONFIG_WIN32
  57 +BLOCK_OBJS += block-raw-win32.o
  58 +else
  59 +BLOCK_OBJS += block-raw-posix.o
  60 +endif
  61 +
  62 +ifdef CONFIG_AIO
  63 +BLOCK_OBJS += compatfd.o
52 64 endif
53 65  
54 66 ######################################################################
... ... @@ -59,11 +71,6 @@ endif
59 71  
60 72 OBJS=$(BLOCK_OBJS)
61 73 OBJS+=readline.o console.o
62   -OBJS+=block.o
63   -
64   -ifndef CONFIG_WIN32
65   -OBJS+=nbd.o
66   -endif
67 74  
68 75 OBJS+=irq.o
69 76 OBJS+=i2c.o smbus.o smbus_eeprom.o max7310.o max111x.o wm8750.o
... ... @@ -173,33 +180,15 @@ libqemu_user.a: $(USER_OBJS)
173 180 rm -f $@
174 181 $(AR) rcs $@ $(USER_OBJS)
175 182  
176   -QEMU_IMG_BLOCK_OBJS = $(BLOCK_OBJS)
177   -ifdef CONFIG_WIN32
178   -QEMU_IMG_BLOCK_OBJS += qemu-img-block-raw-win32.o
179   -else
180   -QEMU_IMG_BLOCK_OBJS += nbd.o qemu-img-block-raw-posix.o
181   -endif
182   -
183   -ifdef CONFIG_AIO
184   -QEMU_IMG_BLOCK_OBJS += compatfd.o
185   -endif
186   -
187 183 ######################################################################
188 184  
189   -qemu-img$(EXESUF): qemu-img.o qemu-img-block.o $(QEMU_IMG_BLOCK_OBJS)
  185 +qemu-img$(EXESUF): qemu-img.o qemu-tool.o osdep.o $(BLOCK_OBJS)
190 186 $(CC) $(LDFLAGS) -o $@ $^ -lz $(LIBS)
191 187  
192   -qemu-img-%.o: %.c
193   - $(CC) $(CFLAGS) $(CPPFLAGS) -DQEMU_IMG -c -o $@ $<
194   -
195 188 %.o: %.c
196 189 $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<
197 190  
198   -qemu-nbd-%.o: %.c
199   - $(CC) $(CFLAGS) $(CPPFLAGS) -DQEMU_NBD -c -o $@ $<
200   -
201   -qemu-nbd$(EXESUF): qemu-nbd.o qemu-nbd-nbd.o qemu-img-block.o \
202   - osdep.o qemu-nbd-block-raw-posix.o compatfd.o $(BLOCK_OBJS)
  191 +qemu-nbd$(EXESUF): qemu-nbd.o qemu-tool.o osdep.o $(BLOCK_OBJS)
203 192 $(CC) $(LDFLAGS) -o $@ $^ -lz $(LIBS)
204 193  
205 194 # dyngen host tool
... ...
block-raw-posix.c
... ... @@ -22,11 +22,8 @@
22 22 * THE SOFTWARE.
23 23 */
24 24 #include "qemu-common.h"
25   -#if !defined(QEMU_IMG) && !defined(QEMU_NBD)
26 25 #include "qemu-timer.h"
27   -#include "exec-all.h"
28 26 #include "qemu-char.h"
29   -#endif
30 27 #include "block_int.h"
31 28 #include "compatfd.h"
32 29 #include <assert.h>
... ... @@ -70,7 +67,7 @@
70 67 //#define DEBUG_FLOPPY
71 68  
72 69 //#define DEBUG_BLOCK
73   -#if defined(DEBUG_BLOCK) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
  70 +#if defined(DEBUG_BLOCK)
74 71 #define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (loglevel != 0) \
75 72 { fprintf(logfile, formatCstr, ##args); fflush(logfile); } } while (0)
76 73 #else
... ... @@ -99,7 +96,7 @@ typedef struct BDRVRawState {
99 96 int fd_got_error;
100 97 int fd_media_changed;
101 98 #endif
102   -#if defined(O_DIRECT) && !defined(QEMU_IMG)
  99 +#if defined(O_DIRECT)
103 100 uint8_t* aligned_buf;
104 101 #endif
105 102 } BDRVRawState;
... ... @@ -137,7 +134,7 @@ static int raw_open(BlockDriverState *bs, const char *filename, int flags)
137 134 return ret;
138 135 }
139 136 s->fd = fd;
140   -#if defined(O_DIRECT) && !defined(QEMU_IMG)
  137 +#if defined(O_DIRECT)
141 138 s->aligned_buf = NULL;
142 139 if (flags & BDRV_O_DIRECT) {
143 140 s->aligned_buf = qemu_memalign(512, ALIGNED_BUFFER_SIZE);
... ... @@ -272,7 +269,7 @@ label__raw_write__success:
272 269 }
273 270  
274 271  
275   -#if defined(O_DIRECT) && !defined(QEMU_IMG)
  272 +#if defined(O_DIRECT)
276 273 /*
277 274 * offset and count are in bytes and possibly not aligned. For files opened
278 275 * with O_DIRECT, necessary alignments are ensured before calling
... ... @@ -525,9 +522,7 @@ void qemu_aio_init(void)
525 522  
526 523 fcntl(aio_sig_fd, F_SETFL, O_NONBLOCK);
527 524  
528   -#if !defined(QEMU_IMG) && !defined(QEMU_NBD)
529 525 qemu_set_fd_handler2(aio_sig_fd, NULL, qemu_aio_poll, NULL, NULL);
530   -#endif
531 526  
532 527 #if defined(__GLIBC__) && defined(__linux__)
533 528 {
... ... @@ -556,10 +551,8 @@ void qemu_aio_wait(void)
556 551 {
557 552 int ret;
558 553  
559   -#if !defined(QEMU_IMG) && !defined(QEMU_NBD)
560 554 if (qemu_bh_poll())
561 555 return;
562   -#endif
563 556  
564 557 if (!first_aio)
565 558 return;
... ... @@ -605,14 +598,12 @@ static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
605 598 return acb;
606 599 }
607 600  
608   -#if !defined(QEMU_IMG) && !defined(QEMU_NBD)
609 601 static void raw_aio_em_cb(void* opaque)
610 602 {
611 603 RawAIOCB *acb = opaque;
612 604 acb->common.cb(acb->common.opaque, acb->ret);
613 605 qemu_aio_release(acb);
614 606 }
615   -#endif
616 607  
617 608 static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
618 609 int64_t sector_num, uint8_t *buf, int nb_sectors,
... ... @@ -624,7 +615,7 @@ static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
624 615 * If O_DIRECT is used and the buffer is not aligned fall back
625 616 * to synchronous IO.
626 617 */
627   -#if defined(O_DIRECT) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
  618 +#if defined(O_DIRECT)
628 619 BDRVRawState *s = bs->opaque;
629 620  
630 621 if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
... ... @@ -657,7 +648,7 @@ static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
657 648 * If O_DIRECT is used and the buffer is not aligned fall back
658 649 * to synchronous IO.
659 650 */
660   -#if defined(O_DIRECT) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
  651 +#if defined(O_DIRECT)
661 652 BDRVRawState *s = bs->opaque;
662 653  
663 654 if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
... ... @@ -719,9 +710,7 @@ void qemu_aio_flush(void)
719 710  
720 711 void qemu_aio_wait(void)
721 712 {
722   -#if !defined(QEMU_IMG) && !defined(QEMU_NBD)
723 713 qemu_bh_poll();
724   -#endif
725 714 }
726 715  
727 716 #endif /* CONFIG_AIO */
... ... @@ -732,7 +721,7 @@ static void raw_close(BlockDriverState *bs)
732 721 if (s->fd >= 0) {
733 722 close(s->fd);
734 723 s->fd = -1;
735   -#if defined(O_DIRECT) && !defined(QEMU_IMG)
  724 +#if defined(O_DIRECT)
736 725 if (s->aligned_buf != NULL)
737 726 qemu_free(s->aligned_buf);
738 727 #endif
... ... @@ -1002,7 +991,7 @@ static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
1002 991 return 0;
1003 992 }
1004 993  
1005   -#if defined(__linux__) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
  994 +#if defined(__linux__)
1006 995  
1007 996 /* Note: we do not have a reliable method to detect if the floppy is
1008 997 present. The current method is to try to open the floppy at every
... ... @@ -1052,14 +1041,6 @@ static int fd_open(BlockDriverState *bs)
1052 1041 s->fd_got_error = 0;
1053 1042 return 0;
1054 1043 }
1055   -#else
1056   -static int fd_open(BlockDriverState *bs)
1057   -{
1058   - return 0;
1059   -}
1060   -#endif
1061   -
1062   -#if defined(__linux__)
1063 1044  
1064 1045 static int raw_is_inserted(BlockDriverState *bs)
1065 1046 {
... ...
block-raw-win32.c
... ... @@ -22,14 +22,13 @@
22 22 * THE SOFTWARE.
23 23 */
24 24 #include "qemu-common.h"
25   -#ifndef QEMU_IMG
26 25 #include "qemu-timer.h"
27   -#include "exec-all.h"
28   -#endif
29 26 #include "block_int.h"
30 27 #include <assert.h>
31 28 #include <winioctl.h>
32 29  
  30 +//#define WIN32_AIO
  31 +
33 32 #define FTYPE_FILE 0
34 33 #define FTYPE_CD 1
35 34 #define FTYPE_HARDDISK 2
... ... @@ -100,10 +99,10 @@ static int raw_open(BlockDriverState *bs, const char *filename, int flags)
100 99 } else {
101 100 create_flags = OPEN_EXISTING;
102 101 }
103   -#ifdef QEMU_IMG
104   - overlapped = FILE_ATTRIBUTE_NORMAL;
105   -#else
  102 +#ifdef WIN32_AIO
106 103 overlapped = FILE_FLAG_OVERLAPPED;
  104 +#else
  105 + overlapped = FILE_ATTRIBUTE_NORMAL;
107 106 #endif
108 107 if (flags & BDRV_O_DIRECT)
109 108 overlapped |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
... ... @@ -133,10 +132,12 @@ static int raw_pread(BlockDriverState *bs, int64_t offset,
133 132 ov.OffsetHigh = offset >> 32;
134 133 ret = ReadFile(s->hfile, buf, count, &ret_count, &ov);
135 134 if (!ret) {
  135 +#ifdef WIN32_AIO
136 136 ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
137 137 if (!ret)
138 138 return -EIO;
139 139 else
  140 +#endif
140 141 return ret_count;
141 142 }
142 143 return ret_count;
... ... @@ -155,17 +156,18 @@ static int raw_pwrite(BlockDriverState *bs, int64_t offset,
155 156 ov.OffsetHigh = offset >> 32;
156 157 ret = WriteFile(s->hfile, buf, count, &ret_count, &ov);
157 158 if (!ret) {
  159 +#ifdef WIN32_AIO
158 160 ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
159 161 if (!ret)
160 162 return -EIO;
161 163 else
  164 +#endif
162 165 return ret_count;
163 166 }
164 167 return ret_count;
165 168 }
166 169  
167   -#if 0
168   -#ifndef QEMU_IMG
  170 +#ifdef WIN32_AIO
169 171 static void raw_aio_cb(void *opaque)
170 172 {
171 173 RawAIOCB *acb = opaque;
... ... @@ -181,7 +183,6 @@ static void raw_aio_cb(void *opaque)
181 183 acb->common.cb(acb->common.opaque, 0);
182 184 }
183 185 }
184   -#endif
185 186  
186 187 static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
187 188 int64_t sector_num, uint8_t *buf, int nb_sectors,
... ... @@ -204,9 +205,7 @@ static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
204 205 acb->ov.OffsetHigh = offset >> 32;
205 206 acb->ov.hEvent = acb->hEvent;
206 207 acb->count = nb_sectors * 512;
207   -#ifndef QEMU_IMG
208 208 qemu_add_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
209   -#endif
210 209 return acb;
211 210 }
212 211  
... ... @@ -226,9 +225,7 @@ static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
226 225 qemu_aio_release(acb);
227 226 return NULL;
228 227 }
229   -#ifdef QEMU_IMG
230 228 qemu_aio_release(acb);
231   -#endif
232 229 return (BlockDriverAIOCB *)acb;
233 230 }
234 231  
... ... @@ -248,15 +245,12 @@ static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
248 245 qemu_aio_release(acb);
249 246 return NULL;
250 247 }
251   -#ifdef QEMU_IMG
252 248 qemu_aio_release(acb);
253   -#endif
254 249 return (BlockDriverAIOCB *)acb;
255 250 }
256 251  
257 252 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
258 253 {
259   -#ifndef QEMU_IMG
260 254 RawAIOCB *acb = (RawAIOCB *)blockacb;
261 255 BlockDriverState *bs = acb->common.bs;
262 256 BDRVRawState *s = bs->opaque;
... ... @@ -265,9 +259,8 @@ static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
265 259 /* XXX: if more than one async I/O it is not correct */
266 260 CancelIo(s->hfile);
267 261 qemu_aio_release(acb);
268   -#endif
269 262 }
270   -#endif /* #if 0 */
  263 +#endif /* #if WIN32_AIO */
271 264  
272 265 static void raw_flush(BlockDriverState *bs)
273 266 {
... ... @@ -356,9 +349,7 @@ void qemu_aio_flush(void)
356 349  
357 350 void qemu_aio_wait(void)
358 351 {
359   -#ifndef QEMU_IMG
360 352 qemu_bh_poll();
361   -#endif
362 353 }
363 354  
364 355 BlockDriver bdrv_raw = {
... ... @@ -372,7 +363,7 @@ BlockDriver bdrv_raw = {
372 363 raw_create,
373 364 raw_flush,
374 365  
375   -#if 0
  366 +#ifdef WIN32_AIO
376 367 .bdrv_aio_read = raw_aio_read,
377 368 .bdrv_aio_write = raw_aio_write,
378 369 .bdrv_aio_cancel = raw_aio_cancel,
... ... @@ -458,10 +449,10 @@ static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
458 449 }
459 450 create_flags = OPEN_EXISTING;
460 451  
461   -#ifdef QEMU_IMG
462   - overlapped = FILE_ATTRIBUTE_NORMAL;
463   -#else
  452 +#ifdef WIN32_AIO
464 453 overlapped = FILE_FLAG_OVERLAPPED;
  454 +#else
  455 + overlapped = FILE_ATTRIBUTE_NORMAL;
465 456 #endif
466 457 if (flags & BDRV_O_DIRECT)
467 458 overlapped |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
... ... @@ -524,7 +515,7 @@ BlockDriver bdrv_host_device = {
524 515 NULL,
525 516 raw_flush,
526 517  
527   -#if 0
  518 +#ifdef WIN32_AIO
528 519 .bdrv_aio_read = raw_aio_read,
529 520 .bdrv_aio_write = raw_aio_write,
530 521 .bdrv_aio_cancel = raw_aio_cancel,
... ...
... ... @@ -22,9 +22,7 @@
22 22 * THE SOFTWARE.
23 23 */
24 24 #include "qemu-common.h"
25   -#ifndef QEMU_IMG
26 25 #include "console.h"
27   -#endif
28 26 #include "block_int.h"
29 27  
30 28 #ifdef _BSD
... ... @@ -922,7 +920,6 @@ int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
922 920 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
923 921 }
924 922  
925   -#ifndef QEMU_IMG
926 923 void bdrv_info(void)
927 924 {
928 925 BlockDriverState *bs;
... ... @@ -980,7 +977,6 @@ void bdrv_info_stats (void)
980 977 bs->rd_ops, bs->wr_ops);
981 978 }
982 979 }
983   -#endif
984 980  
985 981 void bdrv_get_backing_filename(BlockDriverState *bs,
986 982 char *filename, int filename_size)
... ... @@ -1203,31 +1199,6 @@ void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1203 1199 /**************************************************************/
1204 1200 /* async block device emulation */
1205 1201  
1206   -#ifdef QEMU_IMG
1207   -static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1208   - int64_t sector_num, uint8_t *buf, int nb_sectors,
1209   - BlockDriverCompletionFunc *cb, void *opaque)
1210   -{
1211   - int ret;
1212   - ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1213   - cb(opaque, ret);
1214   - return NULL;
1215   -}
1216   -
1217   -static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1218   - int64_t sector_num, const uint8_t *buf, int nb_sectors,
1219   - BlockDriverCompletionFunc *cb, void *opaque)
1220   -{
1221   - int ret;
1222   - ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1223   - cb(opaque, ret);
1224   - return NULL;
1225   -}
1226   -
1227   -static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb)
1228   -{
1229   -}
1230   -#else
1231 1202 static void bdrv_aio_bh_cb(void *opaque)
1232 1203 {
1233 1204 BlockDriverAIOCBSync *acb = opaque;
... ... @@ -1273,7 +1244,6 @@ static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1273 1244 qemu_bh_cancel(acb->bh);
1274 1245 qemu_aio_release(acb);
1275 1246 }
1276   -#endif /* !QEMU_IMG */
1277 1247  
1278 1248 /**************************************************************/
1279 1249 /* sync block device emulation */
... ... @@ -1337,9 +1307,7 @@ void bdrv_init(void)
1337 1307 bdrv_register(&bdrv_vvfat);
1338 1308 bdrv_register(&bdrv_qcow2);
1339 1309 bdrv_register(&bdrv_parallels);
1340   -#ifndef _WIN32
1341 1310 bdrv_register(&bdrv_nbd);
1342   -#endif
1343 1311  
1344 1312 qemu_aio_init();
1345 1313 }
... ...
... ... @@ -47,10 +47,8 @@ typedef struct QEMUSnapshotInfo {
47 47 bdrv_file_open()) */
48 48 #define BDRV_O_DIRECT 0x0020
49 49  
50   -#ifndef QEMU_IMG
51 50 void bdrv_info(void);
52 51 void bdrv_info_stats(void);
53   -#endif
54 52  
55 53 void bdrv_init(void);
56 54 BlockDriver *bdrv_find_format(const char *format_name);
... ...
... ... @@ -21,28 +21,27 @@
21 21  
22 22 #include <errno.h>
23 23 #include <string.h>
  24 +#ifndef _WIN32
24 25 #include <sys/ioctl.h>
  26 +#endif
25 27 #ifdef __sun__
26 28 #include <sys/ioccom.h>
27 29 #endif
28 30 #include <ctype.h>
29 31 #include <inttypes.h>
30   -#include <sys/socket.h>
31   -#include <sys/un.h>
32   -#include <netinet/in.h>
33   -#include <netinet/tcp.h>
34   -#include <arpa/inet.h>
35   -#include <netdb.h>
36   -
37   -#if defined(QEMU_NBD)
38   -extern int verbose;
39   -#else
40   -static int verbose = 0;
41   -#endif
42 32  
  33 +#include "qemu_socket.h"
  34 +
  35 +//#define DEBUG_NBD
  36 +
  37 +#ifdef DEBUG_NBD
43 38 #define TRACE(msg, ...) do { \
44   - if (verbose) LOG(msg, ## __VA_ARGS__); \
  39 + LOG(msg, ## __VA_ARGS__); \
45 40 } while(0)
  41 +#else
  42 +#define TRACE(msg, ...) \
  43 + do { } while (0)
  44 +#endif
46 45  
47 46 #define LOG(msg, ...) do { \
48 47 fprintf(stderr, "%s:%s():L%d: " msg "\n", \
... ... @@ -77,11 +76,14 @@ size_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read)
77 76 ssize_t len;
78 77  
79 78 if (do_read) {
80   - len = read(fd, buffer + offset, size - offset);
  79 + len = recv(fd, buffer + offset, size - offset, 0);
81 80 } else {
82   - len = write(fd, buffer + offset, size - offset);
  81 + len = send(fd, buffer + offset, size - offset, 0);
83 82 }
84 83  
  84 + if (len == -1)
  85 + errno = socket_error();
  86 +
85 87 /* recoverable error */
86 88 if (len == -1 && (errno == EAGAIN || errno == EINTR)) {
87 89 continue;
... ... @@ -108,7 +110,6 @@ int tcp_socket_outgoing(const char *address, uint16_t port)
108 110 int s;
109 111 struct in_addr in;
110 112 struct sockaddr_in addr;
111   - int serrno;
112 113  
113 114 s = socket(PF_INET, SOCK_STREAM, 0);
114 115 if (s == -1) {
... ... @@ -136,9 +137,7 @@ int tcp_socket_outgoing(const char *address, uint16_t port)
136 137  
137 138 return s;
138 139 error:
139   - serrno = errno;
140   - close(s);
141   - errno = serrno;
  140 + closesocket(s);
142 141 return -1;
143 142 }
144 143  
... ... @@ -147,7 +146,6 @@ int tcp_socket_incoming(const char *address, uint16_t port)
147 146 int s;
148 147 struct in_addr in;
149 148 struct sockaddr_in addr;
150   - int serrno;
151 149 int opt;
152 150  
153 151 s = socket(PF_INET, SOCK_STREAM, 0);
... ... @@ -185,17 +183,15 @@ int tcp_socket_incoming(const char *address, uint16_t port)
185 183  
186 184 return s;
187 185 error:
188   - serrno = errno;
189   - close(s);
190   - errno = serrno;
  186 + closesocket(s);
191 187 return -1;
192 188 }
193 189  
  190 +#ifndef _WIN32
194 191 int unix_socket_incoming(const char *path)
195 192 {
196 193 int s;
197 194 struct sockaddr_un addr;
198   - int serrno;
199 195  
200 196 s = socket(PF_UNIX, SOCK_STREAM, 0);
201 197 if (s == -1) {
... ... @@ -216,9 +212,7 @@ int unix_socket_incoming(const char *path)
216 212  
217 213 return s;
218 214 error:
219   - serrno = errno;
220   - close(s);
221   - errno = serrno;
  215 + closesocket(s);
222 216 return -1;
223 217 }
224 218  
... ... @@ -226,7 +220,6 @@ int unix_socket_outgoing(const char *path)
226 220 {
227 221 int s;
228 222 struct sockaddr_un addr;
229   - int serrno;
230 223  
231 224 s = socket(PF_UNIX, SOCK_STREAM, 0);
232 225 if (s == -1) {
... ... @@ -243,12 +236,23 @@ int unix_socket_outgoing(const char *path)
243 236  
244 237 return s;
245 238 error:
246   - serrno = errno;
247   - close(s);
248   - errno = serrno;
  239 + closesocket(s);
  240 + return -1;
  241 +}
  242 +#else
  243 +int unix_socket_incoming(const char *path)
  244 +{
  245 + errno = ENOTSUP;
249 246 return -1;
250 247 }
251 248  
  249 +int unix_socket_outgoing(const char *path)
  250 +{
  251 + errno = ENOTSUP;
  252 + return -1;
  253 +}
  254 +#endif
  255 +
252 256  
253 257 /* Basic flow
254 258  
... ... @@ -337,6 +341,7 @@ int nbd_receive_negotiate(int csock, off_t *size, size_t *blocksize)
337 341 return 0;
338 342 }
339 343  
  344 +#ifndef _WIN32
340 345 int nbd_init(int fd, int csock, off_t size, size_t blocksize)
341 346 {
342 347 TRACE("Setting block size to %lu", (unsigned long)blocksize);
... ... @@ -410,6 +415,25 @@ int nbd_client(int fd, int csock)
410 415 errno = serrno;
411 416 return ret;
412 417 }
  418 +#else
  419 +int nbd_init(int fd, int csock, off_t size, size_t blocksize)
  420 +{
  421 + errno = ENOTSUP;
  422 + return -1;
  423 +}
  424 +
  425 +int nbd_disconnect(int fd)
  426 +{
  427 + errno = ENOTSUP;
  428 + return -1;
  429 +}
  430 +
  431 +int nbd_client(int fd, int csock)
  432 +{
  433 + errno = ENOTSUP;
  434 + return -1;
  435 +}
  436 +#endif
413 437  
414 438 int nbd_send_request(int csock, struct nbd_request *request)
415 439 {
... ...
... ... @@ -45,6 +45,8 @@
45 45 #include <malloc.h>
46 46 #endif
47 47  
  48 +#include "qemu_socket.h"
  49 +
48 50 #if defined(_WIN32)
49 51 void *qemu_memalign(size_t alignment, size_t size)
50 52 {
... ... @@ -283,3 +285,28 @@ int qemu_gettimeofday(qemu_timeval *tp)
283 285 return 0;
284 286 }
285 287 #endif /* _WIN32 */
  288 +
  289 +
  290 +#ifdef _WIN32
  291 +void socket_set_nonblock(int fd)
  292 +{
  293 + unsigned long opt = 1;
  294 + ioctlsocket(fd, FIONBIO, &opt);
  295 +}
  296 +
  297 +int inet_aton(const char *cp, struct in_addr *ia)
  298 +{
  299 + uint32_t addr = inet_addr(cp);
  300 + if (addr == 0xffffffff)
  301 + return 0;
  302 + ia->s_addr = addr;
  303 + return 1;
  304 +}
  305 +#else
  306 +void socket_set_nonblock(int fd)
  307 +{
  308 + int f;
  309 + f = fcntl(fd, F_GETFL);
  310 + fcntl(fd, F_SETFL, f | O_NONBLOCK);
  311 +}
  312 +#endif
... ...
qemu-tool.c 0 โ†’ 100644
  1 +/*
  2 + * Compatibility for qemu-img/qemu-nbd
  3 + *
  4 + * Copyright IBM, Corp. 2008
  5 + *
  6 + * Authors:
  7 + * Anthony Liguori <aliguori@us.ibm.com>
  8 + *
  9 + * This work is licensed under the terms of the GNU GPL, version 2. See
  10 + * the COPYING file in the top-level directory.
  11 + *
  12 + */
  13 +
  14 +#include "qemu-common.h"
  15 +#include "console.h"
  16 +#include "sysemu.h"
  17 +#include "qemu-timer.h"
  18 +
  19 +#include <sys/time.h>
  20 +
  21 +QEMUClock *rt_clock;
  22 +
  23 +struct QEMUBH
  24 +{
  25 + QEMUBHFunc *cb;
  26 + void *opaque;
  27 +};
  28 +
  29 +void term_printf(const char *fmt, ...)
  30 +{
  31 +}
  32 +
  33 +void term_print_filename(const char *filename)
  34 +{
  35 +}
  36 +
  37 +QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
  38 +{
  39 + QEMUBH *bh;
  40 +
  41 + bh = qemu_malloc(sizeof(*bh));
  42 + if (bh) {
  43 + bh->cb = cb;
  44 + bh->opaque = opaque;
  45 + }
  46 +
  47 + return bh;
  48 +}
  49 +
  50 +int qemu_bh_poll(void)
  51 +{
  52 + return 0;
  53 +}
  54 +
  55 +void qemu_bh_schedule(QEMUBH *bh)
  56 +{
  57 + bh->cb(bh->opaque);
  58 +}
  59 +
  60 +void qemu_bh_cancel(QEMUBH *bh)
  61 +{
  62 +}
  63 +
  64 +void qemu_bh_delete(QEMUBH *bh)
  65 +{
  66 + qemu_free(bh);
  67 +}
  68 +
  69 +int qemu_set_fd_handler2(int fd,
  70 + IOCanRWHandler *fd_read_poll,
  71 + IOHandler *fd_read,
  72 + IOHandler *fd_write,
  73 + void *opaque)
  74 +{
  75 + return 0;
  76 +}
  77 +
  78 +int64_t qemu_get_clock(QEMUClock *clock)
  79 +{
  80 + struct timeval tv;
  81 + gettimeofday(&tv, NULL);
  82 + return (tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000)) / 1000000;
  83 +}
... ...
qemu_socket.h
... ... @@ -14,11 +14,15 @@
14 14 #define EINTR WSAEINTR
15 15 #define EINPROGRESS WSAEINPROGRESS
16 16  
  17 +int inet_aton(const char *cp, struct in_addr *ia);
  18 +
17 19 #else
18 20  
19 21 #include <sys/socket.h>
20 22 #include <netinet/in.h>
21 23 #include <netinet/tcp.h>
  24 +#include <arpa/inet.h>
  25 +#include <netdb.h>
22 26 #include <sys/un.h>
23 27  
24 28 #define socket_error() errno
... ...
slirp/misc.c
... ... @@ -66,20 +66,6 @@ redir_x(inaddr, start_port, display, screen)
66 66 }
67 67 #endif
68 68  
69   -#ifndef HAVE_INET_ATON
70   -int
71   -inet_aton(cp, ia)
72   - const char *cp;
73   - struct in_addr *ia;
74   -{
75   - u_int32_t addr = inet_addr(cp);
76   - if (addr == 0xffffffff)
77   - return 0;
78   - ia->s_addr = addr;
79   - return 1;
80   -}
81   -#endif
82   -
83 69 /*
84 70 * Get our IP address and put it in our_addr
85 71 */
... ...
... ... @@ -100,11 +100,10 @@
100 100 #include <stropts.h>
101 101 #endif
102 102 #endif
103   -#else
104   -#include <winsock2.h>
105   -int inet_aton(const char *cp, struct in_addr *ia);
106 103 #endif
107 104  
  105 +#include "qemu_socket.h"
  106 +
108 107 #if defined(CONFIG_SLIRP)
109 108 #include "libslirp.h"
110 109 #endif
... ... @@ -125,8 +124,6 @@ int inet_aton(const char *cp, struct in_addr *ia);
125 124 #define memalign(align, size) malloc(size)
126 125 #endif
127 126  
128   -#include "qemu_socket.h"
129   -
130 127 #ifdef CONFIG_SDL
131 128 #ifdef __APPLE__
132 129 #include <SDL/SDL.h>
... ... @@ -2133,12 +2130,6 @@ static int send_all(int fd, const uint8_t *buf, int len1)
2133 2130 return len1 - len;
2134 2131 }
2135 2132  
2136   -void socket_set_nonblock(int fd)
2137   -{
2138   - unsigned long opt = 1;
2139   - ioctlsocket(fd, FIONBIO, &opt);
2140   -}
2141   -
2142 2133 #else
2143 2134  
2144 2135 static int unix_write(int fd, const uint8_t *buf, int len1)
... ... @@ -2165,13 +2156,6 @@ static inline int send_all(int fd, const uint8_t *buf, int len1)
2165 2156 {
2166 2157 return unix_write(fd, buf, len1);
2167 2158 }
2168   -
2169   -void socket_set_nonblock(int fd)
2170   -{
2171   - int f;
2172   - f = fcntl(fd, F_GETFL);
2173   - fcntl(fd, F_SETFL, f | O_NONBLOCK);
2174   -}
2175 2159 #endif /* !_WIN32 */
2176 2160  
2177 2161 #ifndef _WIN32
... ...