Imported Upstream version 2.5.0
[debian/amanda] / contrib / gsc / ucfggsc.c
1 /*
2  * %W% %G%
3  * $Id: ucfggsc.c,v 1.1 2001/04/15 11:12:37 ant Exp $
4  * Copyright (c) 1997 by Matthew Jacob
5  *
6  *      This software is free software; you can redistribute it and/or
7  *      modify it under the terms of the GNU Library General Public
8  *      License as published by the Free Software Foundation; version 2.
9  *
10  *      This software is distributed in the hope that it will be useful,
11  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *      Library General Public License for more details.
14  *
15  *      You should have received a copy of the GNU Library General Public
16  *      License along with this software; if not, write to the Free
17  *      Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  *      The author may be reached via electronic communications at
20  *
21  *              mjacob@feral.com
22  *
23  *      or, via United States Postal Address
24  *
25  *              Matthew Jacob
26  *              1831 Castro Street
27  *              San Francisco, CA, 94131
28  */
29
30 #include <stdio.h>
31 #include <sys/types.h>
32 #include <errno.h>
33 #include <sys/cfgdb.h>
34 #include <sys/cfgodm.h>
35 #include <cf.h>
36 #include <sys/sysconfig.h>
37 #include <sys/sysmacros.h>
38 #include <sys/device.h>
39
40 #include "gscdds.h"
41
42 #define vprintf if (verbose) printf
43
44 extern mid_t loadext(char *, int, int);
45
46 static void
47 err_exit(char exitcode)
48 {
49     odm_close_class(CuDv_CLASS);
50     odm_close_class(PdDv_CLASS);
51     odm_terminate();
52     exit(exitcode);
53 }
54
55 int
56 main(int argc, char **argv)
57 {
58     char *logical_name, *ptr;
59     char sstring[256];
60     struct CuDv cudvobj;
61     struct PdDv pddvobj;
62     int rc, how_many, errflg, c, majorno, minorno, unit, verbose;
63     struct cfg_dd cfg;
64     struct cfg_load load;
65     int *minor_list;
66     extern int optind;
67     extern char *optarg;
68
69     verbose = errflg = 0;
70     logical_name = NULL;
71     while ((c = getopt(argc,argv,"vl:")) != EOF) {
72         switch (c) {
73         case 'v':
74             verbose++;
75             break;
76         case 'l':
77             if (logical_name != NULL)
78                 errflg++;
79             logical_name = optarg;
80             break;
81         default:
82             errflg++;
83         }
84     }
85     if (errflg)
86         exit(E_ARGS);
87
88     if (logical_name == NULL)
89         exit(E_LNAME);
90
91     if (odm_initialize() == -1)
92         exit(E_ODMINIT);
93
94     /* Get Customized Device Object for this device */
95     sprintf(sstring,"name = '%s'",logical_name);
96     rc = (int) odm_get_first(CuDv_CLASS, sstring, &cudvobj);
97     if (rc ==  0) {
98         err_exit(E_NOCuDv);
99     } else if (rc == -1) {
100         err_exit(E_ODMGET);
101     }
102
103     if (cudvobj.status == DEFINED)
104         err_exit(E_OK);  /* already unconf'd */
105
106     /* get device's predefined object */
107     sprintf(sstring,"uniquetype = '%s'", cudvobj.PdDvLn_Lvalue);
108     rc = (int) odm_get_first(PdDv_CLASS, sstring, &pddvobj);
109     if (rc ==  0)
110         err_exit(E_NOPdDv);
111     else if (rc == -1)
112         err_exit(E_ODMGET);
113
114     /*
115      * Call sysconfig() to "terminate" the device.
116      * If fails with EBUSY, then device instance is "open",
117      * and device cannot be "unconfigured".  Any other errno
118      * returned will be ignored since we MUST unconfigure the
119      * device even if it reports some other error.
120      */
121
122     /* get major number of device */
123     majorno = genmajor(pddvobj.DvDr);
124     if (majorno == -1) {
125         return(E_MAJORNO);
126     }
127     /* get minor number */
128     minor_list = getminor(majorno, &how_many, pddvobj.DvDr);
129     if (minor_list == NULL || how_many == 0)
130         err_exit (E_MINORNO);
131     vprintf("how_many=%d\n", how_many);
132     ptr = logical_name;
133     ptr += strlen(pddvobj.prefix);
134     unit = atoi(ptr);
135     if (unit >= how_many) {
136         err_exit (E_MINORNO);
137     }
138     minorno = minor_list[unit];
139     vprintf("unit %d minorno %d\n", unit, minorno);
140
141     /* create devno for this device */
142     cfg.devno = makedev(majorno, minorno);
143     cfg.kmid = 0;
144     cfg.ddsptr = (caddr_t) NULL;
145     cfg.ddslen = (int) 0;
146     cfg.cmd = CFG_TERM;
147     if (sysconfig(SYS_CFGDD, &cfg, sizeof(struct cfg_dd)) == -1) {
148         if (errno == EBUSY)
149             err_exit(E_BUSY);
150     }
151     cfg.kmid = loadext(pddvobj.DvDr, FALSE, FALSE);
152     if (cfg.kmid == NULL)
153         err_exit(E_UNLOADEXT);
154
155     /* Change the status field of device to "DEFINED" */
156     cudvobj.status = DEFINED;
157     if (odm_change_obj(CuDv_CLASS, &cudvobj) == -1) 
158         err_exit(E_ODMUPDATE);
159
160     /*
161      * Terminate ODM
162      */
163     odm_terminate();
164     return (E_OK);
165 }
166 /*
167  * Overrides for Emacs so that we follow Linus's tabbing style.
168  * Emacs will notice this stuff at the end of the file and automatically
169  * adjust the settings for this buffer only.  This must remain at the end
170  * of the file.
171  * ---------------------------------------------------------------------------
172  * Local variables:
173  * c-indent-level: 4
174  * c-brace-imaginary-offset: 0
175  * c-brace-offset: -4
176  * c-argdecl-indent: 4
177  * c-label-offset: -4
178  * c-continued-statement-offset: 4
179  * c-continued-brace-offset: 0
180  * End:
181  */