GC !__DragonFly__ section.
[dragonfly.git] / sbin / vinum / v.c
CommitLineData
984263bc
MD
1/* vinum.c: vinum interface program */
2/*-
3 * Copyright (c) 1997, 1998
4 * Nan Yang Computer Services Limited. All rights reserved.
5 *
6 * Written by Greg Lehey
7 *
8 * This software is distributed under the so-called ``Berkeley
9 * License'':
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by Nan Yang Computer
22 * Services Limited.
23 * 4. Neither the name of the Company nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * This software is provided ``as is'', and any express or implied
28 * warranties, including, but not limited to, the implied warranties of
29 * merchantability and fitness for a particular purpose are disclaimed.
30 * In no event shall the company or contributors be liable for any
31 * direct, indirect, incidental, special, exemplary, or consequential
32 * damages (including, but not limited to, procurement of substitute
33 * goods or services; loss of use, data, or profits; or business
34 * interruption) however caused and on any theory of liability, whether
35 * in contract, strict liability, or tort (including negligence or
36 * otherwise) arising in any way out of the use of this software, even if
37 * advised of the possibility of such damage.
38 *
39 * $Id: v.c,v 1.31 2000/09/03 01:29:26 grog Exp grog $
40 * $FreeBSD: src/sbin/vinum/v.c,v 1.26.2.3 2001/03/13 03:04:06 grog Exp $
38a690d7 41 * $DragonFly: src/sbin/vinum/v.c,v 1.3 2003/08/08 04:18:41 dillon Exp $
984263bc
MD
42 */
43
44#include <ctype.h>
45#include <errno.h>
46#include <fcntl.h>
47#include <sys/mman.h>
48#include <netdb.h>
49#include <setjmp.h>
50#include <signal.h>
51#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
54#include <syslog.h>
55#include <unistd.h>
56#include <sys/ioctl.h>
38a690d7 57#include <dev/raid/vinum/vinumhdr.h>
984263bc
MD
58#include "vext.h"
59#include <sys/types.h>
60#include <sys/wait.h>
61#include <readline/history.h>
62#include <readline/readline.h>
63#include <sys/linker.h>
64#include <sys/module.h>
65#include <sys/resource.h>
66
67FILE *cf; /* config file handle */
68FILE *history; /* history file */
69char *historyfile; /* and its name */
70
71char *dateformat; /* format in which to store date */
72
73char buffer[BUFSIZE]; /* buffer to read in to */
74
75int line = 0; /* stdin line number for error messages */
76int file_line = 0; /* and line in input file (yes, this is tacky) */
77int inerror; /* set to 1 to exit after end of config file */
78
79/* flags */
80
81#if VINUMDEBUG
82int debug = 0; /* debug flag, usage varies */
83#endif
84int force = 0; /* set to 1 to force some dangerous ops */
85int interval = 0; /* interval in ms between init/revive */
86int vflag = 0; /* set verbose operation or verify */
87int Verbose = 0; /* set very verbose operation */
88int recurse = 0; /* set recursion */
89int sflag = 0; /* show statistics */
90int SSize = 0; /* sector size for revive */
91int dowait = 0; /* wait for completion */
92char *objectname; /* name to be passed for -n flag */
93
94/* Structures to read kernel data into */
95struct _vinum_conf vinum_conf; /* configuration information */
96
97struct volume vol;
98struct plex plex;
99struct sd sd;
100struct drive drive;
101
102jmp_buf command_fail; /* return on a failed command */
103int superdev; /* vinum super device */
104
105void start_daemon(void);
106
107#define ofs(x) ((void *) (& ((struct confdata *) 0)->x)) /* offset of x in struct confdata */
108
109char *token[MAXARGS]; /* pointers to individual tokens */
110int tokens; /* number of tokens */
111
112int
113main(int argc, char *argv[], char *envp[])
114{
115 struct stat histstat;
116
117 if (modfind(VINUMMOD) < 0) {
118 /* need to load the vinum module */
119 if (kldload(VINUMMOD) < 0 || modfind(VINUMMOD) < 0) {
120 perror(VINUMMOD ": Kernel module not available");
121 return 1;
122 }
123 }
124 dateformat = getenv("VINUM_DATEFORMAT");
125 if (dateformat == NULL)
126 dateformat = "%e %b %Y %H:%M:%S";
127 historyfile = getenv("VINUM_HISTORY");
128 if (historyfile == NULL)
129 historyfile = DEFAULT_HISTORYFILE;
130 if (stat(historyfile, &histstat) == 0) { /* history file exists */
131 if ((histstat.st_mode & S_IFMT) != S_IFREG) {
132 fprintf(stderr,
133 "Vinum history file %s must be a regular file\n",
134 historyfile);
135 exit(1);
136 }
137 } else if ((errno != ENOENT) /* not "not there", */
138 &&(errno != EROFS)) { /* and not read-only file system */
139 fprintf(stderr,
140 "Can't open %s: %s (%d)\n",
141 historyfile,
142 strerror(errno),
143 errno);
144 exit(1);
145 }
146 history = fopen(historyfile, "a+");
147 if (history != NULL) {
148 timestamp();
149 fprintf(history, "*** " VINUMMOD " started ***\n");
150 fflush(history); /* before we start the daemon */
151 }
152 superdev = open(VINUM_SUPERDEV_NAME, O_RDWR); /* open vinum superdevice */
153 if (superdev < 0) { /* no go */
154 if (errno == ENODEV) { /* not configured, */
155 superdev = open(VINUM_WRONGSUPERDEV_NAME, O_RDWR); /* do we have a debug mismatch? */
156 if (superdev >= 0) { /* yup! */
157#if VINUMDEBUG
158 fprintf(stderr,
159 "This program is compiled with debug support, but the kernel module does\n"
160 "not have debug support. This program must be matched with the kernel\n"
161 "module. Please alter /usr/src/sbin/" VINUMMOD "/Makefile and remove\n"
162 "the option -DVINUMDEBUG from the CFLAGS definition, or alternatively\n"
163 "edit /usr/src/sys/modules/" VINUMMOD "/Makefile and add the option\n"
164 "-DVINUMDEBUG to the CFLAGS definition. Then rebuild the component\n"
165 "of your choice with 'make clean all install'. If you rebuild the kernel\n"
166 "module, you must stop " VINUMMOD " and restart it\n");
167#else
168 fprintf(stderr,
169 "This program is compiled without debug support, but the kernel module\n"
170 "includes debug support. This program must be matched with the kernel\n"
171 "module. Please alter /usr/src/sbin/" VINUMMOD "/Makefile and add\n"
172 "the option -DVINUMDEBUG to the CFLAGS definition, or alternatively\n"
173 "edit /usr/src/sys/modules/" VINUMMOD "/Makefile and remove the option\n"
174 "-DVINUMDEBUG from the CFLAGS definition. Then rebuild the component\n"
175 "of your choice with 'make clean all install'. If you rebuild the kernel\n"
176 "module, you must stop " VINUMMOD " and restart it\n");
177#endif
178 return 1;
179 }
180 } else if (errno == ENOENT) /* we don't have our node, */
181 make_devices(); /* create them first */
182 if (superdev < 0) {
183 perror("Can't open " VINUM_SUPERDEV_NAME);
184 return 1;
185 }
186 }
187