Annotation of embedaddon/libpdel/config/app_config_directory.c, revision 1.1.1.1

1.1       misho       1: 
                      2: /*
                      3:  * Copyright (c) 2001-2002 Packet Design, LLC.
                      4:  * All rights reserved.
                      5:  * 
                      6:  * Subject to the following obligations and disclaimer of warranty,
                      7:  * use and redistribution of this software, in source or object code
                      8:  * forms, with or without modifications are expressly permitted by
                      9:  * Packet Design; provided, however, that:
                     10:  * 
                     11:  *    (i)  Any and all reproductions of the source or object code
                     12:  *         must include the copyright notice above and the following
                     13:  *         disclaimer of warranties; and
                     14:  *    (ii) No rights are granted, in any manner or form, to use
                     15:  *         Packet Design trademarks, including the mark "PACKET DESIGN"
                     16:  *         on advertising, endorsements, or otherwise except as such
                     17:  *         appears in the above copyright notice or in the software.
                     18:  * 
                     19:  * THIS SOFTWARE IS BEING PROVIDED BY PACKET DESIGN "AS IS", AND
                     20:  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, PACKET DESIGN MAKES NO
                     21:  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING
                     22:  * THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED
                     23:  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
                     24:  * OR NON-INFRINGEMENT.  PACKET DESIGN DOES NOT WARRANT, GUARANTEE,
                     25:  * OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS
                     26:  * OF THE USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY,
                     27:  * RELIABILITY OR OTHERWISE.  IN NO EVENT SHALL PACKET DESIGN BE
                     28:  * LIABLE FOR ANY DAMAGES RESULTING FROM OR ARISING OUT OF ANY USE
                     29:  * OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY DIRECT,
                     30:  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE, OR CONSEQUENTIAL
                     31:  * DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, LOSS OF
                     32:  * USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY THEORY OF
                     33:  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     34:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
                     35:  * THE USE OF THIS SOFTWARE, EVEN IF PACKET DESIGN IS ADVISED OF
                     36:  * THE POSSIBILITY OF SUCH DAMAGE.
                     37:  *
                     38:  * Author: Archie Cobbs <archie@freebsd.org>
                     39:  */
                     40: 
                     41: #include <sys/types.h>
                     42: 
                     43: #include <netinet/in.h>
                     44: 
                     45: #include <stdio.h>
                     46: #include <assert.h>
                     47: #include <syslog.h>
                     48: #include <unistd.h>
                     49: #include <stdarg.h>
                     50: 
                     51: #include "structs/structs.h"
                     52: #include "structs/type/array.h"
                     53: #include "util/typed_mem.h"
                     54: #include "config/app_config.h"
                     55: #include "sys/alog.h"
                     56: 
                     57: /************************************************************************
                     58:                        DIRECTORY SUBSYSTEM
                     59: ************************************************************************/
                     60: 
                     61: static app_ss_startup_t                app_directory_start;
                     62: static app_ss_willrun_t                app_directory_willrun;
                     63: static app_ss_changed_t                app_directory_changed;
                     64: 
                     65: const struct app_subsystem     app_config_directory_subsystem = {
                     66:        "directory",
                     67:        NULL,
                     68:        app_directory_start,
                     69:        NULL,
                     70:        app_directory_willrun,
                     71:        app_directory_changed,
                     72:        NULL
                     73: };
                     74: 
                     75: /*
                     76:  * Directory startup
                     77:  */
                     78: static int
                     79: app_directory_start(struct app_config_ctx *ctx,
                     80:        const struct app_subsystem *ss, const void *config)
                     81: {
                     82:        const char *const name = ss->arg;
                     83:        char *dir;
                     84:        int r;
                     85: 
                     86:        /* Get directory */
                     87:        if ((dir = structs_get_string(app_config_get_type(ctx),
                     88:            name, config, TYPED_MEM_TEMP)) == NULL) {
                     89:                alogf(LOG_ERR, "%s: %m", "structs_get_string");
                     90:                return (-1);
                     91:        }
                     92: 
                     93:        /* Change into it */
                     94:        if ((r = chdir(dir)) == -1)
                     95:                alog(LOG_ERR, "can't change to directory \"%s\": %m", dir);
                     96:        FREE(TYPED_MEM_TEMP, dir);
                     97:        return (r);
                     98: }
                     99: 
                    100: /*
                    101:  * Directory necessity check
                    102:  */
                    103: static int
                    104: app_directory_willrun(struct app_config_ctx *ctx,
                    105:        const struct app_subsystem *ss, const void *config)
                    106: {
                    107:        const char *const name = ss->arg;
                    108:        char *dir;
                    109:        int r;
                    110: 
                    111:        /* Get directory */
                    112:        assert(name != NULL);
                    113:        if ((dir = structs_get_string(app_config_get_type(ctx),
                    114:            name, config, TYPED_MEM_TEMP)) == NULL) {
                    115:                alogf(LOG_ERR, "%s: %m", "structs_get_string");
                    116:                return (-1);
                    117:        }
                    118: 
                    119:        /* Check if not NULL */
                    120:        r = (*dir != '\0');
                    121:        FREE(TYPED_MEM_TEMP, dir);
                    122:        return (r);
                    123: }
                    124: 
                    125: /*
                    126:  * Directory configuration changed check
                    127:  */
                    128: static int
                    129: app_directory_changed(struct app_config_ctx *ctx,
                    130:        const struct app_subsystem *ss, const void *config1,
                    131:        const void *config2)
                    132: {
                    133:        const char *const name = ss->arg;
                    134:        int ret;
                    135: 
                    136:        /* Compare directory names */
                    137:        if ((ret = structs_equal(app_config_get_type(ctx),
                    138:            name, config1, config2)) == -1) {
                    139:                alogf(LOG_ERR, "%s: %m", "structs_equal");
                    140:                return (-1);
                    141:        }
                    142:        return (!ret);
                    143: }
                    144: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>