diff options
| author | Dan McDonald <danmcd@joyent.com> | 2021-11-23 14:12:50 -0500 |
|---|---|---|
| committer | Dan McDonald <danmcd@joyent.com> | 2021-11-23 14:12:50 -0500 |
| commit | 97a17035ae6224196e076e91ecbffa8498786fbf (patch) | |
| tree | 05d66869ee153ea902e064d7261847e3f9ec4816 /usr/src/common | |
| parent | 99582f15dd8ddfce645cd6baf0e92e6599fe4c7b (diff) | |
| parent | 6f443ebc1fb4fec01d6e8fa8ca4648182ed215bb (diff) | |
| download | illumos-joyent-97a17035ae6224196e076e91ecbffa8498786fbf.tar.gz | |
[illumos-gate merge]
commit 6f443ebc1fb4fec01d6e8fa8ca4648182ed215bb
13689 Want AWS ENA driver
commit a28480febf31f0e61debac062a55216a98a05a92
14208 zoneadmd should not inherit zoneadm's environment
commit b0de25cb23668fa4535078d18a0618eee442c000
14081 bhyve upstream sync 2021 September
commit 0554d5ecd11d9644cbb915be31b5a0b7abb40122
14229 secflags_dts relies on compiler defaults
commit 96bb5f330b9b75a0f74766485ffaf400a063a668
14256 package inc files should be pkgfmt v2 as well
Conflicts:
manifest
usr/src/cmd/bhyve/Makefile
usr/src/lib/libzonecfg/Makefile.com
usr/src/lib/libzonecfg/common/libzonecfg.c
usr/src/pkg/manifests/system-kernel.man9f.inc
usr/src/pkg/manifests/system-test-ostest.p5m
usr/src/test/os-tests/tests/secflags/secflags_dts.sh
usr/src/uts/intel/os/driver_aliases
usr/src/uts/intel/os/name_to_major
Diffstat (limited to 'usr/src/common')
| -rw-r--r-- | usr/src/common/definit/definit.c | 188 | ||||
| -rw-r--r-- | usr/src/common/definit/definit.h | 47 |
2 files changed, 235 insertions, 0 deletions
diff --git a/usr/src/common/definit/definit.c b/usr/src/common/definit/definit.c new file mode 100644 index 0000000000..56d77f871d --- /dev/null +++ b/usr/src/common/definit/definit.c @@ -0,0 +1,188 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License (the "License"). + * You may not use this file except in compliance with the License. + * + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE + * or http://www.opensolaris.org/os/licensing. + * See the License for the specific language governing permissions + * and limitations under the License. + * + * When distributing Covered Code, include this CDDL HEADER in each + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. + * If applicable, add the following below this CDDL HEADER, with the + * fields enclosed by brackets "[]" replaced with your own identifying + * information: Portions Copyright [yyyy] [name of copyright owner] + * + * CDDL HEADER END + */ +/* + * Copyright 2009 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + * + * Copyright 2021 OmniOS Community Edition (OmniOSce) Association. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <sys/types.h> +#include <definit.h> + +/* Tokens are separated by spaces, tabs and newlines. */ +#define SEPARATORS " \t\n" + +typedef struct definit { + FILE *di_fp; + char *di_line; + char *di_tok; +} definit_t; + +int +definit_open(const char *file, void **statep) +{ + FILE *fp; + int _errno; + definit_t *state = NULL; + + if ((fp = fopen(file, "r")) == NULL) + return (-1); + + if ((state = calloc(1, sizeof (*state))) == NULL) + goto err; + + if ((state->di_line = calloc(DEFINIT_MAXLINE, sizeof (char))) == NULL) + goto err; + + state->di_fp = fp; + *statep = state; + + return (0); + +err: + _errno = errno; + (void) fclose(fp); + if (state != NULL) { + free(state->di_line); + free(state); + } + errno = _errno; + return (-1); +} + +void +definit_close(void *statep) +{ + definit_t *state = statep; + + (void) fclose(state->di_fp); + free(state->di_line); + free(state); +} + +/* + * This parser was written to produce the same output as the ones it replaced + * in init and svc.startd. As such it has some shortcomings: + * - Values may be quoted but the quotes are just stripped and separators such + * as whitespace are not treated specially within quotes; + * - Lines which are longer than DEFINIT_MAXLINE -1 bytes are split. Tokens + * which span a split will be truncated, one way or another. + * - Comments at the end of a line (after a token) are not supported. + * These could be corrected in the future if strict backwards compatibility is + * not required. + */ + +static char * +definit_nextline(definit_t *state) +{ + char *line; + + while ((line = fgets(state->di_line, DEFINIT_MAXLINE, state->di_fp)) + != NULL) { + boolean_t inquotes; + char *p, *bp; + size_t wslength; + + /* + * Ignore blank or comment lines. + */ + if (line[0] == '#' || line[0] == '\0' || + (wslength = strspn(line, SEPARATORS)) == strlen(line) || + line[wslength] == '#') { + continue; + } + + /* + * Make a pass through the line and: + * - Replace any non-quoted semicolons with spaces; + * - Remove any quote characters. + * + * While walking this, 'p' is the current position in the line + * and, if any characters have been found which need to be + * removed, 'bp' tracks the position in the line where + * subsequent characters need to be written in order to close + * the gap; 'bp' trails 'p'. + * If 'bp' is NULL, no characters to remove have been found. + */ + inquotes = B_FALSE; + for (p = line, bp = NULL; *p != '\0'; p++) { + switch (*p) { + case '"': + case '\'': + inquotes = !inquotes; + if (bp == NULL) + bp = p; + break; + case ';': + if (!inquotes) + *p = ' '; + /* FALLTHROUGH */ + default: + if (bp != NULL) + *bp++ = *p; + break; + } + } + if (bp != NULL) + *bp = '\0'; + + /* + * Perform an initial strtok_r() call on the new line. + * definit_token() will repeatedly call strtok_r() until the + * line is consumed, and then call this function again for + * more input. + */ + if ((p = strtok_r(line, SEPARATORS, &state->di_tok)) != NULL) + return (p); + } + + return (NULL); +} + +const char * +definit_token(void *statep) +{ + definit_t *state = statep; + char *tok; + + for (;;) { + tok = NULL; + + if (state->di_tok != NULL) + tok = strtok_r(NULL, SEPARATORS, &state->di_tok); + + if (tok == NULL) + tok = definit_nextline(state); + + if (tok == NULL) + break; + + if (strchr(tok, '=') != NULL && *tok != '=') + return (tok); + } + + return (NULL); +} diff --git a/usr/src/common/definit/definit.h b/usr/src/common/definit/definit.h new file mode 100644 index 0000000000..24338220de --- /dev/null +++ b/usr/src/common/definit/definit.h @@ -0,0 +1,47 @@ +/* + * This file and its contents are supplied under the terms of the + * Common Development and Distribution License ("CDDL"), version 1.0. + * You may only use this file in accordance with the terms of version + * 1.0 of the CDDL. + * + * A full copy of the text of the CDDL should have accompanied this + * source. A copy of the CDDL is also available via the Internet at + * http://www.illumos.org/license/CDDL. + */ + +/* + * Copyright 2021 OmniOS Community Edition (OmniOSce) Association. + */ + +#ifndef _DEFINIT_H +#define _DEFINIT_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Routines for parsing the default init file, /etc/default/init. + * Used by init, svc.startd and libzonecfg for setting up a default + * environment. + * + * After calling definit_open(), callers should call definit_token() in a loop + * until it returns NULL, indicating that all tokens in the file have been + * processed. To clean up when finished, call definit_close(). + */ + +#define DEFINIT_DEFAULT_FILE "/etc/default/init" +#define DEFINIT_MAXLINE 512 + +#define DEFINIT_MIN_UMASK 0 +#define DEFINIT_MAX_UMASK 077 + +int definit_open(const char *, void **); +void definit_close(void *); +const char *definit_token(void *); + +#ifdef __cplusplus +} +#endif + +#endif /* !_DEFINIT_H */ |
