[BACK]Return to checkrc.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / distrib / utils / sysinst

Annotation of src/distrib/utils/sysinst/checkrc.c, Revision 1.2

1.2     ! jdf         1: /* $NetBSD: checkrc.c,v 1.1 2012/04/06 23:48:53 riz Exp $ */
1.1       riz         2:
                      3: /*-
                      4:  * Copyright (c) 2012 The NetBSD Foundation, Inc.
                      5:  * All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to The NetBSD Foundation
                      8:  * by Jeffrey C. Rizzo
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     20:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     21:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     22:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     23:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     24:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     25:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     26:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     27:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     28:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     29:  * POSSIBILITY OF SUCH DAMAGE.
                     30:  */
                     31:
                     32: /* checkrc.c -- Create a script on the target to check the state of
                     33:  * its rc.conf variables. */
                     34:
                     35: #include <curses.h>
                     36: #include <err.h>
                     37: #include <stdio.h>
                     38: #include "defs.h"
                     39: #include "msg_defs.h"
                     40: #include "menu_defs.h"
                     41:
                     42: #define RC_CHECK_SCRIPT "/tmp/checkrc.sh"
                     43:
                     44: static int create_script(const char *, int);
                     45: static int check(const char *, int);
                     46:
                     47: char *rcconf = NULL;
                     48:
                     49: enum {
                     50:        CHECK_CONF,
                     51:        CHECK_DEFAULT
                     52: };
                     53:
                     54: static int
                     55: create_script(const char *varname, int filetocheck)
                     56: {
                     57:        FILE    *fp;
                     58:
                     59:        if ((fp = fopen(target_expand(RC_CHECK_SCRIPT), "w")) == NULL) {
                     60:                if (logfp)
                     61:                        fprintf(logfp,"Could not open %s for writing",
                     62:                            target_expand(RC_CHECK_SCRIPT));
                     63:                warn("Could not open %s for writing",
                     64:                    target_expand(RC_CHECK_SCRIPT));
                     65:                return 1;
                     66:        }
                     67:
                     68:        if (filetocheck == CHECK_DEFAULT)
                     69:                fprintf(fp, "#!/bin/sh\n. /etc/defaults/rc.conf\n"
                     70:                    ". /etc/rc.subr\n");
                     71:        else
                     72:                fprintf(fp, "#!/bin/sh\n. /etc/rc.conf\n. /etc/rc.subr\n");
                     73:        fprintf(fp, "if checkyesno %s\nthen\necho YES\nelse\necho NO\nfi\n",
                     74:            varname);
                     75:
                     76:        fclose(fp);
                     77:        return 0;
                     78: }
                     79:
                     80: static int
                     81: check(const char *varname, int filetocheck)
                     82: {
                     83:        char *buf;
                     84:
                     85:        create_script(varname, filetocheck);
                     86:
1.2     ! jdf        87:        if (target_already_root())
        !            88:                collect(T_OUTPUT, &buf, "/bin/sh %s 2>&1", RC_CHECK_SCRIPT);
        !            89:        else
        !            90:                collect(T_OUTPUT, &buf, "chroot %s /bin/sh %s 2>&1",
        !            91:                                target_prefix(), RC_CHECK_SCRIPT);
        !            92:
1.1       riz        93:
                     94:        unlink(target_expand(RC_CHECK_SCRIPT));
                     95:
                     96:        if (logfp) {
                     97:                fprintf(logfp,"var %s is %s\n", varname, buf);
                     98:                fflush(logfp);
                     99:        }
                    100:
                    101:        if (strncmp(buf, "YES", strlen("YES")) == 0)
                    102:                return 1;
                    103:        else
                    104:                return 0;
                    105: }
                    106:
                    107: int
                    108: check_rcvar(const char *varname)
                    109: {
                    110:        return check(varname, CHECK_CONF);
                    111: }
                    112:
                    113: int
                    114: check_rcdefault(const char *varname)
                    115: {
                    116:        return check(varname, CHECK_DEFAULT);
                    117: }

CVSweb <webmaster@jp.NetBSD.org>