rump_allserver.c revision 1.38 1 /* $NetBSD: rump_allserver.c,v 1.38 2014/11/04 19:05:17 pooka Exp $ */
2
3 /*-
4 * Copyright (c) 2010, 2011 Antti Kantee. 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
8 * are met:
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 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <rump/rumpuser_port.h>
29
30 #ifndef lint
31 __RCSID("$NetBSD: rump_allserver.c,v 1.38 2014/11/04 19:05:17 pooka Exp $");
32 #endif /* !lint */
33
34 #include <sys/types.h>
35 #include <sys/stat.h>
36
37 #include <dlfcn.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <semaphore.h>
41 #include <signal.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <stdint.h>
45 #include <string.h>
46 #include <unistd.h>
47
48 #include <rump/rump.h>
49 #include <rump/rump_syscalls.h>
50 #include <rump/rumpdefs.h>
51 #include <rump/rumperr.h>
52
53 __dead static void
54 usage(void)
55 {
56
57 #ifndef HAVE_GETPROGNAME
58 #define getprogname() "rump_server"
59 #endif
60 fprintf(stderr, "usage: %s [-s] [-c ncpu] [-d drivespec] [-l libs] "
61 "[-m modules] bindurl\n", getprogname());
62 exit(1);
63 }
64
65 __dead static void
66 diedie(int sflag, const char *reason, int error, const char *errstr)
67 {
68
69 if (reason != NULL)
70 fputs(reason, stderr);
71 if (errstr) {
72 fprintf(stderr, ": %s", errstr);
73 }
74 fputc('\n', stderr);
75 if (!sflag)
76 rump_daemonize_done(error);
77 exit(1);
78 }
79
80 __dead static void
81 die(int sflag, int error, const char *reason)
82 {
83
84 diedie(sflag, reason, error, error == 0 ? NULL : strerror(error));
85 }
86
87 __dead static void
88 die_rumperr(int sflag, int error, const char *reason)
89 {
90
91 diedie(sflag, reason, error, error == 0 ? NULL : rump_strerror(error));
92 }
93
94 static sem_t sigsem;
95 static void
96 sigreboot(int sig)
97 {
98
99 sem_post(&sigsem);
100 }
101
102 static const char *const disktokens[] = {
103 #define DKEY 0
104 "key",
105 #define DFILE 1
106 "hostpath",
107 #define DSIZE 2
108 #define DSIZE_E -1
109 "size",
110 #define DOFFSET 3
111 "offset",
112 #define DLABEL 4
113 "disklabel",
114 #define DTYPE 5
115 "type",
116 NULL
117 };
118
119 struct etfsreg {
120 const char *key;
121 const char *hostpath;
122 off_t flen;
123 off_t foffset;
124 char partition;
125 enum rump_etfs_type type;
126 };
127
128 struct etfstype {
129 const char *name;
130 enum rump_etfs_type type;
131 } etfstypes[] = {
132 { "blk", RUMP_ETFS_BLK },
133 { "chr", RUMP_ETFS_CHR },
134 { "reg", RUMP_ETFS_REG },
135 };
136
137 static void processlabel(int, int, int, off_t *, off_t *);
138
139 #define ALLOCCHUNK 32
140
141 int
142 main(int argc, char *argv[])
143 {
144 const char *serverurl;
145 struct etfsreg *etfs = NULL;
146 unsigned netfs = 0, curetfs = 0;
147 int error;
148 int ch, sflag, onthepath;
149 unsigned i;
150 char **modarray = NULL, **libarray = NULL;
151 unsigned nmods = 0, curmod = 0, nlibs = 0, curlib = 0, libidx;
152 unsigned liblast = -1; /* XXXgcc */
153
154 setprogname(argv[0]);
155 sflag = 0;
156 while ((ch = getopt(argc, argv, "c:d:l:m:r:sv")) != -1) {
157 switch (ch) {
158 case 'c':
159 setenv("RUMP_NCPU", optarg, 1);
160 break;
161 case 'd': {
162 char *options, *value;
163 char *key, *hostpath;
164 long long flen, foffset;
165 char partition;
166 int ftype;
167
168 flen = foffset = 0;
169 partition = 0;
170 key = hostpath = NULL;
171 ftype = -1;
172 options = optarg;
173 while (*options) {
174 switch (getsubopt(&options,
175 __UNCONST(disktokens), &value)) {
176 case DKEY:
177 if (key != NULL) {
178 fprintf(stderr,
179 "key already given\n");
180 usage();
181 }
182 key = value;
183 break;
184
185 case DFILE:
186 if (hostpath != NULL) {
187 fprintf(stderr,
188 "hostpath already given\n");
189 usage();
190 }
191 hostpath = value;
192 break;
193
194 case DSIZE:
195 if (flen != 0) {
196 fprintf(stderr,
197 "size already given\n");
198 usage();
199 }
200 if (strcmp(value, "host") == 0) {
201 if (foffset != 0) {
202 fprintf(stderr,
203 "cannot specify "
204 "offset with "
205 "size=host\n");
206 usage();
207 }
208 flen = DSIZE_E;
209 } else {
210 #ifdef HAVE_STRSUFTOLL
211 /* XXX: off_t max? */
212 flen = strsuftoll("-d size",
213 value, 0, LLONG_MAX);
214 #else
215 flen = strtoull(value,
216 NULL, 10);
217 #endif
218 }
219 break;
220 case DOFFSET:
221 if (foffset != 0) {
222 fprintf(stderr,
223 "offset already given\n");
224 usage();
225 }
226 if (flen == DSIZE_E) {
227 fprintf(stderr, "cannot "
228 "specify offset with "
229 "size=host\n");
230 usage();
231 }
232 #ifdef HAVE_STRSUFTOLL
233 /* XXX: off_t max? */
234 foffset = strsuftoll("-d offset", value,
235 0, LLONG_MAX);
236 #else
237 foffset = strtoull(value, NULL, 10);
238 #endif
239 break;
240
241 case DLABEL:
242 if (foffset != 0 || flen != 0) {
243 fprintf(stderr,
244 "disklabel needs to be "
245 "used alone\n");
246 usage();
247 }
248 if (strlen(value) != 1 ||
249 *value < 'a' || *value > 'z') {
250 fprintf(stderr,
251 "invalid label part\n");
252 usage();
253 }
254 partition = *value;
255 break;
256
257 case DTYPE:
258 if (ftype != -1) {
259 fprintf(stderr,
260 "type already specified\n");
261 usage();
262 }
263
264 for (i = 0;
265 i < __arraycount(etfstypes);
266 i++) {
267 if (strcmp(etfstypes[i].name,
268 value) == 0)
269 break;
270 }
271 if (i == __arraycount(etfstypes)) {
272 fprintf(stderr,
273 "invalid type %s\n", value);
274 usage();
275 }
276 ftype = etfstypes[i].type;
277 break;
278
279 default:
280 fprintf(stderr, "invalid dtoken\n");
281 usage();
282 break;
283 }
284 }
285
286 if (key == NULL || hostpath == NULL ||
287 (flen == 0 && partition == 0)) {
288 fprintf(stderr, "incomplete drivespec\n");
289 usage();
290 }
291 if (ftype == -1)
292 ftype = RUMP_ETFS_BLK;
293
294 if (netfs - curetfs == 0) {
295 etfs = realloc(etfs,
296 (netfs+ALLOCCHUNK)*sizeof(*etfs));
297 if (etfs == NULL)
298 die(1, errno, "realloc etfs");
299 netfs += ALLOCCHUNK;
300 }
301
302 etfs[curetfs].key = key;
303 etfs[curetfs].hostpath = hostpath;
304 etfs[curetfs].flen = flen;
305 etfs[curetfs].foffset = foffset;
306 etfs[curetfs].partition = partition;
307 etfs[curetfs].type = ftype;
308 curetfs++;
309
310 break;
311 }
312 case 'l':
313 if (nlibs - curlib == 0) {
314 libarray = realloc(libarray,
315 (nlibs+ALLOCCHUNK) * sizeof(char *));
316 if (libarray == NULL)
317 die(1, errno, "realloc");
318 nlibs += ALLOCCHUNK;
319 }
320 libarray[curlib++] = optarg;
321 break;
322 case 'm':
323 if (nmods - curmod == 0) {
324 modarray = realloc(modarray,
325 (nmods+ALLOCCHUNK) * sizeof(char *));
326 if (modarray == NULL)
327 die(1, errno, "realloc");
328 nmods += ALLOCCHUNK;
329 }
330 modarray[curmod++] = optarg;
331 break;
332 case 'r':
333 setenv("RUMP_MEMLIMIT", optarg, 1);
334 break;
335 case 's':
336 sflag = 1;
337 break;
338 case 'v':
339 setenv("RUMP_VERBOSE", "1", 1);
340 break;
341 default:
342 usage();
343 /*NOTREACHED*/
344 }
345 }
346
347 argc -= optind;
348 argv += optind;
349
350 if (argc != 1)
351 usage();
352
353 /*
354 * Automatically "resolve" component dependencies, i.e.
355 * try to load libs in a loop until all are loaded or a
356 * full loop completes with no loads (latter case is an error).
357 */
358 for (onthepath = 1, nlibs = curlib; onthepath && nlibs > 0;) {
359 onthepath = 0;
360 for (libidx = 0; libidx < curlib; libidx++) {
361 /* loaded already? */
362 if (libarray[libidx] == NULL)
363 continue;
364
365 /* try to load */
366 liblast = libidx;
367 if (dlopen(libarray[libidx],
368 RTLD_LAZY|RTLD_GLOBAL) == NULL) {
369 char pb[MAXPATHLEN];
370 /* try to mimic linker -l syntax */
371 snprintf(pb, sizeof(pb),
372 "lib%s.so", libarray[libidx]);
373 if (dlopen(pb, RTLD_LAZY|RTLD_GLOBAL) == NULL)
374 continue;
375 }
376
377 /* managed to load that one */
378 libarray[libidx] = NULL;
379 nlibs--;
380 onthepath = 1;
381 }
382 }
383 if (nlibs > 0) {
384 fprintf(stderr,
385 "failed to load -libraries, last error from \"%s\":\n",
386 libarray[liblast]);
387 fprintf(stderr, " %s", dlerror());
388 die(1, 0, NULL);
389 }
390 free(libarray);
391
392 serverurl = argv[0];
393
394 if (!sflag) {
395 error = rump_daemonize_begin();
396 if (error)
397 die_rumperr(1, error, "rump daemonize");
398 }
399
400 error = rump_init();
401 if (error)
402 die_rumperr(sflag, error, "rump init failed");
403
404 /* load modules */
405 for (i = 0; i < curmod; i++) {
406 struct rump_modctl_load ml;
407
408 #define ETFSKEY "/module.mod"
409 if ((error = rump_pub_etfs_register(ETFSKEY,
410 modarray[0], RUMP_ETFS_REG)) != 0)
411 die_rumperr(sflag,
412 error, "module etfs register failed");
413 memset(&ml, 0, sizeof(ml));
414 ml.ml_filename = ETFSKEY;
415 /*
416 * XXX: since this is a syscall, error namespace depends
417 * on loaded emulations. revisit and fix.
418 */
419 if (rump_sys_modctl(RUMP_MODCTL_LOAD, &ml) == -1)
420 die(sflag, errno, "module load failed");
421 rump_pub_etfs_remove(ETFSKEY);
422 #undef ETFSKEY
423 }
424 free(modarray);
425
426 /* register host drives */
427 for (i = 0; i < curetfs; i++) {
428 struct stat sb;
429 off_t foffset, flen, fendoff;
430 int fd, oflags;
431
432 oflags = etfs[i].flen == DSIZE_E ? 0 : O_CREAT;
433 fd = open(etfs[i].hostpath, O_RDWR | oflags, 0644);
434 if (fd == -1)
435 die(sflag, errno, "etfs hostpath open");
436
437 if (etfs[i].partition) {
438 processlabel(sflag, fd, etfs[i].partition - 'a',
439 &foffset, &flen);
440 } else {
441 foffset = etfs[i].foffset;
442 flen = etfs[i].flen;
443 }
444
445 if (fstat(fd, &sb) == -1)
446 die(sflag, errno, "fstat etfs hostpath");
447 if (flen == DSIZE_E) {
448 if (sb.st_size == 0)
449 die(sflag, EINVAL, "size=host, but cannot "
450 "query non-zero size");
451 flen = sb.st_size;
452 }
453 fendoff = foffset + flen;
454 if (S_ISREG(sb.st_mode) && sb.st_size < fendoff) {
455 if (ftruncate(fd, fendoff) == -1)
456 die(sflag, errno, "truncate");
457 }
458 close(fd);
459
460 if ((error = rump_pub_etfs_register_withsize(etfs[i].key,
461 etfs[i].hostpath, etfs[i].type, foffset, flen)) != 0)
462 die_rumperr(sflag, error, "etfs register");
463 }
464
465 error = rump_init_server(serverurl);
466 if (error)
467 die_rumperr(sflag, error, "rump server init failed");
468
469 if (!sflag)
470 rump_daemonize_done(RUMP_DAEMONIZE_SUCCESS);
471
472 sem_init(&sigsem, 0, 0);
473 signal(SIGTERM, sigreboot);
474 signal(SIGINT, sigreboot);
475 sem_wait(&sigsem);
476
477 rump_sys_reboot(0, NULL);
478 /*NOTREACHED*/
479
480 return 0;
481 }
482
483 /*
484 * Copyright (c) 1987, 1988, 1993
485 * The Regents of the University of California. All rights reserved.
486 *
487 * Redistribution and use in source and binary forms, with or without
488 * modification, are permitted provided that the following conditions
489 * are met:
490 * 1. Redistributions of source code must retain the above copyright
491 * notice, this list of conditions and the following disclaimer.
492 * 2. Redistributions in binary form must reproduce the above copyright
493 * notice, this list of conditions and the following disclaimer in the
494 * documentation and/or other materials provided with the distribution.
495 * 3. Neither the name of the University nor the names of its contributors
496 * may be used to endorse or promote products derived from this software
497 * without specific prior written permission.
498 *
499 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
500 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
501 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
502 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
503 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
504 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
505 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
506 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
507 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
508 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
509 * SUCH DAMAGE.
510 *
511 * @(#)disklabel.h 8.2 (Berkeley) 7/10/94
512 */
513
514 #define RUMPSERVER_MAXPARTITIONS 22
515 #define RUMPSERVER_DISKMAGIC ((uint32_t)0x82564557) /* magic */
516 #define RUMPSERVER_DEVSHIFT 9
517
518 struct rumpserver_disklabel {
519 uint32_t d_magic; /* the magic number */
520 uint16_t d_type; /* drive type */
521 uint16_t d_subtype; /* controller/d_type specific */
522 char d_typename[16]; /* type name, e.g. "eagle" */
523
524 /*
525 * d_packname contains the pack identifier and is returned when
526 * the disklabel is read off the disk or in-core copy.
527 * d_boot0 and d_boot1 are the (optional) names of the
528 * primary (block 0) and secondary (block 1-15) bootstraps
529 * as found in /usr/mdec. These are returned when using
530 * getdiskbyname(3) to retrieve the values from /etc/disktab.
531 */
532 union {
533 char un_d_packname[16]; /* pack identifier */
534 struct {
535 char *un_d_boot0; /* primary bootstrap name */
536 char *un_d_boot1; /* secondary bootstrap name */
537 } un_b;
538 } d_un;
539 #define d_packname d_un.un_d_packname
540 #define d_boot0 d_un.un_b.un_d_boot0
541 #define d_boot1 d_un.un_b.un_d_boot1
542
543 /* disk geometry: */
544 uint32_t d_secsize; /* # of bytes per sector */
545 uint32_t d_nsectors; /* # of data sectors per track */
546 uint32_t d_ntracks; /* # of tracks per cylinder */
547 uint32_t d_ncylinders; /* # of data cylinders per unit */
548 uint32_t d_secpercyl; /* # of data sectors per cylinder */
549 uint32_t d_secperunit; /* # of data sectors per unit */
550
551 /*
552 * Spares (bad sector replacements) below are not counted in
553 * d_nsectors or d_secpercyl. Spare sectors are assumed to
554 * be physical sectors which occupy space at the end of each
555 * track and/or cylinder.
556 */
557 uint16_t d_sparespertrack; /* # of spare sectors per track */
558 uint16_t d_sparespercyl; /* # of spare sectors per cylinder */
559 /*
560 * Alternative cylinders include maintenance, replacement,
561 * configuration description areas, etc.
562 */
563 uint32_t d_acylinders; /* # of alt. cylinders per unit */
564
565 /* hardware characteristics: */
566 /*
567 * d_interleave, d_trackskew and d_cylskew describe perturbations
568 * in the media format used to compensate for a slow controller.
569 * Interleave is physical sector interleave, set up by the
570 * formatter or controller when formatting. When interleaving is
571 * in use, logically adjacent sectors are not physically
572 * contiguous, but instead are separated by some number of
573 * sectors. It is specified as the ratio of physical sectors
574 * traversed per logical sector. Thus an interleave of 1:1
575 * implies contiguous layout, while 2:1 implies that logical
576 * sector 0 is separated by one sector from logical sector 1.
577 * d_trackskew is the offset of sector 0 on track N relative to
578 * sector 0 on track N-1 on the same cylinder. Finally, d_cylskew
579 * is the offset of sector 0 on cylinder N relative to sector 0
580 * on cylinder N-1.
581 */
582 uint16_t d_rpm; /* rotational speed */
583 uint16_t d_interleave; /* hardware sector interleave */
584 uint16_t d_trackskew; /* sector 0 skew, per track */
585 uint16_t d_cylskew; /* sector 0 skew, per cylinder */
586 uint32_t d_headswitch; /* head switch time, usec */
587 uint32_t d_trkseek; /* track-to-track seek, usec */
588 uint32_t d_flags; /* generic flags */
589 #define NDDATA 5
590 uint32_t d_drivedata[NDDATA]; /* drive-type specific information */
591 #define NSPARE 5
592 uint32_t d_spare[NSPARE]; /* reserved for future use */
593 uint32_t d_magic2; /* the magic number (again) */
594 uint16_t d_checksum; /* xor of data incl. partitions */
595
596 /* filesystem and partition information: */
597 uint16_t d_npartitions; /* number of partitions in following */
598 uint32_t d_bbsize; /* size of boot area at sn0, bytes */
599 uint32_t d_sbsize; /* max size of fs superblock, bytes */
600 struct rumpserver_partition { /* the partition table */
601 uint32_t p_size; /* number of sectors in partition */
602 uint32_t p_offset; /* starting sector */
603 union {
604 uint32_t fsize; /* FFS, ADOS:
605 filesystem basic fragment size */
606 uint32_t cdsession; /* ISO9660: session offset */
607 } __partition_u2;
608 #define p_fsize __partition_u2.fsize
609 #define p_cdsession __partition_u2.cdsession
610 uint8_t p_fstype; /* filesystem type, see below */
611 uint8_t p_frag; /* filesystem fragments per block */
612 union {
613 uint16_t cpg; /* UFS: FS cylinders per group */
614 uint16_t sgs; /* LFS: FS segment shift */
615 } __partition_u1;
616 #define p_cpg __partition_u1.cpg
617 #define p_sgs __partition_u1.sgs
618 } d_partitions[RUMPSERVER_MAXPARTITIONS]; /* actually may be more */
619 };
620
621
622 /* for swapping disklabel, so don't care about perf, just portability */
623 #define bs32(x) \
624 ((((x) & 0xff000000) >> 24)| \
625 (((x) & 0x00ff0000) >> 8) | \
626 (((x) & 0x0000ff00) << 8) | \
627 (((x) & 0x000000ff) << 24))
628 #define bs16(x) ((((x) & 0xff00) >> 8) | (((x) & 0x00ff) << 8))
629
630 /*
631 * From:
632 * $NetBSD: disklabel_dkcksum.c,v 1.4 2005/05/15 21:01:34 thorpej Exp
633 */
634
635 /*-
636 * Copyright (c) 1991, 1993
637 * The Regents of the University of California. All rights reserved.
638 *
639 * Redistribution and use in source and binary forms, with or without
640 * modification, are permitted provided that the following conditions
641 * are met:
642 * 1. Redistributions of source code must retain the above copyright
643 * notice, this list of conditions and the following disclaimer.
644 * 2. Redistributions in binary form must reproduce the above copyright
645 * notice, this list of conditions and the following disclaimer in the
646 * documentation and/or other materials provided with the distribution.
647 * 3. Neither the name of the University nor the names of its contributors
648 * may be used to endorse or promote products derived from this software
649 * without specific prior written permission.
650 *
651 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
652 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
653 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
654 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
655 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
656 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
657 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
658 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
659 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
660 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
661 * SUCH DAMAGE.
662 */
663
664 static uint16_t
665 rs_dl_dkcksum(struct rumpserver_disklabel *lp, int imswapped)
666 {
667 uint16_t *start, *end;
668 uint16_t sum;
669 uint16_t npart;
670
671 if (imswapped)
672 npart = bs16(lp->d_npartitions);
673 else
674 npart = lp->d_npartitions;
675
676 sum = 0;
677 start = (uint16_t *)(void *)lp;
678 end = (uint16_t *)(void *)&lp->d_partitions[npart];
679 while (start < end) {
680 if (imswapped)
681 sum ^= bs16(*start);
682 else
683 sum ^= *start;
684 start++;
685 }
686 return (sum);
687 }
688
689 /*
690 * From:
691 * NetBSD: disklabel_scan.c,v 1.3 2009/01/18 12:13:03 lukem Exp
692 */
693
694 /*-
695 * Copyright (c) 2002 The NetBSD Foundation, Inc.
696 * All rights reserved.
697 *
698 * This code is derived from software contributed to The NetBSD Foundation
699 * by Roland C. Dowdeswell.
700 *
701 * Redistribution and use in source and binary forms, with or without
702 * modification, are permitted provided that the following conditions
703 * are met:
704 * 1. Redistributions of source code must retain the above copyright
705 * notice, this list of conditions and the following disclaimer.
706 * 2. Redistributions in binary form must reproduce the above copyright
707 * notice, this list of conditions and the following disclaimer in the
708 * documentation and/or other materials provided with the distribution.
709 *
710 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
711 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
712 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
713 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
714 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
715 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
716 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
717 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
718 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
719 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
720 * POSSIBILITY OF SUCH DAMAGE.
721 */
722
723 static int
724 rs_dl_scan(struct rumpserver_disklabel *lp, int *isswapped,
725 char *buf, size_t buflen)
726 {
727 size_t i;
728 int imswapped;
729 uint16_t npart;
730
731 /* scan for the correct magic numbers. */
732
733 for (i=0; i <= buflen - sizeof(*lp); i += 4) {
734 memcpy(lp, buf + i, sizeof(*lp));
735 if (lp->d_magic == RUMPSERVER_DISKMAGIC &&
736 lp->d_magic2 == RUMPSERVER_DISKMAGIC) {
737 imswapped = 0;
738 goto sanity;
739 }
740 if (lp->d_magic == bs32(RUMPSERVER_DISKMAGIC) &&
741 lp->d_magic2 == bs32(RUMPSERVER_DISKMAGIC)) {
742 imswapped = 1;
743 goto sanity;
744 }
745 }
746
747 return 1;
748
749 sanity:
750 if (imswapped)
751 npart = bs16(lp->d_npartitions);
752 else
753 npart = lp->d_npartitions;
754 /* we've found something, let's sanity check it */
755 if (npart > RUMPSERVER_MAXPARTITIONS
756 || rs_dl_dkcksum(lp, imswapped))
757 return 1;
758
759 *isswapped = imswapped;
760 return 0;
761 }
762
763 static void
764 processlabel(int sflag, int fd, int partition, off_t *foffp, off_t *flenp)
765 {
766 struct rumpserver_disklabel dl;
767 char buf[1<<16];
768 uint32_t foffset, flen;
769 int imswapped;
770
771 if (pread(fd, buf, sizeof(buf), 0) == -1)
772 die(sflag, errno, "could not read disk device");
773 if (rs_dl_scan(&dl, &imswapped, buf, sizeof(buf)))
774 die(sflag, ENOENT, "disklabel not found");
775
776 if (partition >= dl.d_npartitions)
777 die(sflag, ENOENT, "partition not available");
778
779 foffset = dl.d_partitions[partition].p_offset << RUMPSERVER_DEVSHIFT;
780 flen = dl.d_partitions[partition].p_size << RUMPSERVER_DEVSHIFT;
781 if (imswapped) {
782 foffset = bs32(foffset);
783 flen = bs32(flen);
784 }
785
786 *foffp = (off_t)foffset;
787 *flenp = (off_t)flen;
788 }
789