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