Index: head/sbin/devd/parse.y =================================================================== --- head/sbin/devd/parse.y (revision 235788) +++ head/sbin/devd/parse.y (revision 235789) @@ -1,152 +1,153 @@ %{ /*- * DEVD (Device action daemon) * * Copyright (c) 2002 M. Warner Losh . * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ +#include #include "devd.h" #include #include %} %union { char *str; int i; struct eps *eps; /* EventProcStatement */ struct event_proc *eventproc; } %token SEMICOLON BEGINBLOCK ENDBLOCK COMMA %token NUMBER %token STRING %token ID %token OPTIONS SET DIRECTORY PID_FILE DEVICE_NAME ACTION MATCH %token ATTACH DETACH NOMATCH NOTIFY MEDIA_TYPE CLASS SUBDEVICE %type match_or_action_list %type match_or_action match action %% config_file : config_list | ; config_list : config | config_list config ; config : option_block | attach_block | detach_block | nomatch_block | notify_block ; option_block : OPTIONS BEGINBLOCK options ENDBLOCK SEMICOLON ; options : option | options option option : directory_option | pid_file_option | set_option ; directory_option : DIRECTORY STRING SEMICOLON { add_directory($2); } ; pid_file_option : PID_FILE STRING SEMICOLON { set_pidfile($2); } ; set_option : SET ID STRING SEMICOLON { set_variable($2, $3); } ; attach_block : ATTACH NUMBER BEGINBLOCK match_or_action_list ENDBLOCK SEMICOLON { add_attach($2, $4); } | ATTACH NUMBER BEGINBLOCK ENDBLOCK SEMICOLON ; detach_block : DETACH NUMBER BEGINBLOCK match_or_action_list ENDBLOCK SEMICOLON { add_detach($2, $4); } | DETACH NUMBER BEGINBLOCK ENDBLOCK SEMICOLON ; nomatch_block : NOMATCH NUMBER BEGINBLOCK match_or_action_list ENDBLOCK SEMICOLON { add_nomatch($2, $4); } | NOMATCH NUMBER BEGINBLOCK ENDBLOCK SEMICOLON ; notify_block : NOTIFY NUMBER BEGINBLOCK match_or_action_list ENDBLOCK SEMICOLON { add_notify($2, $4); } | NOTIFY NUMBER BEGINBLOCK ENDBLOCK SEMICOLON ; match_or_action_list : match_or_action { $$ = add_to_event_proc( NULL, $1); } | match_or_action_list match_or_action { $$ = add_to_event_proc($1, $2); } ; match_or_action : match | action ; match : MATCH STRING STRING SEMICOLON { $$ = new_match($2, $3); } | DEVICE_NAME STRING SEMICOLON { $$ = new_match(strdup("device-name"), $2); } | MEDIA_TYPE STRING SEMICOLON { $$ = new_media(strdup("media-type"), $2); } | CLASS STRING SEMICOLON { $$ = new_match(strdup("class"), $2); } | SUBDEVICE STRING SEMICOLON { $$ = new_match(strdup("subdevice"), $2); } ; action : ACTION STRING SEMICOLON { $$ = new_action($2); } ; %% Index: head/sbin/hastd/hast.h =================================================================== --- head/sbin/hastd/hast.h (revision 235788) +++ head/sbin/hastd/hast.h (revision 235789) @@ -1,249 +1,248 @@ /*- * Copyright (c) 2009-2010 The FreeBSD Foundation * Copyright (c) 2011 Pawel Jakub Dawidek * All rights reserved. * * This software was developed by Pawel Jakub Dawidek under sponsorship from * the FreeBSD Foundation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #ifndef _HAST_H_ #define _HAST_H_ #include #include #include #include #include #include #include #include #include #include "proto.h" /* * Version history: * 0 - initial version * 1 - HIO_KEEPALIVE added */ #define HAST_PROTO_VERSION 1 #define EHAST_OK 0 #define EHAST_NOENTRY 1 #define EHAST_INVALID 2 #define EHAST_NOMEMORY 3 #define EHAST_UNIMPLEMENTED 4 #define HASTCTL_CMD_UNKNOWN 0 #define HASTCTL_CMD_SETROLE 1 #define HASTCTL_CMD_STATUS 2 #define HAST_ROLE_UNDEF 0 #define HAST_ROLE_INIT 1 #define HAST_ROLE_PRIMARY 2 #define HAST_ROLE_SECONDARY 3 #define HAST_SYNCSRC_UNDEF 0 #define HAST_SYNCSRC_PRIMARY 1 #define HAST_SYNCSRC_SECONDARY 2 #define HIO_UNDEF 0 #define HIO_READ 1 #define HIO_WRITE 2 #define HIO_DELETE 3 #define HIO_FLUSH 4 #define HIO_KEEPALIVE 5 #define HAST_USER "hast" #define HAST_TIMEOUT 20 #define HAST_CONFIG "/etc/hast.conf" #define HAST_CONTROL "/var/run/hastctl" #define HASTD_LISTEN_TCP4 "tcp4://0.0.0.0:8457" #define HASTD_LISTEN_TCP6 "tcp6://[::]:8457" #define HASTD_PIDFILE "/var/run/hastd.pid" /* Default extent size. */ #define HAST_EXTENTSIZE 2097152 /* Default maximum number of extents that are kept dirty. */ #define HAST_KEEPDIRTY 64 #define HAST_ADDRSIZE 1024 #define HAST_TOKEN_SIZE 16 /* Number of seconds to sleep between reconnect retries or keepalive packets. */ #define HAST_KEEPALIVE 10 struct hastd_listen { /* Address to listen on. */ char hl_addr[HAST_ADDRSIZE]; /* Protocol-specific data. */ struct proto_conn *hl_conn; TAILQ_ENTRY(hastd_listen) hl_next; }; struct hastd_config { /* Address to communicate with hastctl(8). */ char hc_controladdr[HAST_ADDRSIZE]; /* Protocol-specific data. */ struct proto_conn *hc_controlconn; /* Incoming control connection. */ struct proto_conn *hc_controlin; /* PID file path. */ char hc_pidfile[PATH_MAX]; /* List of addresses to listen on. */ TAILQ_HEAD(, hastd_listen) hc_listen; /* List of resources. */ TAILQ_HEAD(, hast_resource) hc_resources; }; #define HAST_REPLICATION_FULLSYNC 0 #define HAST_REPLICATION_MEMSYNC 1 #define HAST_REPLICATION_ASYNC 2 #define HAST_COMPRESSION_NONE 0 #define HAST_COMPRESSION_HOLE 1 #define HAST_COMPRESSION_LZF 2 #define HAST_CHECKSUM_NONE 0 #define HAST_CHECKSUM_CRC32 1 #define HAST_CHECKSUM_SHA256 2 /* * Structure that describes single resource. */ struct hast_resource { /* Resource name. */ char hr_name[NAME_MAX]; /* Replication mode (HAST_REPLICATION_*). */ int hr_replication; /* Provider name that will appear in /dev/hast/. */ char hr_provname[NAME_MAX]; /* Synchronization extent size. */ int hr_extentsize; /* Maximum number of extents that are kept dirty. */ int hr_keepdirty; /* Path to a program to execute on various events. */ char hr_exec[PATH_MAX]; /* Compression algorithm. */ int hr_compression; /* Checksum algorithm. */ int hr_checksum; /* Path to local component. */ char hr_localpath[PATH_MAX]; /* Descriptor to access local component. */ int hr_localfd; /* Offset into local component. */ off_t hr_localoff; /* Size of usable space. */ off_t hr_datasize; /* Size of entire local provider. */ off_t hr_local_mediasize; /* Sector size of local provider. */ unsigned int hr_local_sectorsize; /* Is flushing write cache supported by the local provider? */ bool hr_localflush; /* Flush write cache on metadata updates? */ int hr_metaflush; /* Descriptor for /dev/ggctl communication. */ int hr_ggatefd; /* Unit number for ggate communication. */ int hr_ggateunit; /* Address of the remote component. */ char hr_remoteaddr[HAST_ADDRSIZE]; /* Local address to bind to for outgoing connections. */ char hr_sourceaddr[HAST_ADDRSIZE]; /* Connection for incoming data. */ struct proto_conn *hr_remotein; /* Connection for outgoing data. */ struct proto_conn *hr_remoteout; /* Token to verify both in and out connection are coming from the same node (not necessarily from the same address). */ unsigned char hr_token[HAST_TOKEN_SIZE]; /* Connection timeout. */ int hr_timeout; /* Resource unique identifier. */ uint64_t hr_resuid; /* Primary's local modification count. */ uint64_t hr_primary_localcnt; /* Primary's remote modification count. */ uint64_t hr_primary_remotecnt; /* Secondary's local modification count. */ uint64_t hr_secondary_localcnt; /* Secondary's remote modification count. */ uint64_t hr_secondary_remotecnt; /* Synchronization source. */ uint8_t hr_syncsrc; /* Resource role: HAST_ROLE_{INIT,PRIMARY,SECONDARY}. */ int hr_role; /* Previous resource role: HAST_ROLE_{INIT,PRIMARY,SECONDARY}. */ int hr_previous_role; /* PID of child worker process. 0 - no child. */ pid_t hr_workerpid; /* Control commands from parent to child. */ struct proto_conn *hr_ctrl; /* Events from child to parent. */ struct proto_conn *hr_event; /* Connection requests from child to parent. */ struct proto_conn *hr_conn; /* Activemap structure. */ struct activemap *hr_amp; /* Locked used to synchronize access to hr_amp. */ pthread_mutex_t hr_amp_lock; /* Number of BIO_READ requests. */ uint64_t hr_stat_read; /* Number of BIO_WRITE requests. */ uint64_t hr_stat_write; /* Number of BIO_DELETE requests. */ uint64_t hr_stat_delete; /* Number of BIO_FLUSH requests. */ uint64_t hr_stat_flush; /* Number of activemap updates. */ uint64_t hr_stat_activemap_update; /* Next resource. */ TAILQ_ENTRY(hast_resource) hr_next; }; struct hastd_config *yy_config_parse(const char *config, bool exitonerror); void yy_config_free(struct hastd_config *config); void yyerror(const char *); int yylex(void); -int yyparse(void); #endif /* !_HAST_H_ */ Index: head/sbin/hastd/parse.y =================================================================== --- head/sbin/hastd/parse.y (revision 235788) +++ head/sbin/hastd/parse.y (revision 235789) @@ -1,1006 +1,1012 @@ %{ /*- * Copyright (c) 2009-2010 The FreeBSD Foundation * Copyright (c) 2011 Pawel Jakub Dawidek * All rights reserved. * * This software was developed by Pawel Jakub Dawidek under sponsorship from * the FreeBSD Foundation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include /* MAXHOSTNAMELEN */ #include #include #include #include #include #include #include #include #include #include #include #include "hast.h" extern int depth; extern int lineno; extern FILE *yyin; extern char *yytext; static struct hastd_config *lconfig; static struct hast_resource *curres; static bool mynode, hadmynode; static char depth0_control[HAST_ADDRSIZE]; static char depth0_pidfile[PATH_MAX]; static char depth0_listen_tcp4[HAST_ADDRSIZE]; static char depth0_listen_tcp6[HAST_ADDRSIZE]; static TAILQ_HEAD(, hastd_listen) depth0_listen; static int depth0_replication; static int depth0_checksum; static int depth0_compression; static int depth0_timeout; static char depth0_exec[PATH_MAX]; static int depth0_metaflush; static char depth1_provname[PATH_MAX]; static char depth1_localpath[PATH_MAX]; static int depth1_metaflush; extern void yyrestart(FILE *); -static int -isitme(const char *name) -{ - char buf[MAXHOSTNAMELEN]; - char *pos; - size_t bufsize; - - /* - * First check if the given name matches our full hostname. - */ - if (gethostname(buf, sizeof(buf)) < 0) { - pjdlog_errno(LOG_ERR, "gethostname() failed"); - return (-1); - } - if (strcmp(buf, name) == 0) - return (1); - - /* - * Now check if it matches first part of the host name. - */ - pos = strchr(buf, '.'); - if (pos != NULL && (size_t)(pos - buf) == strlen(name) && - strncmp(buf, name, pos - buf) == 0) { - return (1); - } - - /* - * At the end check if name is equal to our host's UUID. - */ - bufsize = sizeof(buf); - if (sysctlbyname("kern.hostuuid", buf, &bufsize, NULL, 0) < 0) { - pjdlog_errno(LOG_ERR, "sysctlbyname(kern.hostuuid) failed"); - return (-1); - } - if (strcasecmp(buf, name) == 0) - return (1); - - /* - * Looks like this isn't about us. - */ - return (0); -} - -static bool -family_supported(int family) -{ - int sock; - - sock = socket(family, SOCK_STREAM, 0); - if (sock == -1 && errno == EPROTONOSUPPORT) - return (false); - if (sock >= 0) - (void)close(sock); - return (true); -} - -static int -node_names(char **namesp) -{ - static char names[MAXHOSTNAMELEN * 3]; - char buf[MAXHOSTNAMELEN]; - char *pos; - size_t bufsize; - - if (gethostname(buf, sizeof(buf)) < 0) { - pjdlog_errno(LOG_ERR, "gethostname() failed"); - return (-1); - } - - /* First component of the host name. */ - pos = strchr(buf, '.'); - if (pos != NULL && pos != buf) { - (void)strlcpy(names, buf, MIN((size_t)(pos - buf + 1), - sizeof(names))); - (void)strlcat(names, ", ", sizeof(names)); - } - - /* Full host name. */ - (void)strlcat(names, buf, sizeof(names)); - (void)strlcat(names, ", ", sizeof(names)); - - /* Host UUID. */ - bufsize = sizeof(buf); - if (sysctlbyname("kern.hostuuid", buf, &bufsize, NULL, 0) < 0) { - pjdlog_errno(LOG_ERR, "sysctlbyname(kern.hostuuid) failed"); - return (-1); - } - (void)strlcat(names, buf, sizeof(names)); - - *namesp = names; - - return (0); -} - -void -yyerror(const char *str) -{ - - pjdlog_error("Unable to parse configuration file at line %d near '%s': %s", - lineno, yytext, str); -} - -struct hastd_config * -yy_config_parse(const char *config, bool exitonerror) -{ - int ret; - - curres = NULL; - mynode = false; - depth = 0; - lineno = 0; - - depth0_timeout = HAST_TIMEOUT; - depth0_replication = HAST_REPLICATION_FULLSYNC; - depth0_checksum = HAST_CHECKSUM_NONE; - depth0_compression = HAST_COMPRESSION_HOLE; - strlcpy(depth0_control, HAST_CONTROL, sizeof(depth0_control)); - strlcpy(depth0_pidfile, HASTD_PIDFILE, sizeof(depth0_pidfile)); - TAILQ_INIT(&depth0_listen); - strlcpy(depth0_listen_tcp4, HASTD_LISTEN_TCP4, - sizeof(depth0_listen_tcp4)); - strlcpy(depth0_listen_tcp6, HASTD_LISTEN_TCP6, - sizeof(depth0_listen_tcp6)); - depth0_exec[0] = '\0'; - depth0_metaflush = 1; - - lconfig = calloc(1, sizeof(*lconfig)); - if (lconfig == NULL) { - pjdlog_error("Unable to allocate memory for configuration."); - if (exitonerror) - exit(EX_TEMPFAIL); - return (NULL); - } - - TAILQ_INIT(&lconfig->hc_listen); - TAILQ_INIT(&lconfig->hc_resources); - - yyin = fopen(config, "r"); - if (yyin == NULL) { - pjdlog_errno(LOG_ERR, "Unable to open configuration file %s", - config); - yy_config_free(lconfig); - if (exitonerror) - exit(EX_OSFILE); - return (NULL); - } - yyrestart(yyin); - ret = yyparse(); - fclose(yyin); - if (ret != 0) { - yy_config_free(lconfig); - if (exitonerror) - exit(EX_CONFIG); - return (NULL); - } - - /* - * Let's see if everything is set up. - */ - if (lconfig->hc_controladdr[0] == '\0') { - strlcpy(lconfig->hc_controladdr, depth0_control, - sizeof(lconfig->hc_controladdr)); - } - if (lconfig->hc_pidfile[0] == '\0') { - strlcpy(lconfig->hc_pidfile, depth0_pidfile, - sizeof(lconfig->hc_pidfile)); - } - if (!TAILQ_EMPTY(&depth0_listen)) - TAILQ_CONCAT(&lconfig->hc_listen, &depth0_listen, hl_next); - if (TAILQ_EMPTY(&lconfig->hc_listen)) { - struct hastd_listen *lst; - - if (family_supported(AF_INET)) { - lst = calloc(1, sizeof(*lst)); - if (lst == NULL) { - pjdlog_error("Unable to allocate memory for listen address."); - yy_config_free(lconfig); - if (exitonerror) - exit(EX_TEMPFAIL); - return (NULL); - } - (void)strlcpy(lst->hl_addr, depth0_listen_tcp4, - sizeof(lst->hl_addr)); - TAILQ_INSERT_TAIL(&lconfig->hc_listen, lst, hl_next); - } else { - pjdlog_debug(1, - "No IPv4 support in the kernel, not listening on IPv4 address."); - } - if (family_supported(AF_INET6)) { - lst = calloc(1, sizeof(*lst)); - if (lst == NULL) { - pjdlog_error("Unable to allocate memory for listen address."); - yy_config_free(lconfig); - if (exitonerror) - exit(EX_TEMPFAIL); - return (NULL); - } - (void)strlcpy(lst->hl_addr, depth0_listen_tcp6, - sizeof(lst->hl_addr)); - TAILQ_INSERT_TAIL(&lconfig->hc_listen, lst, hl_next); - } else { - pjdlog_debug(1, - "No IPv6 support in the kernel, not listening on IPv6 address."); - } - if (TAILQ_EMPTY(&lconfig->hc_listen)) { - pjdlog_error("No address to listen on."); - yy_config_free(lconfig); - if (exitonerror) - exit(EX_TEMPFAIL); - return (NULL); - } - } - TAILQ_FOREACH(curres, &lconfig->hc_resources, hr_next) { - PJDLOG_ASSERT(curres->hr_provname[0] != '\0'); - PJDLOG_ASSERT(curres->hr_localpath[0] != '\0'); - PJDLOG_ASSERT(curres->hr_remoteaddr[0] != '\0'); - - if (curres->hr_replication == -1) { - /* - * Replication is not set at resource-level. - * Use global or default setting. - */ - curres->hr_replication = depth0_replication; - } - if (curres->hr_replication == HAST_REPLICATION_MEMSYNC) { - pjdlog_warning("Replication mode \"%s\" is not implemented, falling back to \"%s\".", - "memsync", "fullsync"); - curres->hr_replication = HAST_REPLICATION_FULLSYNC; - } - if (curres->hr_checksum == -1) { - /* - * Checksum is not set at resource-level. - * Use global or default setting. - */ - curres->hr_checksum = depth0_checksum; - } - if (curres->hr_compression == -1) { - /* - * Compression is not set at resource-level. - * Use global or default setting. - */ - curres->hr_compression = depth0_compression; - } - if (curres->hr_timeout == -1) { - /* - * Timeout is not set at resource-level. - * Use global or default setting. - */ - curres->hr_timeout = depth0_timeout; - } - if (curres->hr_exec[0] == '\0') { - /* - * Exec is not set at resource-level. - * Use global or default setting. - */ - strlcpy(curres->hr_exec, depth0_exec, - sizeof(curres->hr_exec)); - } - if (curres->hr_metaflush == -1) { - /* - * Metaflush is not set at resource-level. - * Use global or default setting. - */ - curres->hr_metaflush = depth0_metaflush; - } - } - - return (lconfig); -} - -void -yy_config_free(struct hastd_config *config) -{ - struct hastd_listen *lst; - struct hast_resource *res; - - while ((lst = TAILQ_FIRST(&depth0_listen)) != NULL) { - TAILQ_REMOVE(&depth0_listen, lst, hl_next); - free(lst); - } - while ((lst = TAILQ_FIRST(&config->hc_listen)) != NULL) { - TAILQ_REMOVE(&config->hc_listen, lst, hl_next); - free(lst); - } - while ((res = TAILQ_FIRST(&config->hc_resources)) != NULL) { - TAILQ_REMOVE(&config->hc_resources, res, hr_next); - free(res); - } - free(config); -} +static int isitme(const char *name); +static bool family_supported(int family); +static int node_names(char **namesp); %} %token CONTROL PIDFILE LISTEN REPLICATION CHECKSUM COMPRESSION METAFLUSH %token TIMEOUT EXEC RESOURCE NAME LOCAL REMOTE SOURCE ON OFF %token FULLSYNC MEMSYNC ASYNC NONE CRC32 SHA256 HOLE LZF %token NUM STR OB CB %type remote_str %type replication_type %type checksum_type %type compression_type %type boolean %union { int num; char *str; } %token NUM %token STR %% statements: | statements statement ; statement: control_statement | pidfile_statement | listen_statement | replication_statement | checksum_statement | compression_statement | timeout_statement | exec_statement | metaflush_statement | node_statement | resource_statement ; control_statement: CONTROL STR { switch (depth) { case 0: if (strlcpy(depth0_control, $2, sizeof(depth0_control)) >= sizeof(depth0_control)) { pjdlog_error("control argument is too long."); free($2); return (1); } break; case 1: if (!mynode) break; if (strlcpy(lconfig->hc_controladdr, $2, sizeof(lconfig->hc_controladdr)) >= sizeof(lconfig->hc_controladdr)) { pjdlog_error("control argument is too long."); free($2); return (1); } break; default: PJDLOG_ABORT("control at wrong depth level"); } free($2); } ; pidfile_statement: PIDFILE STR { switch (depth) { case 0: if (strlcpy(depth0_pidfile, $2, sizeof(depth0_pidfile)) >= sizeof(depth0_pidfile)) { pjdlog_error("pidfile argument is too long."); free($2); return (1); } break; case 1: if (!mynode) break; if (strlcpy(lconfig->hc_pidfile, $2, sizeof(lconfig->hc_pidfile)) >= sizeof(lconfig->hc_pidfile)) { pjdlog_error("pidfile argument is too long."); free($2); return (1); } break; default: PJDLOG_ABORT("pidfile at wrong depth level"); } free($2); } ; listen_statement: LISTEN STR { struct hastd_listen *lst; lst = calloc(1, sizeof(*lst)); if (lst == NULL) { pjdlog_error("Unable to allocate memory for listen address."); free($2); return (1); } if (strlcpy(lst->hl_addr, $2, sizeof(lst->hl_addr)) >= sizeof(lst->hl_addr)) { pjdlog_error("listen argument is too long."); free($2); free(lst); return (1); } switch (depth) { case 0: TAILQ_INSERT_TAIL(&depth0_listen, lst, hl_next); break; case 1: if (mynode) TAILQ_INSERT_TAIL(&depth0_listen, lst, hl_next); else free(lst); break; default: PJDLOG_ABORT("listen at wrong depth level"); } free($2); } ; replication_statement: REPLICATION replication_type { switch (depth) { case 0: depth0_replication = $2; break; case 1: PJDLOG_ASSERT(curres != NULL); curres->hr_replication = $2; break; default: PJDLOG_ABORT("replication at wrong depth level"); } } ; replication_type: FULLSYNC { $$ = HAST_REPLICATION_FULLSYNC; } | MEMSYNC { $$ = HAST_REPLICATION_MEMSYNC; } | ASYNC { $$ = HAST_REPLICATION_ASYNC; } ; checksum_statement: CHECKSUM checksum_type { switch (depth) { case 0: depth0_checksum = $2; break; case 1: PJDLOG_ASSERT(curres != NULL); curres->hr_checksum = $2; break; default: PJDLOG_ABORT("checksum at wrong depth level"); } } ; checksum_type: NONE { $$ = HAST_CHECKSUM_NONE; } | CRC32 { $$ = HAST_CHECKSUM_CRC32; } | SHA256 { $$ = HAST_CHECKSUM_SHA256; } ; compression_statement: COMPRESSION compression_type { switch (depth) { case 0: depth0_compression = $2; break; case 1: PJDLOG_ASSERT(curres != NULL); curres->hr_compression = $2; break; default: PJDLOG_ABORT("compression at wrong depth level"); } } ; compression_type: NONE { $$ = HAST_COMPRESSION_NONE; } | HOLE { $$ = HAST_COMPRESSION_HOLE; } | LZF { $$ = HAST_COMPRESSION_LZF; } ; timeout_statement: TIMEOUT NUM { if ($2 <= 0) { pjdlog_error("Negative or zero timeout."); return (1); } switch (depth) { case 0: depth0_timeout = $2; break; case 1: PJDLOG_ASSERT(curres != NULL); curres->hr_timeout = $2; break; default: PJDLOG_ABORT("timeout at wrong depth level"); } } ; exec_statement: EXEC STR { switch (depth) { case 0: if (strlcpy(depth0_exec, $2, sizeof(depth0_exec)) >= sizeof(depth0_exec)) { pjdlog_error("Exec path is too long."); free($2); return (1); } break; case 1: PJDLOG_ASSERT(curres != NULL); if (strlcpy(curres->hr_exec, $2, sizeof(curres->hr_exec)) >= sizeof(curres->hr_exec)) { pjdlog_error("Exec path is too long."); free($2); return (1); } break; default: PJDLOG_ABORT("exec at wrong depth level"); } free($2); } ; metaflush_statement: METAFLUSH boolean { switch (depth) { case 0: depth0_metaflush = $2; break; case 1: PJDLOG_ASSERT(curres != NULL); depth1_metaflush = $2; break; case 2: if (!mynode) break; PJDLOG_ASSERT(curres != NULL); curres->hr_metaflush = $2; break; default: PJDLOG_ABORT("metaflush at wrong depth level"); } } ; boolean: ON { $$ = 1; } | OFF { $$ = 0; } ; node_statement: ON node_start OB node_entries CB { mynode = false; } ; node_start: STR { switch (isitme($1)) { case -1: free($1); return (1); case 0: break; case 1: mynode = true; break; default: PJDLOG_ABORT("invalid isitme() return value"); } free($1); } ; node_entries: | node_entries node_entry ; node_entry: control_statement | pidfile_statement | listen_statement ; resource_statement: RESOURCE resource_start OB resource_entries CB { if (curres != NULL) { /* * There must be section for this node, at least with * remote address configuration. */ if (!hadmynode) { char *names; if (node_names(&names) != 0) return (1); pjdlog_error("No resource %s configuration for this node (acceptable node names: %s).", curres->hr_name, names); return (1); } /* * Let's see if there are some resource-level settings * that we can use for node-level settings. */ if (curres->hr_provname[0] == '\0' && depth1_provname[0] != '\0') { /* * Provider name is not set at node-level, * but is set at resource-level, use it. */ strlcpy(curres->hr_provname, depth1_provname, sizeof(curres->hr_provname)); } if (curres->hr_localpath[0] == '\0' && depth1_localpath[0] != '\0') { /* * Path to local provider is not set at * node-level, but is set at resource-level, * use it. */ strlcpy(curres->hr_localpath, depth1_localpath, sizeof(curres->hr_localpath)); } if (curres->hr_metaflush == -1 && depth1_metaflush != -1) { /* * Metaflush is not set at node-level, * but is set at resource-level, use it. */ curres->hr_metaflush = depth1_metaflush; } /* * If provider name is not given, use resource name * as provider name. */ if (curres->hr_provname[0] == '\0') { strlcpy(curres->hr_provname, curres->hr_name, sizeof(curres->hr_provname)); } /* * Remote address has to be configured at this point. */ if (curres->hr_remoteaddr[0] == '\0') { pjdlog_error("Remote address not configured for resource %s.", curres->hr_name); return (1); } /* * Path to local provider has to be configured at this * point. */ if (curres->hr_localpath[0] == '\0') { pjdlog_error("Path to local component not configured for resource %s.", curres->hr_name); return (1); } /* Put it onto resource list. */ TAILQ_INSERT_TAIL(&lconfig->hc_resources, curres, hr_next); curres = NULL; } } ; resource_start: STR { /* Check if there is no duplicate entry. */ TAILQ_FOREACH(curres, &lconfig->hc_resources, hr_next) { if (strcmp(curres->hr_name, $1) == 0) { pjdlog_error("Resource %s configured more than once.", curres->hr_name); free($1); return (1); } } /* * Clear those, so we can tell if they were set at * resource-level or not. */ depth1_provname[0] = '\0'; depth1_localpath[0] = '\0'; depth1_metaflush = -1; hadmynode = false; curres = calloc(1, sizeof(*curres)); if (curres == NULL) { pjdlog_error("Unable to allocate memory for resource."); free($1); return (1); } if (strlcpy(curres->hr_name, $1, sizeof(curres->hr_name)) >= sizeof(curres->hr_name)) { pjdlog_error("Resource name is too long."); free(curres); free($1); return (1); } free($1); curres->hr_role = HAST_ROLE_INIT; curres->hr_previous_role = HAST_ROLE_INIT; curres->hr_replication = -1; curres->hr_checksum = -1; curres->hr_compression = -1; curres->hr_timeout = -1; curres->hr_exec[0] = '\0'; curres->hr_provname[0] = '\0'; curres->hr_localpath[0] = '\0'; curres->hr_localfd = -1; curres->hr_localflush = true; curres->hr_metaflush = -1; curres->hr_remoteaddr[0] = '\0'; curres->hr_sourceaddr[0] = '\0'; curres->hr_ggateunit = -1; } ; resource_entries: | resource_entries resource_entry ; resource_entry: replication_statement | checksum_statement | compression_statement | timeout_statement | exec_statement | metaflush_statement | name_statement | local_statement | resource_node_statement ; name_statement: NAME STR { switch (depth) { case 1: if (strlcpy(depth1_provname, $2, sizeof(depth1_provname)) >= sizeof(depth1_provname)) { pjdlog_error("name argument is too long."); free($2); return (1); } break; case 2: if (!mynode) break; PJDLOG_ASSERT(curres != NULL); if (strlcpy(curres->hr_provname, $2, sizeof(curres->hr_provname)) >= sizeof(curres->hr_provname)) { pjdlog_error("name argument is too long."); free($2); return (1); } break; default: PJDLOG_ABORT("name at wrong depth level"); } free($2); } ; local_statement: LOCAL STR { switch (depth) { case 1: if (strlcpy(depth1_localpath, $2, sizeof(depth1_localpath)) >= sizeof(depth1_localpath)) { pjdlog_error("local argument is too long."); free($2); return (1); } break; case 2: if (!mynode) break; PJDLOG_ASSERT(curres != NULL); if (strlcpy(curres->hr_localpath, $2, sizeof(curres->hr_localpath)) >= sizeof(curres->hr_localpath)) { pjdlog_error("local argument is too long."); free($2); return (1); } break; default: PJDLOG_ABORT("local at wrong depth level"); } free($2); } ; resource_node_statement:ON resource_node_start OB resource_node_entries CB { mynode = false; } ; resource_node_start: STR { if (curres != NULL) { switch (isitme($1)) { case -1: free($1); return (1); case 0: break; case 1: mynode = hadmynode = true; break; default: PJDLOG_ABORT("invalid isitme() return value"); } } free($1); } ; resource_node_entries: | resource_node_entries resource_node_entry ; resource_node_entry: name_statement | local_statement | remote_statement | source_statement | metaflush_statement ; remote_statement: REMOTE remote_str { PJDLOG_ASSERT(depth == 2); if (mynode) { PJDLOG_ASSERT(curres != NULL); if (strlcpy(curres->hr_remoteaddr, $2, sizeof(curres->hr_remoteaddr)) >= sizeof(curres->hr_remoteaddr)) { pjdlog_error("remote argument is too long."); free($2); return (1); } } free($2); } ; remote_str: NONE { $$ = strdup("none"); } | STR { } ; source_statement: SOURCE STR { PJDLOG_ASSERT(depth == 2); if (mynode) { PJDLOG_ASSERT(curres != NULL); if (strlcpy(curres->hr_sourceaddr, $2, sizeof(curres->hr_sourceaddr)) >= sizeof(curres->hr_sourceaddr)) { pjdlog_error("source argument is too long."); free($2); return (1); } } free($2); } ; + +%% + +static int +isitme(const char *name) +{ + char buf[MAXHOSTNAMELEN]; + char *pos; + size_t bufsize; + + /* + * First check if the given name matches our full hostname. + */ + if (gethostname(buf, sizeof(buf)) < 0) { + pjdlog_errno(LOG_ERR, "gethostname() failed"); + return (-1); + } + if (strcmp(buf, name) == 0) + return (1); + + /* + * Now check if it matches first part of the host name. + */ + pos = strchr(buf, '.'); + if (pos != NULL && (size_t)(pos - buf) == strlen(name) && + strncmp(buf, name, pos - buf) == 0) { + return (1); + } + + /* + * At the end check if name is equal to our host's UUID. + */ + bufsize = sizeof(buf); + if (sysctlbyname("kern.hostuuid", buf, &bufsize, NULL, 0) < 0) { + pjdlog_errno(LOG_ERR, "sysctlbyname(kern.hostuuid) failed"); + return (-1); + } + if (strcasecmp(buf, name) == 0) + return (1); + + /* + * Looks like this isn't about us. + */ + return (0); +} + +static bool +family_supported(int family) +{ + int sock; + + sock = socket(family, SOCK_STREAM, 0); + if (sock == -1 && errno == EPROTONOSUPPORT) + return (false); + if (sock >= 0) + (void)close(sock); + return (true); +} + +static int +node_names(char **namesp) +{ + static char names[MAXHOSTNAMELEN * 3]; + char buf[MAXHOSTNAMELEN]; + char *pos; + size_t bufsize; + + if (gethostname(buf, sizeof(buf)) < 0) { + pjdlog_errno(LOG_ERR, "gethostname() failed"); + return (-1); + } + + /* First component of the host name. */ + pos = strchr(buf, '.'); + if (pos != NULL && pos != buf) { + (void)strlcpy(names, buf, MIN((size_t)(pos - buf + 1), + sizeof(names))); + (void)strlcat(names, ", ", sizeof(names)); + } + + /* Full host name. */ + (void)strlcat(names, buf, sizeof(names)); + (void)strlcat(names, ", ", sizeof(names)); + + /* Host UUID. */ + bufsize = sizeof(buf); + if (sysctlbyname("kern.hostuuid", buf, &bufsize, NULL, 0) < 0) { + pjdlog_errno(LOG_ERR, "sysctlbyname(kern.hostuuid) failed"); + return (-1); + } + (void)strlcat(names, buf, sizeof(names)); + + *namesp = names; + + return (0); +} + +void +yyerror(const char *str) +{ + + pjdlog_error("Unable to parse configuration file at line %d near '%s': %s", + lineno, yytext, str); +} + +struct hastd_config * +yy_config_parse(const char *config, bool exitonerror) +{ + int ret; + + curres = NULL; + mynode = false; + depth = 0; + lineno = 0; + + depth0_timeout = HAST_TIMEOUT; + depth0_replication = HAST_REPLICATION_FULLSYNC; + depth0_checksum = HAST_CHECKSUM_NONE; + depth0_compression = HAST_COMPRESSION_HOLE; + strlcpy(depth0_control, HAST_CONTROL, sizeof(depth0_control)); + strlcpy(depth0_pidfile, HASTD_PIDFILE, sizeof(depth0_pidfile)); + TAILQ_INIT(&depth0_listen); + strlcpy(depth0_listen_tcp4, HASTD_LISTEN_TCP4, + sizeof(depth0_listen_tcp4)); + strlcpy(depth0_listen_tcp6, HASTD_LISTEN_TCP6, + sizeof(depth0_listen_tcp6)); + depth0_exec[0] = '\0'; + depth0_metaflush = 1; + + lconfig = calloc(1, sizeof(*lconfig)); + if (lconfig == NULL) { + pjdlog_error("Unable to allocate memory for configuration."); + if (exitonerror) + exit(EX_TEMPFAIL); + return (NULL); + } + + TAILQ_INIT(&lconfig->hc_listen); + TAILQ_INIT(&lconfig->hc_resources); + + yyin = fopen(config, "r"); + if (yyin == NULL) { + pjdlog_errno(LOG_ERR, "Unable to open configuration file %s", + config); + yy_config_free(lconfig); + if (exitonerror) + exit(EX_OSFILE); + return (NULL); + } + yyrestart(yyin); + ret = yyparse(); + fclose(yyin); + if (ret != 0) { + yy_config_free(lconfig); + if (exitonerror) + exit(EX_CONFIG); + return (NULL); + } + + /* + * Let's see if everything is set up. + */ + if (lconfig->hc_controladdr[0] == '\0') { + strlcpy(lconfig->hc_controladdr, depth0_control, + sizeof(lconfig->hc_controladdr)); + } + if (lconfig->hc_pidfile[0] == '\0') { + strlcpy(lconfig->hc_pidfile, depth0_pidfile, + sizeof(lconfig->hc_pidfile)); + } + if (!TAILQ_EMPTY(&depth0_listen)) + TAILQ_CONCAT(&lconfig->hc_listen, &depth0_listen, hl_next); + if (TAILQ_EMPTY(&lconfig->hc_listen)) { + struct hastd_listen *lst; + + if (family_supported(AF_INET)) { + lst = calloc(1, sizeof(*lst)); + if (lst == NULL) { + pjdlog_error("Unable to allocate memory for listen address."); + yy_config_free(lconfig); + if (exitonerror) + exit(EX_TEMPFAIL); + return (NULL); + } + (void)strlcpy(lst->hl_addr, depth0_listen_tcp4, + sizeof(lst->hl_addr)); + TAILQ_INSERT_TAIL(&lconfig->hc_listen, lst, hl_next); + } else { + pjdlog_debug(1, + "No IPv4 support in the kernel, not listening on IPv4 address."); + } + if (family_supported(AF_INET6)) { + lst = calloc(1, sizeof(*lst)); + if (lst == NULL) { + pjdlog_error("Unable to allocate memory for listen address."); + yy_config_free(lconfig); + if (exitonerror) + exit(EX_TEMPFAIL); + return (NULL); + } + (void)strlcpy(lst->hl_addr, depth0_listen_tcp6, + sizeof(lst->hl_addr)); + TAILQ_INSERT_TAIL(&lconfig->hc_listen, lst, hl_next); + } else { + pjdlog_debug(1, + "No IPv6 support in the kernel, not listening on IPv6 address."); + } + if (TAILQ_EMPTY(&lconfig->hc_listen)) { + pjdlog_error("No address to listen on."); + yy_config_free(lconfig); + if (exitonerror) + exit(EX_TEMPFAIL); + return (NULL); + } + } + TAILQ_FOREACH(curres, &lconfig->hc_resources, hr_next) { + PJDLOG_ASSERT(curres->hr_provname[0] != '\0'); + PJDLOG_ASSERT(curres->hr_localpath[0] != '\0'); + PJDLOG_ASSERT(curres->hr_remoteaddr[0] != '\0'); + + if (curres->hr_replication == -1) { + /* + * Replication is not set at resource-level. + * Use global or default setting. + */ + curres->hr_replication = depth0_replication; + } + if (curres->hr_replication == HAST_REPLICATION_MEMSYNC) { + pjdlog_warning("Replication mode \"%s\" is not implemented, falling back to \"%s\".", + "memsync", "fullsync"); + curres->hr_replication = HAST_REPLICATION_FULLSYNC; + } + if (curres->hr_checksum == -1) { + /* + * Checksum is not set at resource-level. + * Use global or default setting. + */ + curres->hr_checksum = depth0_checksum; + } + if (curres->hr_compression == -1) { + /* + * Compression is not set at resource-level. + * Use global or default setting. + */ + curres->hr_compression = depth0_compression; + } + if (curres->hr_timeout == -1) { + /* + * Timeout is not set at resource-level. + * Use global or default setting. + */ + curres->hr_timeout = depth0_timeout; + } + if (curres->hr_exec[0] == '\0') { + /* + * Exec is not set at resource-level. + * Use global or default setting. + */ + strlcpy(curres->hr_exec, depth0_exec, + sizeof(curres->hr_exec)); + } + if (curres->hr_metaflush == -1) { + /* + * Metaflush is not set at resource-level. + * Use global or default setting. + */ + curres->hr_metaflush = depth0_metaflush; + } + } + + return (lconfig); +} + +void +yy_config_free(struct hastd_config *config) +{ + struct hastd_listen *lst; + struct hast_resource *res; + + while ((lst = TAILQ_FIRST(&depth0_listen)) != NULL) { + TAILQ_REMOVE(&depth0_listen, lst, hl_next); + free(lst); + } + while ((lst = TAILQ_FIRST(&config->hc_listen)) != NULL) { + TAILQ_REMOVE(&config->hc_listen, lst, hl_next); + free(lst); + } + while ((res = TAILQ_FIRST(&config->hc_resources)) != NULL) { + TAILQ_REMOVE(&config->hc_resources, res, hr_next); + free(res); + } + free(config); +} Index: head/usr.bin/ar/acpyacc.y =================================================================== --- head/usr.bin/ar/acpyacc.y (revision 235788) +++ head/usr.bin/ar/acpyacc.y (revision 235789) @@ -1,662 +1,661 @@ %{ /*- * Copyright (c) 2008 Kai Wang * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer * in this position and unchanged. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "ar.h" #define TEMPLATE "arscp.XXXXXXXX" struct list { char *str; struct list *next; }; extern int yylex(void); -extern int yyparse(void); static void yyerror(const char *); static void arscp_addlib(char *archive, struct list *list); static void arscp_addmod(struct list *list); static void arscp_clear(void); static int arscp_copy(int ifd, int ofd); static void arscp_create(char *in, char *out); static void arscp_delete(struct list *list); static void arscp_dir(char *archive, struct list *list, char *rlt); static void arscp_end(int eval); static void arscp_extract(struct list *list); static void arscp_free_argv(void); static void arscp_free_mlist(struct list *list); static void arscp_list(void); static struct list *arscp_mlist(struct list *list, char *str); static void arscp_mlist2argv(struct list *list); static int arscp_mlist_len(struct list *list); static void arscp_open(char *fname); static void arscp_prompt(void); static void arscp_replace(struct list *list); static void arscp_save(void); static int arscp_target_exist(void); extern int lineno; static struct bsdar *bsdar; static char *target; static char *tmpac; static int interactive; static int verbose; %} %token ADDLIB %token ADDMOD %token CLEAR %token CREATE %token DELETE %token DIRECTORY %token END %token EXTRACT %token LIST %token OPEN %token REPLACE %token VERBOSE %token SAVE %token LP %token RP %token COMMA %token EOL %token FNAME %type mod_list %union { char *str; struct list *list; } %% begin : { arscp_prompt(); } ar_script ; ar_script : cmd_list | ; mod_list : FNAME { $$ = arscp_mlist(NULL, $1); } | mod_list separator FNAME { $$ = arscp_mlist($1, $3); } ; separator : COMMA | ; cmd_list : rawcmd | cmd_list rawcmd ; rawcmd : cmd EOL { arscp_prompt(); } ; cmd : addlib_cmd | addmod_cmd | clear_cmd | create_cmd | delete_cmd | directory_cmd | end_cmd | extract_cmd | list_cmd | open_cmd | replace_cmd | verbose_cmd | save_cmd | invalid_cmd | empty_cmd | error ; addlib_cmd : ADDLIB FNAME LP mod_list RP { arscp_addlib($2, $4); } | ADDLIB FNAME { arscp_addlib($2, NULL); } ; addmod_cmd : ADDMOD mod_list { arscp_addmod($2); } ; clear_cmd : CLEAR { arscp_clear(); } ; create_cmd : CREATE FNAME { arscp_create(NULL, $2); } ; delete_cmd : DELETE mod_list { arscp_delete($2); } ; directory_cmd : DIRECTORY FNAME { arscp_dir($2, NULL, NULL); } | DIRECTORY FNAME LP mod_list RP { arscp_dir($2, $4, NULL); } | DIRECTORY FNAME LP mod_list RP FNAME { arscp_dir($2, $4, $6); } ; end_cmd : END { arscp_end(EX_OK); } ; extract_cmd : EXTRACT mod_list { arscp_extract($2); } ; list_cmd : LIST { arscp_list(); } ; open_cmd : OPEN FNAME { arscp_open($2); } ; replace_cmd : REPLACE mod_list { arscp_replace($2); } ; save_cmd : SAVE { arscp_save(); } ; verbose_cmd : VERBOSE { verbose = !verbose; } ; empty_cmd : ; invalid_cmd : FNAME { yyerror(NULL); } ; %% /* ARGSUSED */ static void yyerror(const char *s) { (void) s; printf("Syntax error in archive script, line %d\n", lineno); } /* * arscp_open first open an archive and check its validity. If the archive * format is valid, it calls arscp_create to create a temporary copy of * the archive. */ static void arscp_open(char *fname) { struct archive *a; struct archive_entry *entry; int r; if ((a = archive_read_new()) == NULL) bsdar_errc(bsdar, EX_SOFTWARE, 0, "archive_read_new failed"); archive_read_support_compression_none(a); archive_read_support_format_ar(a); AC(archive_read_open_file(a, fname, DEF_BLKSZ)); if ((r = archive_read_next_header(a, &entry))) bsdar_warnc(bsdar, 0, "%s", archive_error_string(a)); AC(archive_read_close(a)); AC(archive_read_finish(a)); if (r != ARCHIVE_OK) return; arscp_create(fname, fname); } /* * Create archive. in != NULL indicate it's a OPEN cmd, and resulting * archive is based on modification of an existing one. If in == NULL, * we are in CREATE cmd and a new empty archive will be created. */ static void arscp_create(char *in, char *out) { struct archive *a; int ifd, ofd; /* Delete previously created temporary archive, if any. */ if (tmpac) { if (unlink(tmpac) < 0) bsdar_errc(bsdar, EX_IOERR, errno, "unlink failed"); free(tmpac); } tmpac = strdup(TEMPLATE); if (tmpac == NULL) bsdar_errc(bsdar, EX_SOFTWARE, errno, "strdup failed"); if ((ofd = mkstemp(tmpac)) < 0) bsdar_errc(bsdar, EX_IOERR, errno, "mkstemp failed"); if (in) { /* * Command OPEN creates a temporary copy of the * input archive. */ if ((ifd = open(in, O_RDONLY)) < 0) { bsdar_warnc(bsdar, errno, "open failed"); return; } if (arscp_copy(ifd, ofd)) { bsdar_warnc(bsdar, 0, "arscp_copy failed"); return; } close(ifd); close(ofd); } else { /* * Command CREATE creates an "empty" archive. * (archive with only global header) */ if ((a = archive_write_new()) == NULL) bsdar_errc(bsdar, EX_SOFTWARE, 0, "archive_write_new failed"); archive_write_set_format_ar_svr4(a); AC(archive_write_open_fd(a, ofd)); AC(archive_write_close(a)); AC(archive_write_finish(a)); } /* Override previous target, if any. */ if (target) free(target); target = out; bsdar->filename = tmpac; } /* A file copying implementation using mmap. */ static int arscp_copy(int ifd, int ofd) { struct stat sb; char *buf, *p; ssize_t w; size_t bytes; if (fstat(ifd, &sb) < 0) { bsdar_warnc(bsdar, errno, "fstate failed"); return (1); } if ((p = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, ifd, (off_t)0)) == MAP_FAILED) { bsdar_warnc(bsdar, errno, "mmap failed"); return (1); } for (buf = p, bytes = sb.st_size; bytes > 0; bytes -= w) { w = write(ofd, buf, bytes); if (w <= 0) { bsdar_warnc(bsdar, errno, "write failed"); break; } } if (munmap(p, sb.st_size) < 0) bsdar_errc(bsdar, EX_SOFTWARE, errno, "munmap failed"); if (bytes > 0) return (1); return (0); } /* * Add all modules of archive to current archive, if list != NULL, * only those modules specified in 'list' will be added. */ static void arscp_addlib(char *archive, struct list *list) { if (!arscp_target_exist()) return; arscp_mlist2argv(list); bsdar->addlib = archive; ar_mode_A(bsdar); arscp_free_argv(); arscp_free_mlist(list); } /* Add modules into current archive. */ static void arscp_addmod(struct list *list) { if (!arscp_target_exist()) return; arscp_mlist2argv(list); ar_mode_q(bsdar); arscp_free_argv(); arscp_free_mlist(list); } /* Delete modules from current archive. */ static void arscp_delete(struct list *list) { if (!arscp_target_exist()) return; arscp_mlist2argv(list); ar_mode_d(bsdar); arscp_free_argv(); arscp_free_mlist(list); } /* Extract modules from current archive. */ static void arscp_extract(struct list *list) { if (!arscp_target_exist()) return; arscp_mlist2argv(list); ar_mode_x(bsdar); arscp_free_argv(); arscp_free_mlist(list); } /* List modules of archive. (Simple Mode) */ static void arscp_list(void) { if (!arscp_target_exist()) return; bsdar->argc = 0; bsdar->argv = NULL; /* Always verbose. */ bsdar->options |= AR_V; ar_mode_t(bsdar); bsdar->options &= ~AR_V; } /* List modules of archive. (Advance Mode) */ static void arscp_dir(char *archive, struct list *list, char *rlt) { FILE *out; /* If rlt != NULL, redirect output to it */ out = NULL; if (rlt) { out = stdout; if ((stdout = fopen(rlt, "w")) == NULL) bsdar_errc(bsdar, EX_IOERR, errno, "fopen %s failed", rlt); } bsdar->filename = archive; if (list) arscp_mlist2argv(list); else { bsdar->argc = 0; bsdar->argv = NULL; } if (verbose) bsdar->options |= AR_V; ar_mode_t(bsdar); bsdar->options &= ~AR_V; if (rlt) { if (fclose(stdout) == EOF) bsdar_errc(bsdar, EX_IOERR, errno, "fclose %s failed", rlt); stdout = out; free(rlt); } free(archive); bsdar->filename = tmpac; arscp_free_argv(); arscp_free_mlist(list); } /* Replace modules of current archive. */ static void arscp_replace(struct list *list) { if (!arscp_target_exist()) return; arscp_mlist2argv(list); ar_mode_r(bsdar); arscp_free_argv(); arscp_free_mlist(list); } /* Rename the temporary archive to the target archive. */ static void arscp_save(void) { mode_t mask; if (target) { if (rename(tmpac, target) < 0) bsdar_errc(bsdar, EX_IOERR, errno, "rename failed"); /* * mkstemp creates temp files with mode 0600, here we * set target archive mode per process umask. */ mask = umask(0); umask(mask); if (chmod(target, 0666 & ~mask) < 0) bsdar_errc(bsdar, EX_IOERR, errno, "chmod failed"); free(tmpac); free(target); tmpac = NULL; target= NULL; bsdar->filename = NULL; } else bsdar_warnc(bsdar, 0, "no open output archive"); } /* * Discard all the contents of current archive. This is achieved by * invoking CREATE cmd on current archive. */ static void arscp_clear(void) { char *new_target; if (target) { new_target = strdup(target); if (new_target == NULL) bsdar_errc(bsdar, EX_SOFTWARE, errno, "strdup failed"); arscp_create(NULL, new_target); } } /* * Quit ar(1). Note that END cmd will not SAVE current archive * before exit. */ static void arscp_end(int eval) { if (target) free(target); if (tmpac) { if (unlink(tmpac) == -1) bsdar_errc(bsdar, EX_IOERR, errno, "unlink %s failed", tmpac); free(tmpac); } exit(eval); } /* * Check if target specified, i.e, whether OPEN or CREATE has been * issued by user. */ static int arscp_target_exist(void) { if (target) return (1); bsdar_warnc(bsdar, 0, "no open output archive"); return (0); } /* Construct module list. */ static struct list * arscp_mlist(struct list *list, char *str) { struct list *l; l = malloc(sizeof(*l)); if (l == NULL) bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed"); l->str = str; l->next = list; return (l); } /* Calculate the length of a mlist. */ static int arscp_mlist_len(struct list *list) { int len; for(len = 0; list; list = list->next) len++; return (len); } /* Free the space allocated for mod_list. */ static void arscp_free_mlist(struct list *list) { struct list *l; /* Note that list->str was freed in arscp_free_argv. */ for(; list; list = l) { l = list->next; free(list); } } /* Convert mlist to argv array. */ static void arscp_mlist2argv(struct list *list) { char **argv; int i, n; n = arscp_mlist_len(list); argv = malloc(n * sizeof(*argv)); if (argv == NULL) bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed"); /* Note that module names are stored in reverse order in mlist. */ for(i = n - 1; i >= 0; i--, list = list->next) { if (list == NULL) bsdar_errc(bsdar, EX_SOFTWARE, errno, "invalid mlist"); argv[i] = list->str; } bsdar->argc = n; bsdar->argv = argv; } /* Free space allocated for argv array and its elements. */ static void arscp_free_argv(void) { int i; for(i = 0; i < bsdar->argc; i++) free(bsdar->argv[i]); free(bsdar->argv); } /* Show a prompt if we are in interactive mode */ static void arscp_prompt(void) { if (interactive) { printf("AR >"); fflush(stdout); } } /* Main function for ar script mode. */ void ar_mode_script(struct bsdar *ar) { bsdar = ar; interactive = isatty(fileno(stdin)); while(yyparse()) { if (!interactive) arscp_end(1); } /* Script ends without END */ arscp_end(EX_OK); } Index: head/usr.bin/bc/bc.y =================================================================== --- head/usr.bin/bc/bc.y (revision 235788) +++ head/usr.bin/bc/bc.y (revision 235789) @@ -1,1206 +1,1205 @@ %{ /* $OpenBSD: bc.y,v 1.33 2009/10/27 23:59:36 deraadt Exp $ */ /* * Copyright (c) 2003, Otto Moerbeek * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ /* * This implementation of bc(1) uses concepts from the original 4.4 * BSD bc(1). The code itself is a complete rewrite, based on the * Posix defined bc(1) grammar. Other differences include type safe * usage of pointers to build the tree of emitted code, typed yacc * rule values, dynamic allocation of all data structures and a * completely rewritten lexical analyzer using lex(1). * * Some effort has been made to make sure that the generated code is * the same as the code generated by the older version, to provide * easy regression testing. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "extern.h" #include "pathnames.h" #define BC_VER "1.0-FreeBSD" #define END_NODE ((ssize_t) -1) #define CONST_STRING ((ssize_t) -2) #define ALLOC_STRING ((ssize_t) -3) extern char *yytext; extern FILE *yyin; struct tree { union { char *astr; const char *cstr; } u; ssize_t index; }; -int yyparse(void); int yywrap(void); int fileindex; int sargc; const char **sargv; const char *filename; char *cmdexpr; static void grow(void); static ssize_t cs(const char *); static ssize_t as(const char *); static ssize_t node(ssize_t, ...); static void emit(ssize_t); static void emit_macro(int, ssize_t); static void free_tree(void); static ssize_t numnode(int); static ssize_t lookup(char *, size_t, char); static ssize_t letter_node(char *); static ssize_t array_node(char *); static ssize_t function_node(char *); static void add_par(ssize_t); static void add_local(ssize_t); static void warning(const char *); static void init(void); static void usage(void); static char *escape(const char *); static ssize_t instr_sz = 0; static struct tree *instructions = NULL; static ssize_t current = 0; static int macro_char = '0'; static int reset_macro_char = '0'; static int nesting = 0; static int breakstack[16]; static int breaksp = 0; static ssize_t prologue; static ssize_t epilogue; static bool st_has_continue; static char str_table[UCHAR_MAX][2]; static bool do_fork = true; static u_short var_count; static pid_t dc; static void sigchld(int); extern char *__progname; #define BREAKSTACK_SZ (sizeof(breakstack)/sizeof(breakstack[0])) /* These values are 4.4BSD bc compatible */ #define FUNC_CHAR 0x01 #define ARRAY_CHAR 0xa1 /* Skip '\0', [, \ and ] */ #define ENCODE(c) ((c) < '[' ? (c) : (c) + 3); #define VAR_BASE (256-4) #define MAX_VARIABLES (VAR_BASE * VAR_BASE) const struct option long_options[] = { {"expression", required_argument, NULL, 'e'}, {"help", no_argument, NULL, 'h'}, {"mathlib", no_argument, NULL, 'l'}, /* compatibility option */ {"quiet", no_argument, NULL, 'q'}, {"version", no_argument, NULL, 'v'}, {NULL, no_argument, NULL, 0} }; %} %start program %union { struct lvalue lvalue; const char *str; char *astr; ssize_t node; } %token COMMA SEMICOLON LPAR RPAR LBRACE RBRACE LBRACKET RBRACKET DOT %token NEWLINE %token LETTER %token NUMBER STRING %token DEFINE BREAK QUIT LENGTH %token RETURN FOR IF WHILE SQRT %token SCALE IBASE OBASE AUTO %token CONTINUE ELSE PRINT %left BOOL_OR %left BOOL_AND %nonassoc BOOL_NOT %nonassoc EQUALS LESS_EQ GREATER_EQ UNEQUALS LESS GREATER %right ASSIGN_OP %left PLUS MINUS %left MULTIPLY DIVIDE REMAINDER %right EXPONENT %nonassoc UMINUS %nonassoc INCR DECR %type named_expression %type argument_list %type alloc_macro %type expression %type function %type function_header %type input_item %type opt_argument_list %type opt_expression %type opt_relational_expression %type opt_statement %type print_expression %type print_expression_list %type relational_expression %type return_expression %type semicolon_list %type statement %type statement_list %% program : /* empty */ | program input_item ; input_item : semicolon_list NEWLINE { emit($1); macro_char = reset_macro_char; putchar('\n'); free_tree(); st_has_continue = false; } | function { putchar('\n'); free_tree(); st_has_continue = false; } | error NEWLINE { yyerrok; } | error QUIT { yyerrok; } ; semicolon_list : /* empty */ { $$ = cs(""); } | statement | semicolon_list SEMICOLON statement { $$ = node($1, $3, END_NODE); } | semicolon_list SEMICOLON ; statement_list : /* empty */ { $$ = cs(""); } | statement | statement_list NEWLINE | statement_list NEWLINE statement { $$ = node($1, $3, END_NODE); } | statement_list SEMICOLON | statement_list SEMICOLON statement { $$ = node($1, $3, END_NODE); } ; opt_statement : /* empty */ { $$ = cs(""); } | statement ; statement : expression { $$ = node($1, cs("ps."), END_NODE); } | named_expression ASSIGN_OP expression { if ($2[0] == '\0') $$ = node($3, cs($2), $1.store, END_NODE); else $$ = node($1.load, $3, cs($2), $1.store, END_NODE); } | STRING { $$ = node(cs("["), as($1), cs("]P"), END_NODE); } | BREAK { if (breaksp == 0) { warning("break not in for or while"); YYERROR; } else { $$ = node( numnode(nesting - breakstack[breaksp-1]), cs("Q"), END_NODE); } } | CONTINUE { if (breaksp == 0) { warning("continue not in for or while"); YYERROR; } else { st_has_continue = true; $$ = node(numnode(nesting - breakstack[breaksp-1] - 1), cs("J"), END_NODE); } } | QUIT { sigset_t mask; putchar('q'); fflush(stdout); if (dc) { sigprocmask(SIG_BLOCK, NULL, &mask); sigsuspend(&mask); } else exit(0); } | RETURN return_expression { if (nesting == 0) { warning("return must be in a function"); YYERROR; } $$ = $2; } | FOR LPAR alloc_macro opt_expression SEMICOLON opt_relational_expression SEMICOLON opt_expression RPAR opt_statement pop_nesting { ssize_t n; if (st_has_continue) n = node($10, cs("M"), $8, cs("s."), $6, $3, END_NODE); else n = node($10, $8, cs("s."), $6, $3, END_NODE); emit_macro($3, n); $$ = node($4, cs("s."), $6, $3, cs(" "), END_NODE); } | IF LPAR alloc_macro pop_nesting relational_expression RPAR opt_statement { emit_macro($3, $7); $$ = node($5, $3, cs(" "), END_NODE); } | IF LPAR alloc_macro pop_nesting relational_expression RPAR opt_statement ELSE alloc_macro pop_nesting opt_statement { emit_macro($3, $7); emit_macro($9, $11); $$ = node($5, $3, cs("e"), $9, cs(" "), END_NODE); } | WHILE LPAR alloc_macro relational_expression RPAR opt_statement pop_nesting { ssize_t n; if (st_has_continue) n = node($6, cs("M"), $4, $3, END_NODE); else n = node($6, $4, $3, END_NODE); emit_macro($3, n); $$ = node($4, $3, cs(" "), END_NODE); } | LBRACE statement_list RBRACE { $$ = $2; } | PRINT print_expression_list { $$ = $2; } ; alloc_macro : /* empty */ { $$ = cs(str_table[macro_char]); macro_char++; /* Do not use [, \ and ] */ if (macro_char == '[') macro_char += 3; /* skip letters */ else if (macro_char == 'a') macro_char = '{'; else if (macro_char == ARRAY_CHAR) macro_char += 26; else if (macro_char == 255) fatal("program too big"); if (breaksp == BREAKSTACK_SZ) fatal("nesting too deep"); breakstack[breaksp++] = nesting++; } ; pop_nesting : /* empty */ { breaksp--; } ; function : function_header opt_parameter_list RPAR opt_newline LBRACE NEWLINE opt_auto_define_list statement_list RBRACE { int n = node(prologue, $8, epilogue, cs("0"), numnode(nesting), cs("Q"), END_NODE); emit_macro($1, n); reset_macro_char = macro_char; nesting = 0; breaksp = 0; } ; function_header : DEFINE LETTER LPAR { $$ = function_node($2); free($2); prologue = cs(""); epilogue = cs(""); nesting = 1; breaksp = 0; breakstack[breaksp] = 0; } ; opt_newline : /* empty */ | NEWLINE ; opt_parameter_list : /* empty */ | parameter_list ; parameter_list : LETTER { add_par(letter_node($1)); free($1); } | LETTER LBRACKET RBRACKET { add_par(array_node($1)); free($1); } | parameter_list COMMA LETTER { add_par(letter_node($3)); free($3); } | parameter_list COMMA LETTER LBRACKET RBRACKET { add_par(array_node($3)); free($3); } ; opt_auto_define_list : /* empty */ | AUTO define_list NEWLINE | AUTO define_list SEMICOLON ; define_list : LETTER { add_local(letter_node($1)); free($1); } | LETTER LBRACKET RBRACKET { add_local(array_node($1)); free($1); } | define_list COMMA LETTER { add_local(letter_node($3)); free($3); } | define_list COMMA LETTER LBRACKET RBRACKET { add_local(array_node($3)); free($3); } ; opt_argument_list : /* empty */ { $$ = cs(""); } | argument_list ; argument_list : expression | argument_list COMMA expression { $$ = node($1, $3, END_NODE); } | argument_list COMMA LETTER LBRACKET RBRACKET { $$ = node($1, cs("l"), array_node($3), END_NODE); free($3); } ; opt_relational_expression : /* empty */ { $$ = cs(" 0 0="); } | relational_expression ; relational_expression : expression EQUALS expression { $$ = node($1, $3, cs("="), END_NODE); } | expression UNEQUALS expression { $$ = node($1, $3, cs("!="), END_NODE); } | expression LESS expression { $$ = node($1, $3, cs(">"), END_NODE); } | expression LESS_EQ expression { $$ = node($1, $3, cs("!<"), END_NODE); } | expression GREATER expression { $$ = node($1, $3, cs("<"), END_NODE); } | expression GREATER_EQ expression { $$ = node($1, $3, cs("!>"), END_NODE); } | expression { $$ = node($1, cs(" 0!="), END_NODE); } ; return_expression : /* empty */ { $$ = node(cs("0"), epilogue, numnode(nesting), cs("Q"), END_NODE); } | expression { $$ = node($1, epilogue, numnode(nesting), cs("Q"), END_NODE); } | LPAR RPAR { $$ = node(cs("0"), epilogue, numnode(nesting), cs("Q"), END_NODE); } ; opt_expression : /* empty */ { $$ = cs(" 0"); } | expression ; expression : named_expression { $$ = node($1.load, END_NODE); } | DOT { $$ = node(cs("l."), END_NODE); } | NUMBER { $$ = node(cs(" "), as($1), END_NODE); } | LPAR expression RPAR { $$ = $2; } | LETTER LPAR opt_argument_list RPAR { $$ = node($3, cs("l"), function_node($1), cs("x"), END_NODE); free($1); } | MINUS expression %prec UMINUS { $$ = node(cs(" 0"), $2, cs("-"), END_NODE); } | expression PLUS expression { $$ = node($1, $3, cs("+"), END_NODE); } | expression MINUS expression { $$ = node($1, $3, cs("-"), END_NODE); } | expression MULTIPLY expression { $$ = node($1, $3, cs("*"), END_NODE); } | expression DIVIDE expression { $$ = node($1, $3, cs("/"), END_NODE); } | expression REMAINDER expression { $$ = node($1, $3, cs("%"), END_NODE); } | expression EXPONENT expression { $$ = node($1, $3, cs("^"), END_NODE); } | INCR named_expression { $$ = node($2.load, cs("1+d"), $2.store, END_NODE); } | DECR named_expression { $$ = node($2.load, cs("1-d"), $2.store, END_NODE); } | named_expression INCR { $$ = node($1.load, cs("d1+"), $1.store, END_NODE); } | named_expression DECR { $$ = node($1.load, cs("d1-"), $1.store, END_NODE); } | named_expression ASSIGN_OP expression { if ($2[0] == '\0') $$ = node($3, cs($2), cs("d"), $1.store, END_NODE); else $$ = node($1.load, $3, cs($2), cs("d"), $1.store, END_NODE); } | LENGTH LPAR expression RPAR { $$ = node($3, cs("Z"), END_NODE); } | SQRT LPAR expression RPAR { $$ = node($3, cs("v"), END_NODE); } | SCALE LPAR expression RPAR { $$ = node($3, cs("X"), END_NODE); } | BOOL_NOT expression { $$ = node($2, cs("N"), END_NODE); } | expression BOOL_AND alloc_macro pop_nesting expression { ssize_t n = node(cs("R"), $5, END_NODE); emit_macro($3, n); $$ = node($1, cs("d0!="), $3, END_NODE); } | expression BOOL_OR alloc_macro pop_nesting expression { ssize_t n = node(cs("R"), $5, END_NODE); emit_macro($3, n); $$ = node($1, cs("d0="), $3, END_NODE); } | expression EQUALS expression { $$ = node($1, $3, cs("G"), END_NODE); } | expression UNEQUALS expression { $$ = node($1, $3, cs("GN"), END_NODE); } | expression LESS expression { $$ = node($3, $1, cs("("), END_NODE); } | expression LESS_EQ expression { $$ = node($3, $1, cs("{"), END_NODE); } | expression GREATER expression { $$ = node($1, $3, cs("("), END_NODE); } | expression GREATER_EQ expression { $$ = node($1, $3, cs("{"), END_NODE); } ; named_expression : LETTER { $$.load = node(cs("l"), letter_node($1), END_NODE); $$.store = node(cs("s"), letter_node($1), END_NODE); free($1); } | LETTER LBRACKET expression RBRACKET { $$.load = node($3, cs(";"), array_node($1), END_NODE); $$.store = node($3, cs(":"), array_node($1), END_NODE); free($1); } | SCALE { $$.load = cs("K"); $$.store = cs("k"); } | IBASE { $$.load = cs("I"); $$.store = cs("i"); } | OBASE { $$.load = cs("O"); $$.store = cs("o"); } ; print_expression_list : print_expression | print_expression_list COMMA print_expression { $$ = node($1, $3, END_NODE); } print_expression : expression { $$ = node($1, cs("ds.n"), END_NODE); } | STRING { char *p = escape($1); $$ = node(cs("["), as(p), cs("]n"), END_NODE); free(p); } %% static void grow(void) { struct tree *p; size_t newsize; if (current == instr_sz) { newsize = instr_sz * 2 + 1; p = realloc(instructions, newsize * sizeof(*p)); if (p == NULL) { free(instructions); err(1, NULL); } instructions = p; instr_sz = newsize; } } static ssize_t cs(const char *str) { grow(); instructions[current].index = CONST_STRING; instructions[current].u.cstr = str; return (current++); } static ssize_t as(const char *str) { grow(); instructions[current].index = ALLOC_STRING; instructions[current].u.astr = strdup(str); if (instructions[current].u.astr == NULL) err(1, NULL); return (current++); } static ssize_t node(ssize_t arg, ...) { va_list ap; ssize_t ret; va_start(ap, arg); ret = current; grow(); instructions[current++].index = arg; do { arg = va_arg(ap, ssize_t); grow(); instructions[current++].index = arg; } while (arg != END_NODE); va_end(ap); return (ret); } static void emit(ssize_t i) { if (instructions[i].index >= 0) while (instructions[i].index != END_NODE) emit(instructions[i++].index); else fputs(instructions[i].u.cstr, stdout); } static void emit_macro(int nodeidx, ssize_t code) { putchar('['); emit(code); printf("]s%s\n", instructions[nodeidx].u.cstr); nesting--; } static void free_tree(void) { ssize_t i; for (i = 0; i < current; i++) if (instructions[i].index == ALLOC_STRING) free(instructions[i].u.astr); current = 0; } static ssize_t numnode(int num) { const char *p; if (num < 10) p = str_table['0' + num]; else if (num < 16) p = str_table['A' - 10 + num]; else errx(1, "internal error: break num > 15"); return (node(cs(" "), cs(p), END_NODE)); } static ssize_t lookup(char * str, size_t len, char type) { ENTRY entry, *found; u_char *p; u_short num; /* The scanner allocated an extra byte already */ if (str[len-1] != type) { str[len] = type; str[len+1] = '\0'; } entry.key = str; found = hsearch(entry, FIND); if (found == NULL) { if (var_count == MAX_VARIABLES) errx(1, "too many variables"); p = malloc(4); if (p == NULL) err(1, NULL); num = var_count++; p[0] = 255; p[1] = ENCODE(num / VAR_BASE + 1); p[2] = ENCODE(num % VAR_BASE + 1); p[3] = '\0'; entry.data = (char *)p; entry.key = strdup(str); if (entry.key == NULL) err(1, NULL); found = hsearch(entry, ENTER); if (found == NULL) err(1, NULL); } return (cs(found->data)); } static ssize_t letter_node(char *str) { size_t len; len = strlen(str); if (len == 1 && str[0] != '_') return (cs(str_table[(int)str[0]])); else return (lookup(str, len, 'L')); } static ssize_t array_node(char *str) { size_t len; len = strlen(str); if (len == 1 && str[0] != '_') return (cs(str_table[(int)str[0] - 'a' + ARRAY_CHAR])); else return (lookup(str, len, 'A')); } static ssize_t function_node(char *str) { size_t len; len = strlen(str); if (len == 1 && str[0] != '_') return (cs(str_table[(int)str[0] - 'a' + FUNC_CHAR])); else return (lookup(str, len, 'F')); } static void add_par(ssize_t n) { prologue = node(cs("S"), n, prologue, END_NODE); epilogue = node(epilogue, cs("L"), n, cs("s."), END_NODE); } static void add_local(ssize_t n) { prologue = node(cs("0S"), n, prologue, END_NODE); epilogue = node(epilogue, cs("L"), n, cs("s."), END_NODE); } void yyerror(const char *s) { char *p, *str; int n; if (yyin != NULL && feof(yyin)) n = asprintf(&str, "%s: %s:%d: %s: unexpected EOF", __progname, filename, lineno, s); else if (isspace(yytext[0]) || !isprint(yytext[0])) n = asprintf(&str, "%s: %s:%d: %s: ascii char 0x%02x unexpected", __progname, filename, lineno, s, yytext[0]); else n = asprintf(&str, "%s: %s:%d: %s: %s unexpected", __progname, filename, lineno, s, yytext); if (n == -1) err(1, NULL); fputs("c[", stdout); for (p = str; *p != '\0'; p++) { if (*p == '[' || *p == ']' || *p =='\\') putchar('\\'); putchar(*p); } fputs("]pc\n", stdout); free(str); } void fatal(const char *s) { errx(1, "%s:%d: %s", filename, lineno, s); } static void warning(const char *s) { warnx("%s:%d: %s", filename, lineno, s); } static void init(void) { unsigned int i; for (i = 0; i < UCHAR_MAX; i++) { str_table[i][0] = i; str_table[i][1] = '\0'; } if (hcreate(1 << 16) == 0) err(1, NULL); } static void usage(void) { fprintf(stderr, "usage: %s [-chlqv] [-e expression] [file ...]\n", __progname); exit(1); } static char * escape(const char *str) { char *p, *ret; ret = malloc(strlen(str) + 1); if (ret == NULL) err(1, NULL); p = ret; while (*str != '\0') { /* * We get _escaped_ strings here. Single backslashes are * already converted to double backslashes */ if (*str == '\\') { if (*++str == '\\') { switch (*++str) { case 'a': *p++ = '\a'; break; case 'b': *p++ = '\b'; break; case 'f': *p++ = '\f'; break; case 'n': *p++ = '\n'; break; case 'q': *p++ = '"'; break; case 'r': *p++ = '\r'; break; case 't': *p++ = '\t'; break; case '\\': *p++ = '\\'; break; } str++; } else { *p++ = '\\'; *p++ = *str++; } } else *p++ = *str++; } *p = '\0'; return (ret); } /* ARGSUSED */ void sigchld(int signo) { pid_t pid; int status; switch (signo) { default: for (;;) { pid = waitpid(dc, &status, WUNTRACED); if (pid == -1) { if (errno == EINTR) continue; _exit(0); } if (WIFEXITED(status) || WIFSIGNALED(status)) _exit(0); else break; } } } static const char * dummy_prompt(void) { return (""); } int main(int argc, char *argv[]) { char *q; int p[2]; int ch, i; init(); setlinebuf(stdout); sargv = malloc(argc * sizeof(char *)); if (sargv == NULL) err(1, NULL); if ((cmdexpr = strdup("")) == NULL) err(1, NULL); /* The d debug option is 4.4 BSD bc(1) compatible */ while ((ch = getopt_long(argc, argv, "cde:hlqv", long_options, NULL)) != -1) { switch (ch) { case 'c': case 'd': do_fork = false; break; case 'e': q = cmdexpr; if (asprintf(&cmdexpr, "%s%s\n", cmdexpr, optarg) == -1) err(1, NULL); free(q); break; case 'h': usage(); break; case 'l': sargv[sargc++] = _PATH_LIBB; break; case 'q': /* compatibility option */ break; case 'v': fprintf(stderr, "%s (BSD bc) %s\n", __progname, BC_VER); exit(0); break; default: usage(); } } argc -= optind; argv += optind; interactive = isatty(STDIN_FILENO); for (i = 0; i < argc; i++) sargv[sargc++] = argv[i]; if (do_fork) { if (pipe(p) == -1) err(1, "cannot create pipe"); dc = fork(); if (dc == -1) err(1, "cannot fork"); else if (dc != 0) { signal(SIGCHLD, sigchld); close(STDOUT_FILENO); dup(p[1]); close(p[0]); close(p[1]); } else { close(STDIN_FILENO); dup(p[0]); close(p[0]); close(p[1]); execl(_PATH_DC, "dc", "-x", (char *)NULL); err(1, "cannot find dc"); } } if (interactive) { el = el_init("bc", stdin, stderr, stderr); hist = history_init(); history(hist, &he, H_SETSIZE, 100); el_set(el, EL_HIST, history, hist); el_set(el, EL_EDITOR, "emacs"); el_set(el, EL_SIGNAL, 1); el_set(el, EL_PROMPT, dummy_prompt); el_source(el, NULL); } yywrap(); return (yyparse()); } Index: head/usr.bin/find/getdate.y =================================================================== --- head/usr.bin/find/getdate.y (revision 235788) +++ head/usr.bin/find/getdate.y (revision 235789) @@ -1,961 +1,959 @@ %{ /* ** Originally written by Steven M. Bellovin while ** at the University of North Carolina at Chapel Hill. Later tweaked by ** a couple of people on Usenet. Completely overhauled by Rich $alz ** and Jim Berets in August, 1990; ** ** This grammar has 10 shift/reduce conflicts. ** ** This code is in the public domain and has no copyright. */ /* SUPPRESS 287 on yaccpar_sccsid *//* Unused static variable */ /* SUPPRESS 288 on yyerrlab *//* Label unused */ #include __FBSDID("$FreeBSD$"); #include #include /* The code at the top of get_date which figures out the offset of the current time zone checks various CPP symbols to see if special tricks are need, but defaults to using the gettimeofday system call. Include if that will be used. */ #if defined(vms) # include #else /* defined(vms) */ # include # include #endif /* !defined(vms) */ #if defined (__STDC__) || defined (USG) #include #endif /* Some old versions of bison generate parsers that use bcopy. That loses on systems that don't provide the function, so we have to redefine it here. */ #if !defined (HAVE_BCOPY) && defined (HAVE_MEMCPY) && !defined (bcopy) #define bcopy(from, to, len) memcpy ((to), (from), (len)) #endif #if defined (__STDC__) #include #endif /* NOTES on rebuilding getdate.c (particularly for inclusion in CVS releases): We don't want to mess with all the portability hassles of alloca. In particular, most (all?) versions of bison will use alloca in their parser. If bison works on your system (e.g. it should work with gcc), then go ahead and use it, but the more general solution is to use byacc instead of bison, which should generate a portable parser. I played with adding "#define alloca dont_use_alloca", to give an error if the parser generator uses alloca (and thus detect unportable getdate.c's), but that seems to cause as many problems as it solves. */ #include -#define yyparse getdate_yyparse #define yylex getdate_yylex #define yyerror getdate_yyerror -static int yyparse(void); static int yylex(void); static int yyerror(const char *); time_t get_date(char *); #define EPOCH 1970 #define HOUR(x) ((time_t)(x) * 60) #define SECSPERDAY (24L * 60L * 60L) /* ** An entry in the lexical lookup table. */ typedef struct _TABLE { const char *name; int type; time_t value; } TABLE; /* ** Daylight-savings mode: on, off, or not yet known. */ typedef enum _DSTMODE { DSTon, DSToff, DSTmaybe } DSTMODE; /* ** Meridian: am, pm, or 24-hour style. */ typedef enum _MERIDIAN { MERam, MERpm, MER24 } MERIDIAN; /* ** Global variables. We could get rid of most of these by using a good ** union as the yacc stack. (This routine was originally written before ** yacc had the %union construct.) Maybe someday; right now we only use ** the %union very rarely. */ static char *yyInput; static DSTMODE yyDSTmode; static time_t yyDayOrdinal; static time_t yyDayNumber; static int yyHaveDate; static int yyHaveDay; static int yyHaveRel; static int yyHaveTime; static int yyHaveZone; static time_t yyTimezone; static time_t yyDay; static time_t yyHour; static time_t yyMinutes; static time_t yyMonth; static time_t yySeconds; static time_t yyYear; static MERIDIAN yyMeridian; static time_t yyRelMonth; static time_t yyRelSeconds; %} %union { time_t Number; enum _MERIDIAN Meridian; } %token tAGO tDAY tDAYZONE tID tMERIDIAN tMINUTE_UNIT tMONTH tMONTH_UNIT %token tSEC_UNIT tSNUMBER tUNUMBER tZONE tDST %type tDAY tDAYZONE tMINUTE_UNIT tMONTH tMONTH_UNIT %type tSEC_UNIT tSNUMBER tUNUMBER tZONE %type tMERIDIAN o_merid %% spec : /* NULL */ | spec item ; item : time { yyHaveTime++; } | zone { yyHaveZone++; } | date { yyHaveDate++; } | day { yyHaveDay++; } | rel { yyHaveRel++; } | number ; time : tUNUMBER tMERIDIAN { yyHour = $1; yyMinutes = 0; yySeconds = 0; yyMeridian = $2; } | tUNUMBER ':' tUNUMBER o_merid { yyHour = $1; yyMinutes = $3; yySeconds = 0; yyMeridian = $4; } | tUNUMBER ':' tUNUMBER tSNUMBER { yyHour = $1; yyMinutes = $3; yyMeridian = MER24; yyDSTmode = DSToff; yyTimezone = - ($4 % 100 + ($4 / 100) * 60); } | tUNUMBER ':' tUNUMBER ':' tUNUMBER o_merid { yyHour = $1; yyMinutes = $3; yySeconds = $5; yyMeridian = $6; } | tUNUMBER ':' tUNUMBER ':' tUNUMBER tSNUMBER { yyHour = $1; yyMinutes = $3; yySeconds = $5; yyMeridian = MER24; yyDSTmode = DSToff; yyTimezone = - ($6 % 100 + ($6 / 100) * 60); } ; zone : tZONE { yyTimezone = $1; yyDSTmode = DSToff; } | tDAYZONE { yyTimezone = $1; yyDSTmode = DSTon; } | tZONE tDST { yyTimezone = $1; yyDSTmode = DSTon; } ; day : tDAY { yyDayOrdinal = 1; yyDayNumber = $1; } | tDAY ',' { yyDayOrdinal = 1; yyDayNumber = $1; } | tUNUMBER tDAY { yyDayOrdinal = $1; yyDayNumber = $2; } ; date : tUNUMBER '/' tUNUMBER { yyMonth = $1; yyDay = $3; } | tUNUMBER '/' tUNUMBER '/' tUNUMBER { if ($1 >= 100) { yyYear = $1; yyMonth = $3; yyDay = $5; } else { yyMonth = $1; yyDay = $3; yyYear = $5; } } | tUNUMBER tSNUMBER tSNUMBER { /* ISO 8601 format. yyyy-mm-dd. */ yyYear = $1; yyMonth = -$2; yyDay = -$3; } | tUNUMBER tMONTH tSNUMBER { /* e.g. 17-JUN-1992. */ yyDay = $1; yyMonth = $2; yyYear = -$3; } | tMONTH tUNUMBER { yyMonth = $1; yyDay = $2; } | tMONTH tUNUMBER ',' tUNUMBER { yyMonth = $1; yyDay = $2; yyYear = $4; } | tUNUMBER tMONTH { yyMonth = $2; yyDay = $1; } | tUNUMBER tMONTH tUNUMBER { yyMonth = $2; yyDay = $1; yyYear = $3; } ; rel : relunit tAGO { yyRelSeconds = -yyRelSeconds; yyRelMonth = -yyRelMonth; } | relunit ; relunit : tUNUMBER tMINUTE_UNIT { yyRelSeconds += $1 * $2 * 60L; } | tSNUMBER tMINUTE_UNIT { yyRelSeconds += $1 * $2 * 60L; } | tMINUTE_UNIT { yyRelSeconds += $1 * 60L; } | tSNUMBER tSEC_UNIT { yyRelSeconds += $1; } | tUNUMBER tSEC_UNIT { yyRelSeconds += $1; } | tSEC_UNIT { yyRelSeconds++; } | tSNUMBER tMONTH_UNIT { yyRelMonth += $1 * $2; } | tUNUMBER tMONTH_UNIT { yyRelMonth += $1 * $2; } | tMONTH_UNIT { yyRelMonth += $1; } ; number : tUNUMBER { if (yyHaveTime && yyHaveDate && !yyHaveRel) yyYear = $1; else { if($1>10000) { yyHaveDate++; yyDay= ($1)%100; yyMonth= ($1/100)%100; yyYear = $1/10000; } else { yyHaveTime++; if ($1 < 100) { yyHour = $1; yyMinutes = 0; } else { yyHour = $1 / 100; yyMinutes = $1 % 100; } yySeconds = 0; yyMeridian = MER24; } } } ; o_merid : /* NULL */ { $$ = MER24; } | tMERIDIAN { $$ = $1; } ; %% /* Month and day table. */ static TABLE const MonthDayTable[] = { { "january", tMONTH, 1 }, { "february", tMONTH, 2 }, { "march", tMONTH, 3 }, { "april", tMONTH, 4 }, { "may", tMONTH, 5 }, { "june", tMONTH, 6 }, { "july", tMONTH, 7 }, { "august", tMONTH, 8 }, { "september", tMONTH, 9 }, { "sept", tMONTH, 9 }, { "october", tMONTH, 10 }, { "november", tMONTH, 11 }, { "december", tMONTH, 12 }, { "sunday", tDAY, 0 }, { "monday", tDAY, 1 }, { "tuesday", tDAY, 2 }, { "tues", tDAY, 2 }, { "wednesday", tDAY, 3 }, { "wednes", tDAY, 3 }, { "thursday", tDAY, 4 }, { "thur", tDAY, 4 }, { "thurs", tDAY, 4 }, { "friday", tDAY, 5 }, { "saturday", tDAY, 6 }, { NULL, 0, 0 } }; /* Time units table. */ static TABLE const UnitsTable[] = { { "year", tMONTH_UNIT, 12 }, { "month", tMONTH_UNIT, 1 }, { "fortnight", tMINUTE_UNIT, 14 * 24 * 60 }, { "week", tMINUTE_UNIT, 7 * 24 * 60 }, { "day", tMINUTE_UNIT, 1 * 24 * 60 }, { "hour", tMINUTE_UNIT, 60 }, { "minute", tMINUTE_UNIT, 1 }, { "min", tMINUTE_UNIT, 1 }, { "second", tSEC_UNIT, 1 }, { "sec", tSEC_UNIT, 1 }, { NULL, 0, 0 } }; /* Assorted relative-time words. */ static TABLE const OtherTable[] = { { "tomorrow", tMINUTE_UNIT, 1 * 24 * 60 }, { "yesterday", tMINUTE_UNIT, -1 * 24 * 60 }, { "today", tMINUTE_UNIT, 0 }, { "now", tMINUTE_UNIT, 0 }, { "last", tUNUMBER, -1 }, { "this", tMINUTE_UNIT, 0 }, { "next", tUNUMBER, 2 }, { "first", tUNUMBER, 1 }, /* { "second", tUNUMBER, 2 }, */ { "third", tUNUMBER, 3 }, { "fourth", tUNUMBER, 4 }, { "fifth", tUNUMBER, 5 }, { "sixth", tUNUMBER, 6 }, { "seventh", tUNUMBER, 7 }, { "eighth", tUNUMBER, 8 }, { "ninth", tUNUMBER, 9 }, { "tenth", tUNUMBER, 10 }, { "eleventh", tUNUMBER, 11 }, { "twelfth", tUNUMBER, 12 }, { "ago", tAGO, 1 }, { NULL, 0, 0 } }; /* The timezone table. */ /* Some of these are commented out because a time_t can't store a float. */ static TABLE const TimezoneTable[] = { { "gmt", tZONE, HOUR( 0) }, /* Greenwich Mean */ { "ut", tZONE, HOUR( 0) }, /* Universal (Coordinated) */ { "utc", tZONE, HOUR( 0) }, { "wet", tZONE, HOUR( 0) }, /* Western European */ { "bst", tDAYZONE, HOUR( 0) }, /* British Summer */ { "wat", tZONE, HOUR( 1) }, /* West Africa */ { "at", tZONE, HOUR( 2) }, /* Azores */ #if 0 /* For completeness. BST is also British Summer, and GST is * also Guam Standard. */ { "bst", tZONE, HOUR( 3) }, /* Brazil Standard */ { "gst", tZONE, HOUR( 3) }, /* Greenland Standard */ #endif #if 0 { "nft", tZONE, HOUR(3.5) }, /* Newfoundland */ { "nst", tZONE, HOUR(3.5) }, /* Newfoundland Standard */ { "ndt", tDAYZONE, HOUR(3.5) }, /* Newfoundland Daylight */ #endif { "ast", tZONE, HOUR( 4) }, /* Atlantic Standard */ { "adt", tDAYZONE, HOUR( 4) }, /* Atlantic Daylight */ { "est", tZONE, HOUR( 5) }, /* Eastern Standard */ { "edt", tDAYZONE, HOUR( 5) }, /* Eastern Daylight */ { "cst", tZONE, HOUR( 6) }, /* Central Standard */ { "cdt", tDAYZONE, HOUR( 6) }, /* Central Daylight */ { "mst", tZONE, HOUR( 7) }, /* Mountain Standard */ { "mdt", tDAYZONE, HOUR( 7) }, /* Mountain Daylight */ { "pst", tZONE, HOUR( 8) }, /* Pacific Standard */ { "pdt", tDAYZONE, HOUR( 8) }, /* Pacific Daylight */ { "yst", tZONE, HOUR( 9) }, /* Yukon Standard */ { "ydt", tDAYZONE, HOUR( 9) }, /* Yukon Daylight */ { "hst", tZONE, HOUR(10) }, /* Hawaii Standard */ { "hdt", tDAYZONE, HOUR(10) }, /* Hawaii Daylight */ { "cat", tZONE, HOUR(10) }, /* Central Alaska */ { "ahst", tZONE, HOUR(10) }, /* Alaska-Hawaii Standard */ { "nt", tZONE, HOUR(11) }, /* Nome */ { "idlw", tZONE, HOUR(12) }, /* International Date Line West */ { "cet", tZONE, -HOUR(1) }, /* Central European */ { "met", tZONE, -HOUR(1) }, /* Middle European */ { "mewt", tZONE, -HOUR(1) }, /* Middle European Winter */ { "mest", tDAYZONE, -HOUR(1) }, /* Middle European Summer */ { "swt", tZONE, -HOUR(1) }, /* Swedish Winter */ { "sst", tDAYZONE, -HOUR(1) }, /* Swedish Summer */ { "fwt", tZONE, -HOUR(1) }, /* French Winter */ { "fst", tDAYZONE, -HOUR(1) }, /* French Summer */ { "eet", tZONE, -HOUR(2) }, /* Eastern Europe, USSR Zone 1 */ { "bt", tZONE, -HOUR(3) }, /* Baghdad, USSR Zone 2 */ #if 0 { "it", tZONE, -HOUR(3.5) },/* Iran */ #endif { "zp4", tZONE, -HOUR(4) }, /* USSR Zone 3 */ { "zp5", tZONE, -HOUR(5) }, /* USSR Zone 4 */ #if 0 { "ist", tZONE, -HOUR(5.5) },/* Indian Standard */ #endif { "zp6", tZONE, -HOUR(6) }, /* USSR Zone 5 */ #if 0 /* For completeness. NST is also Newfoundland Stanard, and SST is * also Swedish Summer. */ { "nst", tZONE, -HOUR(6.5) },/* North Sumatra */ { "sst", tZONE, -HOUR(7) }, /* South Sumatra, USSR Zone 6 */ #endif /* 0 */ { "wast", tZONE, -HOUR(7) }, /* West Australian Standard */ { "wadt", tDAYZONE, -HOUR(7) }, /* West Australian Daylight */ #if 0 { "jt", tZONE, -HOUR(7.5) },/* Java (3pm in Cronusland!) */ #endif { "cct", tZONE, -HOUR(8) }, /* China Coast, USSR Zone 7 */ { "jst", tZONE, -HOUR(9) }, /* Japan Standard, USSR Zone 8 */ #if 0 { "cast", tZONE, -HOUR(9.5) },/* Central Australian Standard */ { "cadt", tDAYZONE, -HOUR(9.5) },/* Central Australian Daylight */ #endif { "east", tZONE, -HOUR(10) }, /* Eastern Australian Standard */ { "eadt", tDAYZONE, -HOUR(10) }, /* Eastern Australian Daylight */ { "gst", tZONE, -HOUR(10) }, /* Guam Standard, USSR Zone 9 */ { "nzt", tZONE, -HOUR(12) }, /* New Zealand */ { "nzst", tZONE, -HOUR(12) }, /* New Zealand Standard */ { "nzdt", tDAYZONE, -HOUR(12) }, /* New Zealand Daylight */ { "idle", tZONE, -HOUR(12) }, /* International Date Line East */ { NULL, 0, 0 } }; /* Military timezone table. */ static TABLE const MilitaryTable[] = { { "a", tZONE, HOUR( 1) }, { "b", tZONE, HOUR( 2) }, { "c", tZONE, HOUR( 3) }, { "d", tZONE, HOUR( 4) }, { "e", tZONE, HOUR( 5) }, { "f", tZONE, HOUR( 6) }, { "g", tZONE, HOUR( 7) }, { "h", tZONE, HOUR( 8) }, { "i", tZONE, HOUR( 9) }, { "k", tZONE, HOUR( 10) }, { "l", tZONE, HOUR( 11) }, { "m", tZONE, HOUR( 12) }, { "n", tZONE, HOUR(- 1) }, { "o", tZONE, HOUR(- 2) }, { "p", tZONE, HOUR(- 3) }, { "q", tZONE, HOUR(- 4) }, { "r", tZONE, HOUR(- 5) }, { "s", tZONE, HOUR(- 6) }, { "t", tZONE, HOUR(- 7) }, { "u", tZONE, HOUR(- 8) }, { "v", tZONE, HOUR(- 9) }, { "w", tZONE, HOUR(-10) }, { "x", tZONE, HOUR(-11) }, { "y", tZONE, HOUR(-12) }, { "z", tZONE, HOUR( 0) }, { NULL, 0, 0 } }; /* ARGSUSED */ static int yyerror(const char *s __unused) { return 0; } static time_t ToSeconds(time_t Hours, time_t Minutes, time_t Seconds, MERIDIAN Meridian) { if (Minutes < 0 || Minutes > 59 || Seconds < 0 || Seconds > 59) return -1; switch (Meridian) { case MER24: if (Hours < 0 || Hours > 23) return -1; return (Hours * 60L + Minutes) * 60L + Seconds; case MERam: if (Hours < 1 || Hours > 12) return -1; if (Hours == 12) Hours = 0; return (Hours * 60L + Minutes) * 60L + Seconds; case MERpm: if (Hours < 1 || Hours > 12) return -1; if (Hours == 12) Hours = 0; return ((Hours + 12) * 60L + Minutes) * 60L + Seconds; default: abort (); } /* NOTREACHED */ } /* Year is either * A negative number, which means to use its absolute value (why?) * A number from 0 to 99, which means a year from 1900 to 1999, or * The actual year (>=100). */ static time_t Convert(time_t Month, time_t Day, time_t Year, time_t Hours, time_t Minutes, time_t Seconds, MERIDIAN Meridian, DSTMODE DSTmode) { static int DaysInMonth[12] = { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; time_t tod; time_t Julian; int i; if (Year < 0) Year = -Year; if (Year < 69) Year += 2000; else if (Year < 100) Year += 1900; DaysInMonth[1] = Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 29 : 28; /* Checking for 2038 bogusly assumes that time_t is 32 bits. But I'm too lazy to try to check for time_t overflow in another way. */ if (Year < EPOCH || Year > 2038 || Month < 1 || Month > 12 /* Lint fluff: "conversion from long may lose accuracy" */ || Day < 1 || Day > DaysInMonth[(int)--Month]) return -1; for (Julian = Day - 1, i = 0; i < Month; i++) Julian += DaysInMonth[i]; for (i = EPOCH; i < Year; i++) Julian += 365 + (i % 4 == 0); Julian *= SECSPERDAY; Julian += yyTimezone * 60L; if ((tod = ToSeconds(Hours, Minutes, Seconds, Meridian)) < 0) return -1; Julian += tod; if (DSTmode == DSTon || (DSTmode == DSTmaybe && localtime(&Julian)->tm_isdst)) Julian -= 60 * 60; return Julian; } static time_t DSTcorrect(time_t Start, time_t Future) { time_t StartDay; time_t FutureDay; StartDay = (localtime(&Start)->tm_hour + 1) % 24; FutureDay = (localtime(&Future)->tm_hour + 1) % 24; return (Future - Start) + (StartDay - FutureDay) * 60L * 60L; } static time_t RelativeDate(time_t Start, time_t DayOrdinal, time_t DayNumber) { struct tm *tm; time_t now; now = Start; tm = localtime(&now); now += SECSPERDAY * ((DayNumber - tm->tm_wday + 7) % 7); now += 7 * SECSPERDAY * (DayOrdinal <= 0 ? DayOrdinal : DayOrdinal - 1); return DSTcorrect(Start, now); } static time_t RelativeMonth(time_t Start, time_t RelMonth) { struct tm *tm; time_t Month; time_t Year; if (RelMonth == 0) return 0; tm = localtime(&Start); Month = 12 * (tm->tm_year + 1900) + tm->tm_mon + RelMonth; Year = Month / 12; Month = Month % 12 + 1; return DSTcorrect(Start, Convert(Month, (time_t)tm->tm_mday, Year, (time_t)tm->tm_hour, (time_t)tm->tm_min, (time_t)tm->tm_sec, MER24, DSTmaybe)); } static int LookupWord(char *buff) { char *p; char *q; const TABLE *tp; int i; int abbrev; /* Make it lowercase. */ for (p = buff; *p; p++) if (isupper(*p)) *p = tolower(*p); if (strcmp(buff, "am") == 0 || strcmp(buff, "a.m.") == 0) { yylval.Meridian = MERam; return tMERIDIAN; } if (strcmp(buff, "pm") == 0 || strcmp(buff, "p.m.") == 0) { yylval.Meridian = MERpm; return tMERIDIAN; } /* See if we have an abbreviation for a month. */ if (strlen(buff) == 3) abbrev = 1; else if (strlen(buff) == 4 && buff[3] == '.') { abbrev = 1; buff[3] = '\0'; } else abbrev = 0; for (tp = MonthDayTable; tp->name; tp++) { if (abbrev) { if (strncmp(buff, tp->name, 3) == 0) { yylval.Number = tp->value; return tp->type; } } else if (strcmp(buff, tp->name) == 0) { yylval.Number = tp->value; return tp->type; } } for (tp = TimezoneTable; tp->name; tp++) if (strcmp(buff, tp->name) == 0) { yylval.Number = tp->value; return tp->type; } if (strcmp(buff, "dst") == 0) return tDST; for (tp = UnitsTable; tp->name; tp++) if (strcmp(buff, tp->name) == 0) { yylval.Number = tp->value; return tp->type; } /* Strip off any plural and try the units table again. */ i = strlen(buff) - 1; if (buff[i] == 's') { buff[i] = '\0'; for (tp = UnitsTable; tp->name; tp++) if (strcmp(buff, tp->name) == 0) { yylval.Number = tp->value; return tp->type; } buff[i] = 's'; /* Put back for "this" in OtherTable. */ } for (tp = OtherTable; tp->name; tp++) if (strcmp(buff, tp->name) == 0) { yylval.Number = tp->value; return tp->type; } /* Military timezones. */ if (buff[1] == '\0' && isalpha(*buff)) { for (tp = MilitaryTable; tp->name; tp++) if (strcmp(buff, tp->name) == 0) { yylval.Number = tp->value; return tp->type; } } /* Drop out any periods and try the timezone table again. */ for (i = 0, p = q = buff; *q; q++) if (*q != '.') *p++ = *q; else i++; *p = '\0'; if (i) for (tp = TimezoneTable; tp->name; tp++) if (strcmp(buff, tp->name) == 0) { yylval.Number = tp->value; return tp->type; } return tID; } static int yylex(void) { char c; char *p; char buff[20]; int Count; int sign; for ( ; ; ) { while (isspace(*yyInput)) yyInput++; if (isdigit(c = *yyInput) || c == '-' || c == '+') { if (c == '-' || c == '+') { sign = c == '-' ? -1 : 1; if (!isdigit(*++yyInput)) /* skip the '-' sign */ continue; } else sign = 0; for (yylval.Number = 0; isdigit(c = *yyInput++); ) yylval.Number = 10 * yylval.Number + c - '0'; yyInput--; if (sign < 0) yylval.Number = -yylval.Number; return sign ? tSNUMBER : tUNUMBER; } if (isalpha(c)) { for (p = buff; isalpha(c = *yyInput++) || c == '.'; ) if (p < &buff[sizeof buff - 1]) *p++ = c; *p = '\0'; yyInput--; return LookupWord(buff); } if (c != '(') return *yyInput++; Count = 0; do { c = *yyInput++; if (c == '\0') return c; if (c == '(') Count++; else if (c == ')') Count--; } while (Count > 0); } } #define TM_YEAR_ORIGIN 1900 /* Yield A - B, measured in seconds. */ static long difftm (struct tm *a, struct tm *b) { int ay = a->tm_year + (TM_YEAR_ORIGIN - 1); int by = b->tm_year + (TM_YEAR_ORIGIN - 1); int days = ( /* difference in day of year */ a->tm_yday - b->tm_yday /* + intervening leap days */ + ((ay >> 2) - (by >> 2)) - (ay/100 - by/100) + ((ay/100 >> 2) - (by/100 >> 2)) /* + difference in years * 365 */ + (long)(ay-by) * 365 ); return (60*(60*(24*days + (a->tm_hour - b->tm_hour)) + (a->tm_min - b->tm_min)) + (a->tm_sec - b->tm_sec)); } time_t get_date(char *p) { struct tm *tm, *gmt_ptr, gmt; int tzoff; time_t Start; time_t tod; time_t nowtime; bzero (&gmt, sizeof(struct tm)); yyInput = p; (void)time (&nowtime); gmt_ptr = gmtime (&nowtime); if (gmt_ptr != NULL) { /* Make a copy, in case localtime modifies *tm (I think that comment now applies to *gmt_ptr, but I am too lazy to dig into how gmtime and locatime allocate the structures they return pointers to). */ gmt = *gmt_ptr; } if (! (tm = localtime (&nowtime))) return -1; if (gmt_ptr != NULL) tzoff = difftm (&gmt, tm) / 60; else /* We are on a system like VMS, where the system clock is in local time and the system has no concept of timezones. Hopefully we can fake this out (for the case in which the user specifies no timezone) by just saying the timezone is zero. */ tzoff = 0; if(tm->tm_isdst) tzoff += 60; tm = localtime(&nowtime); yyYear = tm->tm_year + 1900; yyMonth = tm->tm_mon + 1; yyDay = tm->tm_mday; yyTimezone = tzoff; yyDSTmode = DSTmaybe; yyHour = 0; yyMinutes = 0; yySeconds = 0; yyMeridian = MER24; yyRelSeconds = 0; yyRelMonth = 0; yyHaveDate = 0; yyHaveDay = 0; yyHaveRel = 0; yyHaveTime = 0; yyHaveZone = 0; if (yyparse() || yyHaveTime > 1 || yyHaveZone > 1 || yyHaveDate > 1 || yyHaveDay > 1) return -1; if (yyHaveDate || yyHaveTime || yyHaveDay) { Start = Convert(yyMonth, yyDay, yyYear, yyHour, yyMinutes, yySeconds, yyMeridian, yyDSTmode); if (Start < 0) return -1; } else { Start = nowtime; if (!yyHaveRel) Start -= ((tm->tm_hour * 60L + tm->tm_min) * 60L) + tm->tm_sec; } Start += yyRelSeconds; Start += RelativeMonth(Start, yyRelMonth); if (yyHaveDay && !yyHaveDate) { tod = RelativeDate(Start, yyDayOrdinal, yyDayNumber); Start += tod; } /* Have to do *something* with a legitimate -1 so it's distinguishable * from the error return value. (Alternately could set errno on error.) */ return Start == -1 ? 0 : Start; } #if defined(TEST) /* ARGSUSED */ int main(int ac, char *av[]) { char buff[128]; time_t d; (void)printf("Enter date, or blank line to exit.\n\t> "); (void)fflush(stdout); while (gets(buff) && buff[0]) { d = get_date(buff); if (d == -1) (void)printf("Bad format - couldn't convert.\n"); else (void)printf("%s", ctime(&d)); (void)printf("\t> "); (void)fflush(stdout); } exit(0); /* NOTREACHED */ } #endif /* defined(TEST) */ Index: head/usr.bin/m4/parser.y =================================================================== --- head/usr.bin/m4/parser.y (revision 235788) +++ head/usr.bin/m4/parser.y (revision 235789) @@ -1,86 +1,85 @@ %{ /* $OpenBSD: parser.y,v 1.6 2008/08/21 21:00:14 espie Exp $ */ /* * Copyright (c) 2004 Marc Espie * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * * $FreeBSD$ */ #include #include #define YYSTYPE int32_t extern int32_t end_result; extern int yylex(void); extern int yyerror(const char *); -extern int yyparse(void); %} %token NUMBER %token ERROR %left LOR %left LAND %left '|' %left '^' %left '&' %left EQ NE %left '<' LE '>' GE %left LSHIFT RSHIFT %left '+' '-' %left '*' '/' '%' %right EXPONENT %right UMINUS UPLUS '!' '~' %% top : expr { end_result = $1; } ; expr : expr '+' expr { $$ = $1 + $3; } | expr '-' expr { $$ = $1 - $3; } | expr EXPONENT expr { $$ = pow($1, $3); } | expr '*' expr { $$ = $1 * $3; } | expr '/' expr { if ($3 == 0) { yyerror("division by zero"); exit(1); } $$ = $1 / $3; } | expr '%' expr { if ($3 == 0) { yyerror("modulo zero"); exit(1); } $$ = $1 % $3; } | expr LSHIFT expr { $$ = $1 << $3; } | expr RSHIFT expr { $$ = $1 >> $3; } | expr '<' expr { $$ = $1 < $3; } | expr '>' expr { $$ = $1 > $3; } | expr LE expr { $$ = $1 <= $3; } | expr GE expr { $$ = $1 >= $3; } | expr EQ expr { $$ = $1 == $3; } | expr NE expr { $$ = $1 != $3; } | expr '&' expr { $$ = $1 & $3; } | expr '^' expr { $$ = $1 ^ $3; } | expr '|' expr { $$ = $1 | $3; } | expr LAND expr { $$ = $1 && $3; } | expr LOR expr { $$ = $1 || $3; } | '(' expr ')' { $$ = $2; } | '-' expr %prec UMINUS { $$ = -$2; } | '+' expr %prec UPLUS { $$ = $2; } | '!' expr { $$ = !$2; } | '~' expr { $$ = ~$2; } | NUMBER ; %% Index: head/usr.bin/mkcsmapper/ldef.h =================================================================== --- head/usr.bin/mkcsmapper/ldef.h (revision 235788) +++ head/usr.bin/mkcsmapper/ldef.h (revision 235789) @@ -1,42 +1,41 @@ /* $FreeBSD$ */ /* $NetBSD: ldef.h,v 1.2 2006/09/09 14:35:17 tnozaki Exp $ */ /*- * Copyright (c)2003, 2006 Citrus Project, * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include typedef struct { u_int32_t begin; u_int32_t end; u_int32_t width; } linear_zone_t; extern int debug; extern int line_number; extern int yyerror(const char *); extern int yylex(void); -extern int yyparse(void); Index: head/usr.bin/mklocale/extern.h =================================================================== --- head/usr.bin/mklocale/extern.h (revision 235788) +++ head/usr.bin/mklocale/extern.h (revision 235789) @@ -1,36 +1,35 @@ /*- * Copyright (c) 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Paul Borman at Krystal Technologies. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ int yylex(void); -int yyparse(void); Index: head/usr.sbin/bluetooth/bthidd/parser.y =================================================================== --- head/usr.sbin/bluetooth/bthidd/parser.y (revision 235788) +++ head/usr.sbin/bluetooth/bthidd/parser.y (revision 235789) @@ -1,475 +1,475 @@ %{ /* * parser.y */ /*- * Copyright (c) 2006 Maksim Yevmenkin * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $Id: parser.y,v 1.7 2006/09/07 21:06:53 max Exp $ * $FreeBSD$ */ #include #include #include #include #include #include #include +#include #include #include #include #ifndef BTHIDCONTROL #include #include #define SYSLOG syslog #define LOGCRIT LOG_CRIT #define LOGERR LOG_ERR #define LOGWARNING LOG_WARNING #define EOL #else #define SYSLOG fprintf #define LOGCRIT stderr #define LOGERR stderr #define LOGWARNING stderr #define EOL "\n" #endif /* ndef BTHIDCONTROL */ #include "bthid_config.h" - int yyparse (void); int yylex (void); void yyerror (char const *); static int32_t check_hid_device(hid_device_p hid_device); static void free_hid_device (hid_device_p hid_device); extern FILE *yyin; extern int yylineno; char const *config_file = BTHIDD_CONFFILE; char const *hids_file = BTHIDD_HIDSFILE; static char buffer[1024]; static int32_t hid_descriptor_size; static hid_device_t *hid_device = NULL; static LIST_HEAD(, hid_device) hid_devices; %} %union { bdaddr_t bdaddr; int32_t num; } %token T_BDADDRSTRING %token T_HEXBYTE %token T_DEVICE T_BDADDR T_CONTROL_PSM T_INTERRUPT_PSM T_RECONNECT_INITIATE %token T_BATTERY_POWER T_NORMALLY_CONNECTABLE T_HID_DESCRIPTOR %token T_TRUE T_FALSE T_ERROR %% config: line | config line ; line: T_DEVICE { hid_device = (hid_device_t *) calloc(1, sizeof(*hid_device)); if (hid_device == NULL) { SYSLOG(LOGCRIT, "Could not allocate new " \ "config entry" EOL); YYABORT; } hid_device->new_device = 1; } '{' options '}' { if (check_hid_device(hid_device)) LIST_INSERT_HEAD(&hid_devices,hid_device,next); else free_hid_device(hid_device); hid_device = NULL; } ; options: option ';' | options option ';' ; option: bdaddr | control_psm | interrupt_psm | reconnect_initiate | battery_power | normally_connectable | hid_descriptor | parser_error ; bdaddr: T_BDADDR T_BDADDRSTRING { memcpy(&hid_device->bdaddr, &$2, sizeof(hid_device->bdaddr)); } ; control_psm: T_CONTROL_PSM T_HEXBYTE { hid_device->control_psm = $2; } ; interrupt_psm: T_INTERRUPT_PSM T_HEXBYTE { hid_device->interrupt_psm = $2; } ; reconnect_initiate: T_RECONNECT_INITIATE T_TRUE { hid_device->reconnect_initiate = 1; } | T_RECONNECT_INITIATE T_FALSE { hid_device->reconnect_initiate = 0; } ; battery_power: T_BATTERY_POWER T_TRUE { hid_device->battery_power = 1; } | T_BATTERY_POWER T_FALSE { hid_device->battery_power = 0; } ; normally_connectable: T_NORMALLY_CONNECTABLE T_TRUE { hid_device->normally_connectable = 1; } | T_NORMALLY_CONNECTABLE T_FALSE { hid_device->normally_connectable = 0; } ; hid_descriptor: T_HID_DESCRIPTOR { hid_descriptor_size = 0; } '{' hid_descriptor_bytes '}' { if (hid_device->desc != NULL) hid_dispose_report_desc(hid_device->desc); hid_device->desc = hid_use_report_desc((unsigned char *) buffer, hid_descriptor_size); if (hid_device->desc == NULL) { SYSLOG(LOGCRIT, "Could not use HID descriptor" EOL); YYABORT; } } ; hid_descriptor_bytes: hid_descriptor_byte | hid_descriptor_bytes hid_descriptor_byte ; hid_descriptor_byte: T_HEXBYTE { if (hid_descriptor_size >= (int32_t) sizeof(buffer)) { SYSLOG(LOGCRIT, "HID descriptor is too big" EOL); YYABORT; } buffer[hid_descriptor_size ++] = $1; } ; parser_error: T_ERROR { YYABORT; } %% /* Display parser error message */ void yyerror(char const *message) { SYSLOG(LOGERR, "%s in line %d" EOL, message, yylineno); } /* Re-read config file */ int32_t read_config_file(void) { int32_t e; if (config_file == NULL) { SYSLOG(LOGERR, "Unknown config file name!" EOL); return (-1); } if ((yyin = fopen(config_file, "r")) == NULL) { SYSLOG(LOGERR, "Could not open config file '%s'. %s (%d)" EOL, config_file, strerror(errno), errno); return (-1); } clean_config(); if (yyparse() < 0) { SYSLOG(LOGERR, "Could not parse config file '%s'" EOL, config_file); e = -1; } else e = 0; fclose(yyin); yyin = NULL; return (e); } /* Clean config */ void clean_config(void) { while (!LIST_EMPTY(&hid_devices)) { hid_device_p d = LIST_FIRST(&hid_devices); LIST_REMOVE(d, next); free_hid_device(d); } } /* Lookup config entry */ hid_device_p get_hid_device(bdaddr_p bdaddr) { hid_device_p d; LIST_FOREACH(d, &hid_devices, next) if (memcmp(&d->bdaddr, bdaddr, sizeof(bdaddr_t)) == 0) break; return (d); } /* Get next config entry */ hid_device_p get_next_hid_device(hid_device_p d) { return ((d == NULL)? LIST_FIRST(&hid_devices) : LIST_NEXT(d, next)); } /* Print config entry */ void print_hid_device(hid_device_p d, FILE *f) { /* XXX FIXME hack! */ struct report_desc { unsigned int size; unsigned char data[1]; }; /* XXX FIXME hack! */ struct report_desc *desc = (struct report_desc *) d->desc; uint32_t i; fprintf(f, "device {\n" \ " bdaddr %s;\n" \ " control_psm 0x%x;\n" \ " interrupt_psm 0x%x;\n" \ " reconnect_initiate %s;\n" \ " battery_power %s;\n" \ " normally_connectable %s;\n" \ " hid_descriptor {", bt_ntoa(&d->bdaddr, NULL), d->control_psm, d->interrupt_psm, d->reconnect_initiate? "true" : "false", d->battery_power? "true" : "false", d->normally_connectable? "true" : "false"); for (i = 0; i < desc->size; i ++) { if ((i % 8) == 0) fprintf(f, "\n "); fprintf(f, "0x%2.2x ", desc->data[i]); } fprintf(f, "\n" \ " };\n" \ "}\n"); } /* Check config entry */ static int32_t check_hid_device(hid_device_p d) { hid_data_t hd; hid_item_t hi; int32_t page; if (get_hid_device(&d->bdaddr) != NULL) { SYSLOG(LOGERR, "Ignoring duplicated entry for bdaddr %s" EOL, bt_ntoa(&d->bdaddr, NULL)); return (0); } if (d->control_psm == 0) { SYSLOG(LOGERR, "Ignoring entry with invalid control PSM" EOL); return (0); } if (d->interrupt_psm == 0) { SYSLOG(LOGERR, "Ignoring entry with invalid interrupt PSM" EOL); return (0); } if (d->desc == NULL) { SYSLOG(LOGERR, "Ignoring entry without HID descriptor" EOL); return (0); } /* XXX somehow need to make sure descriptor is valid */ for (hd = hid_start_parse(d->desc, ~0, -1); hid_get_item(hd, &hi) > 0; ) { switch (hi.kind) { case hid_collection: case hid_endcollection: case hid_output: case hid_feature: break; case hid_input: /* Check if the device may send keystrokes */ page = HID_PAGE(hi.usage); if (page == HUP_KEYBOARD) d->keyboard = 1; break; } } hid_end_parse(hd); return (1); } /* Free config entry */ static void free_hid_device(hid_device_p d) { if (d->desc != NULL) hid_dispose_report_desc(d->desc); memset(d, 0, sizeof(*d)); free(d); } /* Re-read hids file */ int32_t read_hids_file(void) { FILE *f; hid_device_t *d; char *line; bdaddr_t bdaddr; int32_t lineno; if (hids_file == NULL) { SYSLOG(LOGERR, "Unknown HIDs file name!" EOL); return (-1); } if ((f = fopen(hids_file, "r")) == NULL) { if (errno == ENOENT) return (0); SYSLOG(LOGERR, "Could not open HIDs file '%s'. %s (%d)" EOL, hids_file, strerror(errno), errno); return (-1); } for (lineno = 1; fgets(buffer, sizeof(buffer), f) != NULL; lineno ++) { if ((line = strtok(buffer, "\r\n\t ")) == NULL) continue; /* ignore empty lines */ if (!bt_aton(line, &bdaddr)) { SYSLOG(LOGWARNING, "Ignoring unparseable BD_ADDR in " \ "%s:%d" EOL, hids_file, lineno); continue; } if ((d = get_hid_device(&bdaddr)) != NULL) d->new_device = 0; } fclose(f); return (0); } /* Write hids file */ int32_t write_hids_file(void) { char path[PATH_MAX]; FILE *f; hid_device_t *d; if (hids_file == NULL) { SYSLOG(LOGERR, "Unknown HIDs file name!" EOL); return (-1); } snprintf(path, sizeof(path), "%s.new", hids_file); if ((f = fopen(path, "w")) == NULL) { SYSLOG(LOGERR, "Could not open HIDs file '%s'. %s (%d)" EOL, path, strerror(errno), errno); return (-1); } LIST_FOREACH(d, &hid_devices, next) if (!d->new_device) fprintf(f, "%s\n", bt_ntoa(&d->bdaddr, NULL)); fclose(f); if (rename(path, hids_file) < 0) { SYSLOG(LOGERR, "Could not rename new HIDs file '%s' to '%s'. " \ "%s (%d)" EOL, path, hids_file, strerror(errno), errno); unlink(path); return (-1); } return (0); } Index: head/usr.sbin/bluetooth/hcsecd/parser.y =================================================================== --- head/usr.sbin/bluetooth/hcsecd/parser.y (revision 235788) +++ head/usr.sbin/bluetooth/hcsecd/parser.y (revision 235789) @@ -1,433 +1,434 @@ %{ /* * parser.y * * Copyright (c) 2001-2002 Maksim Yevmenkin * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $Id: parser.y,v 1.5 2003/06/07 21:22:30 max Exp $ * $FreeBSD$ */ #include #include #include #include #include #include +#include #include #include #include #include #include "hcsecd.h" int yyparse (void); int yylex (void); static void free_key (link_key_p key); static int hexa2int4(char *a); static int hexa2int8(char *a); extern int yylineno; static LIST_HEAD(, link_key) link_keys; char *config_file = "/etc/bluetooth/hcsecd.conf"; static link_key_p key = NULL; %} %union { char *string; } %token T_BDADDRSTRING T_HEXSTRING T_STRING %token T_DEVICE T_BDADDR T_NAME T_KEY T_PIN T_NOKEY T_NOPIN T_JUNK %% config: line | config line ; line: T_DEVICE { key = (link_key_p) malloc(sizeof(*key)); if (key == NULL) { syslog(LOG_ERR, "Could not allocate new " \ "config entry"); exit(1); } memset(key, 0, sizeof(*key)); } '{' options '}' { if (get_key(&key->bdaddr, 1) != NULL) { syslog(LOG_ERR, "Ignoring duplicated entry " \ "for bdaddr %s", bt_ntoa(&key->bdaddr, NULL)); free_key(key); } else LIST_INSERT_HEAD(&link_keys, key, next); key = NULL; } ; options: option ';' | options option ';' ; option: bdaddr | name | key | pin ; bdaddr: T_BDADDR T_BDADDRSTRING { if (!bt_aton($2, &key->bdaddr)) { syslog(LOG_ERR, "Cound not parse BD_ADDR " \ "'%s'", $2); exit(1); } } ; name: T_NAME T_STRING { if (key->name != NULL) free(key->name); key->name = strdup($2); if (key->name == NULL) { syslog(LOG_ERR, "Could not allocate new " \ "device name"); exit(1); } } ; key: T_KEY T_HEXSTRING { int i, len; if (key->key != NULL) free(key->key); key->key = (uint8_t *) malloc(NG_HCI_KEY_SIZE); if (key->key == NULL) { syslog(LOG_ERR, "Could not allocate new " \ "link key"); exit(1); } memset(key->key, 0, NG_HCI_KEY_SIZE); len = strlen($2) / 2; if (len > NG_HCI_KEY_SIZE) len = NG_HCI_KEY_SIZE; for (i = 0; i < len; i ++) key->key[i] = hexa2int8((char *)($2) + 2*i); } | T_KEY T_NOKEY { if (key->key != NULL) free(key->key); key->key = NULL; } ; pin: T_PIN T_STRING { if (key->pin != NULL) free(key->pin); key->pin = strdup($2); if (key->pin == NULL) { syslog(LOG_ERR, "Could not allocate new " \ "PIN code"); exit(1); } } | T_PIN T_NOPIN { if (key->pin != NULL) free(key->pin); key->pin = NULL; } ; %% /* Display parser error message */ void yyerror(char const *message) { syslog(LOG_ERR, "%s in line %d", message, yylineno); } /* Re-read config file */ void read_config_file(void) { extern FILE *yyin; if (config_file == NULL) { syslog(LOG_ERR, "Unknown config file name!"); exit(1); } if ((yyin = fopen(config_file, "r")) == NULL) { syslog(LOG_ERR, "Could not open config file '%s'. %s (%d)", config_file, strerror(errno), errno); exit(1); } clean_config(); if (yyparse() < 0) { syslog(LOG_ERR, "Could not parse config file '%s'",config_file); exit(1); } fclose(yyin); yyin = NULL; #if __config_debug__ dump_config(); #endif } /* Clean config */ void clean_config(void) { link_key_p key = NULL; while ((key = LIST_FIRST(&link_keys)) != NULL) { LIST_REMOVE(key, next); free_key(key); } } /* Find link key entry in the list. Return exact or default match */ link_key_p get_key(bdaddr_p bdaddr, int exact_match) { link_key_p key = NULL, defkey = NULL; LIST_FOREACH(key, &link_keys, next) { if (memcmp(bdaddr, &key->bdaddr, sizeof(key->bdaddr)) == 0) break; if (!exact_match) if (memcmp(NG_HCI_BDADDR_ANY, &key->bdaddr, sizeof(key->bdaddr)) == 0) defkey = key; } return ((key != NULL)? key : defkey); } #if __config_debug__ /* Dump config */ void dump_config(void) { link_key_p key = NULL; char buffer[64]; LIST_FOREACH(key, &link_keys, next) { if (key->key != NULL) snprintf(buffer, sizeof(buffer), "0x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", key->key[0], key->key[1], key->key[2], key->key[3], key->key[4], key->key[5], key->key[6], key->key[7], key->key[8], key->key[9], key->key[10], key->key[11], key->key[12], key->key[13], key->key[14], key->key[15]); syslog(LOG_DEBUG, "device %s " \ "bdaddr %s " \ "pin %s " \ "key %s", (key->name != NULL)? key->name : "noname", bt_ntoa(&key->bdaddr, NULL), (key->pin != NULL)? key->pin : "nopin", (key->key != NULL)? buffer : "nokey"); } } #endif /* Read keys file */ int read_keys_file(void) { FILE *f = NULL; link_key_t *key = NULL; char buf[HCSECD_BUFFER_SIZE], *p = NULL, *cp = NULL; bdaddr_t bdaddr; int i, len; if ((f = fopen(HCSECD_KEYSFILE, "r")) == NULL) { if (errno == ENOENT) return (0); syslog(LOG_ERR, "Could not open keys file %s. %s (%d)\n", HCSECD_KEYSFILE, strerror(errno), errno); return (-1); } while ((p = fgets(buf, sizeof(buf), f)) != NULL) { if (*p == '#') continue; if ((cp = strpbrk(p, " ")) == NULL) continue; *cp++ = '\0'; if (!bt_aton(p, &bdaddr)) continue; if ((key = get_key(&bdaddr, 1)) == NULL) continue; if (key->key == NULL) { key->key = (uint8_t *) malloc(NG_HCI_KEY_SIZE); if (key->key == NULL) { syslog(LOG_ERR, "Could not allocate link key"); exit(1); } } memset(key->key, 0, NG_HCI_KEY_SIZE); len = strlen(cp) / 2; if (len > NG_HCI_KEY_SIZE) len = NG_HCI_KEY_SIZE; for (i = 0; i < len; i ++) key->key[i] = hexa2int8(cp + 2*i); syslog(LOG_DEBUG, "Restored link key for the entry, " \ "remote bdaddr %s, name '%s'", bt_ntoa(&key->bdaddr, NULL), (key->name != NULL)? key->name : "No name"); } fclose(f); return (0); } /* Dump keys file */ int dump_keys_file(void) { link_key_p key = NULL; char tmp[PATH_MAX], buf[HCSECD_BUFFER_SIZE]; int f; snprintf(tmp, sizeof(tmp), "%s.tmp", HCSECD_KEYSFILE); if ((f = open(tmp, O_RDWR|O_CREAT|O_TRUNC|O_EXCL, 0600)) < 0) { syslog(LOG_ERR, "Could not create temp keys file %s. %s (%d)\n", tmp, strerror(errno), errno); return (-1); } LIST_FOREACH(key, &link_keys, next) { if (key->key == NULL) continue; snprintf(buf, sizeof(buf), "%s %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n", bt_ntoa(&key->bdaddr, NULL), key->key[0], key->key[1], key->key[2], key->key[3], key->key[4], key->key[5], key->key[6], key->key[7], key->key[8], key->key[9], key->key[10], key->key[11], key->key[12], key->key[13], key->key[14], key->key[15]); if (write(f, buf, strlen(buf)) < 0) { syslog(LOG_ERR, "Could not write temp keys file. " \ "%s (%d)\n", strerror(errno), errno); break; } } close(f); if (rename(tmp, HCSECD_KEYSFILE) < 0) { syslog(LOG_ERR, "Could not rename(%s, %s). %s (%d)\n", tmp, HCSECD_KEYSFILE, strerror(errno), errno); unlink(tmp); return (-1); } return (0); } /* Free key entry */ static void free_key(link_key_p key) { if (key->name != NULL) free(key->name); if (key->key != NULL) free(key->key); if (key->pin != NULL) free(key->pin); memset(key, 0, sizeof(*key)); free(key); } /* Convert hex ASCII to int4 */ static int hexa2int4(char *a) { if ('0' <= *a && *a <= '9') return (*a - '0'); if ('A' <= *a && *a <= 'F') return (*a - 'A' + 0xa); if ('a' <= *a && *a <= 'f') return (*a - 'a' + 0xa); syslog(LOG_ERR, "Invalid hex character: '%c' (%#x)", *a, *a); exit(1); } /* Convert hex ASCII to int8 */ static int hexa2int8(char *a) { return ((hexa2int4(a) << 4) | hexa2int4(a + 1)); } Index: head/usr.sbin/config/config.h =================================================================== --- head/usr.sbin/config/config.h (revision 235788) +++ head/usr.sbin/config/config.h (revision 235789) @@ -1,205 +1,204 @@ /* * Copyright (c) 1980, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)config.h 8.1 (Berkeley) 6/6/93 * $FreeBSD$ */ /* * Config. */ #include #include #include #include struct cfgfile { STAILQ_ENTRY(cfgfile) cfg_next; char *cfg_path; }; STAILQ_HEAD(, cfgfile) cfgfiles; struct file_list { STAILQ_ENTRY(file_list) f_next; char *f_fn; /* the name */ int f_type; /* type */ u_char f_flags; /* see below */ char *f_compilewith; /* special make rule if present */ char *f_depends; /* additional dependancies */ char *f_clean; /* File list to add to clean rule */ char *f_warn; /* warning message */ const char *f_objprefix; /* prefix string for object name */ }; struct files_name { char *f_name; STAILQ_ENTRY(files_name) f_next; }; /* * Types. */ #define NORMAL 1 #define PROFILING 3 #define NODEPEND 4 #define LOCAL 5 #define DEVDONE 0x80000000 #define TYPEMASK 0x7fffffff /* * Attributes (flags). */ #define NO_IMPLCT_RULE 1 #define NO_OBJ 2 #define BEFORE_DEPEND 4 #define NOWERROR 16 struct device { int d_done; /* processed */ char *d_name; /* name of device (e.g. rk11) */ #define UNKNOWN -2 /* -2 means not set yet */ STAILQ_ENTRY(device) d_next; /* Next one in list */ }; struct config { char *s_sysname; }; /* * Config has a global notion of which machine type is * being used. It uses the name of the machine in choosing * files and directories. Thus if the name of the machine is ``i386'', * it will build from ``Makefile.i386'' and use ``../i386/inline'' * in the makerules, etc. machinearch is the global notion of the * MACHINE_ARCH for this MACHINE. */ char *machinename; char *machinearch; /* * For each machine, a set of CPU's may be specified as supported. * These and the options (below) are put in the C flags in the makefile. */ struct cputype { char *cpu_name; SLIST_ENTRY(cputype) cpu_next; }; SLIST_HEAD(, cputype) cputype; /* * A set of options may also be specified which are like CPU types, * but which may also specify values for the options. * A separate set of options may be defined for make-style options. */ struct opt { char *op_name; char *op_value; int op_ownfile; /* true = own file, false = makefile */ SLIST_ENTRY(opt) op_next; SLIST_ENTRY(opt) op_append; }; SLIST_HEAD(opt_head, opt) opt, mkopt, rmopts; struct opt_list { char *o_name; char *o_file; int o_flags; #define OL_ALIAS 1 SLIST_ENTRY(opt_list) o_next; }; SLIST_HEAD(, opt_list) otab; struct hint { char *hint_name; STAILQ_ENTRY(hint) hint_next; }; STAILQ_HEAD(hint_head, hint) hints; /* * Tag present in the kernelconf.tmlp template file. It's mandatory for those * two strings to be the same. Otherwise you'll get into trouble. */ #define KERNCONFTAG "%%KERNCONFFILE%%" /* * Faked option to note, that the configuration file has been taken from the * kernel file and inclusion of DEFAULTS etc.. isn't nessesery, because we * already have a list of all required devices. */ #define OPT_AUTOGEN "CONFIG_AUTOGENERATED" extern char *ident; extern char *env; extern char kernconfstr[]; extern int do_trace; extern int envmode; extern int hintmode; extern int incignore; char *get_word(FILE *); char *get_quoted_word(FILE *); char *path(const char *); char *raisestr(char *); void remember(const char *); void moveifchanged(const char *, const char *); -int yyparse(void); int yylex(void); void options(void); void makefile(void); void makeenv(void); void makehints(void); void headers(void); void cfgfile_add(const char *); void cfgfile_removeall(void); FILE *open_makefile_template(void); extern STAILQ_HEAD(device_head, device) dtab; extern char errbuf[80]; extern int yyline; extern const char *yyfile; extern STAILQ_HEAD(file_list_head, file_list) ftab; extern STAILQ_HEAD(files_name_head, files_name) fntab; extern int profiling; extern int debugging; extern int found_defaults; extern int maxusers; extern char *PREFIX; /* Config file name - for error messages */ extern char srcdir[]; /* root of the kernel source tree */ #define eq(a,b) (!strcmp(a,b)) #define ns(s) strdup(s) Index: head/usr.sbin/config/main.c =================================================================== --- head/usr.sbin/config/main.c (revision 235788) +++ head/usr.sbin/config/main.c (revision 235789) @@ -1,752 +1,753 @@ /* * Copyright (c) 1980, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef lint static const char copyright[] = "@(#) Copyright (c) 1980, 1993\n\ The Regents of the University of California. All rights reserved.\n"; #endif /* not lint */ #ifndef lint #if 0 static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93"; #endif static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "y.tab.h" #include "config.h" #include "configvers.h" #ifndef TRUE #define TRUE (1) #endif #ifndef FALSE #define FALSE (0) #endif #define CDIR "../compile/" char * PREFIX; char destdir[MAXPATHLEN]; char srcdir[MAXPATHLEN]; int debugging; int profiling; int found_defaults; int incignore; /* * Preserve old behaviour in INCLUDE_CONFIG_FILE handling (files are included * literally). */ int filebased = 0; static void configfile(void); static void get_srcdir(void); static void usage(void); static void cleanheaders(char *); static void kernconfdump(const char *); static void checkversion(void); +extern int yyparse(void); struct hdr_list { char *h_name; struct hdr_list *h_next; } *htab; /* * Config builds a set of files for building a UNIX * system given a description of the desired system. */ int main(int argc, char **argv) { struct stat buf; int ch, len; char *p; char *kernfile; int printmachine; printmachine = 0; kernfile = NULL; while ((ch = getopt(argc, argv, "Cd:gmpVx:")) != -1) switch (ch) { case 'C': filebased = 1; break; case 'm': printmachine = 1; break; case 'd': if (*destdir == '\0') strlcpy(destdir, optarg, sizeof(destdir)); else errx(EXIT_FAILURE, "directory already set"); break; case 'g': debugging++; break; case 'p': profiling++; break; case 'V': printf("%d\n", CONFIGVERS); exit(0); case 'x': kernfile = optarg; break; case '?': default: usage(); } argc -= optind; argv += optind; if (kernfile != NULL) { kernconfdump(kernfile); exit(EXIT_SUCCESS); } if (argc != 1) usage(); PREFIX = *argv; if (stat(PREFIX, &buf) != 0 || !S_ISREG(buf.st_mode)) err(2, "%s", PREFIX); if (freopen("DEFAULTS", "r", stdin) != NULL) { found_defaults = 1; yyfile = "DEFAULTS"; } else { if (freopen(PREFIX, "r", stdin) == NULL) err(2, "%s", PREFIX); yyfile = PREFIX; } if (*destdir != '\0') { len = strlen(destdir); while (len > 1 && destdir[len - 1] == '/') destdir[--len] = '\0'; get_srcdir(); } else { strlcpy(destdir, CDIR, sizeof(destdir)); strlcat(destdir, PREFIX, sizeof(destdir)); } SLIST_INIT(&cputype); SLIST_INIT(&mkopt); SLIST_INIT(&opt); SLIST_INIT(&rmopts); STAILQ_INIT(&cfgfiles); STAILQ_INIT(&dtab); STAILQ_INIT(&fntab); STAILQ_INIT(&ftab); STAILQ_INIT(&hints); if (yyparse()) exit(3); /* * Ensure that required elements (machine, cpu, ident) are present. */ if (machinename == NULL) { printf("Specify machine type, e.g. ``machine i386''\n"); exit(1); } if (ident == NULL) { printf("no ident line specified\n"); exit(1); } if (SLIST_EMPTY(&cputype)) { printf("cpu type must be specified\n"); exit(1); } checkversion(); if (printmachine) { printf("%s\t%s\n",machinename,machinearch); exit(0); } /* Make compile directory */ p = path((char *)NULL); if (stat(p, &buf)) { if (mkdir(p, 0777)) err(2, "%s", p); } else if (!S_ISDIR(buf.st_mode)) errx(EXIT_FAILURE, "%s isn't a directory", p); configfile(); /* put config file into kernel*/ options(); /* make options .h files */ makefile(); /* build Makefile */ makeenv(); /* build env.c */ makehints(); /* build hints.c */ headers(); /* make a lot of .h files */ cleanheaders(p); printf("Kernel build directory is %s\n", p); printf("Don't forget to do ``make cleandepend && make depend''\n"); exit(0); } /* * get_srcdir * determine the root of the kernel source tree * and save that in srcdir. */ static void get_srcdir(void) { struct stat lg, phy; char *p, *pwd; int i; if (realpath("../..", srcdir) == NULL) err(EXIT_FAILURE, "Unable to find root of source tree"); if ((pwd = getenv("PWD")) != NULL && *pwd == '/' && (pwd = strdup(pwd)) != NULL) { /* Remove the last two path components. */ for (i = 0; i < 2; i++) { if ((p = strrchr(pwd, '/')) == NULL) { free(pwd); return; } *p = '\0'; } if (stat(pwd, &lg) != -1 && stat(srcdir, &phy) != -1 && lg.st_dev == phy.st_dev && lg.st_ino == phy.st_ino) strlcpy(srcdir, pwd, MAXPATHLEN); free(pwd); } } static void usage(void) { fprintf(stderr, "usage: config [-CgmpV] [-d destdir] sysname\n"); fprintf(stderr, " config -x kernel\n"); exit(EX_USAGE); } /* * get_word * returns EOF on end of file * NULL on end of line * pointer to the word otherwise */ char * get_word(FILE *fp) { static char line[80]; int ch; char *cp; int escaped_nl = 0; begin: while ((ch = getc(fp)) != EOF) if (ch != ' ' && ch != '\t') break; if (ch == EOF) return ((char *)EOF); if (ch == '\\'){ escaped_nl = 1; goto begin; } if (ch == '\n') { if (escaped_nl){ escaped_nl = 0; goto begin; } else return (NULL); } cp = line; *cp++ = ch; while ((ch = getc(fp)) != EOF) { if (isspace(ch)) break; *cp++ = ch; } *cp = 0; if (ch == EOF) return ((char *)EOF); (void) ungetc(ch, fp); return (line); } /* * get_quoted_word * like get_word but will accept something in double or single quotes * (to allow embedded spaces). */ char * get_quoted_word(FILE *fp) { static char line[256]; int ch; char *cp; int escaped_nl = 0; begin: while ((ch = getc(fp)) != EOF) if (ch != ' ' && ch != '\t') break; if (ch == EOF) return ((char *)EOF); if (ch == '\\'){ escaped_nl = 1; goto begin; } if (ch == '\n') { if (escaped_nl){ escaped_nl = 0; goto begin; } else return (NULL); } cp = line; if (ch == '"' || ch == '\'') { int quote = ch; while ((ch = getc(fp)) != EOF) { if (ch == quote) break; if (ch == '\n') { *cp = 0; printf("config: missing quote reading `%s'\n", line); exit(2); } *cp++ = ch; } } else { *cp++ = ch; while ((ch = getc(fp)) != EOF) { if (isspace(ch)) break; *cp++ = ch; } if (ch != EOF) (void) ungetc(ch, fp); } *cp = 0; if (ch == EOF) return ((char *)EOF); return (line); } /* * prepend the path to a filename */ char * path(const char *file) { char *cp = NULL; if (file) asprintf(&cp, "%s/%s", destdir, file); else cp = strdup(destdir); return (cp); } /* * Generate configuration file based on actual settings. With this mode, user * will be able to obtain and build conifguration file with one command. */ static void configfile_dynamic(struct sbuf *sb) { struct cputype *cput; struct device *d; struct opt *ol; char *lend; unsigned int i; asprintf(&lend, "\\n\\\n"); assert(lend != NULL); sbuf_printf(sb, "options\t%s%s", OPT_AUTOGEN, lend); sbuf_printf(sb, "ident\t%s%s", ident, lend); sbuf_printf(sb, "machine\t%s%s", machinename, lend); SLIST_FOREACH(cput, &cputype, cpu_next) sbuf_printf(sb, "cpu\t%s%s", cput->cpu_name, lend); SLIST_FOREACH(ol, &mkopt, op_next) sbuf_printf(sb, "makeoptions\t%s=%s%s", ol->op_name, ol->op_value, lend); SLIST_FOREACH(ol, &opt, op_next) { if (strncmp(ol->op_name, "DEV_", 4) == 0) continue; sbuf_printf(sb, "options\t%s", ol->op_name); if (ol->op_value != NULL) { sbuf_putc(sb, '='); for (i = 0; i < strlen(ol->op_value); i++) { if (ol->op_value[i] == '"') sbuf_printf(sb, "\\%c", ol->op_value[i]); else sbuf_printf(sb, "%c", ol->op_value[i]); } sbuf_printf(sb, "%s", lend); } else { sbuf_printf(sb, "%s", lend); } } /* * Mark this file as containing everything we need. */ STAILQ_FOREACH(d, &dtab, d_next) sbuf_printf(sb, "device\t%s%s", d->d_name, lend); free(lend); } /* * Generate file from the configuration files. */ static void configfile_filebased(struct sbuf *sb) { FILE *cff; struct cfgfile *cf; int i; /* * Try to read all configuration files. Since those will be present as * C string in the macro, we have to slash their ends then the line * wraps. */ STAILQ_FOREACH(cf, &cfgfiles, cfg_next) { cff = fopen(cf->cfg_path, "r"); if (cff == NULL) { warn("Couldn't open file %s", cf->cfg_path); continue; } while ((i = getc(cff)) != EOF) { if (i == '\n') sbuf_printf(sb, "\\n\\\n"); else if (i == '"' || i == '\'') sbuf_printf(sb, "\\%c", i); else sbuf_putc(sb, i); } fclose(cff); } } static void configfile(void) { FILE *fo; struct sbuf *sb; char *p; /* Add main configuration file to the list of files to be included */ cfgfile_add(PREFIX); p = path("config.c.new"); fo = fopen(p, "w"); if (!fo) err(2, "%s", p); sb = sbuf_new(NULL, NULL, 2048, SBUF_AUTOEXTEND); assert(sb != NULL); sbuf_clear(sb); if (filebased) { /* Is needed, can be used for backward compatibility. */ configfile_filebased(sb); } else { configfile_dynamic(sb); } sbuf_finish(sb); /* * We print first part of the template, replace our tag with * configuration files content and later continue writing our * template. */ p = strstr(kernconfstr, KERNCONFTAG); if (p == NULL) errx(EXIT_FAILURE, "Something went terribly wrong!"); *p = '\0'; fprintf(fo, "%s", kernconfstr); fprintf(fo, "%s", sbuf_data(sb)); p += strlen(KERNCONFTAG); fprintf(fo, "%s", p); sbuf_delete(sb); fclose(fo); moveifchanged(path("config.c.new"), path("config.c")); cfgfile_removeall(); } /* * moveifchanged -- * compare two files; rename if changed. */ void moveifchanged(const char *from_name, const char *to_name) { char *p, *q; int changed; size_t tsize; struct stat from_sb, to_sb; int from_fd, to_fd; changed = 0; if ((from_fd = open(from_name, O_RDONLY)) < 0) err(EX_OSERR, "moveifchanged open(%s)", from_name); if ((to_fd = open(to_name, O_RDONLY)) < 0) changed++; if (!changed && fstat(from_fd, &from_sb) < 0) err(EX_OSERR, "moveifchanged fstat(%s)", from_name); if (!changed && fstat(to_fd, &to_sb) < 0) err(EX_OSERR, "moveifchanged fstat(%s)", to_name); if (!changed && from_sb.st_size != to_sb.st_size) changed++; tsize = (size_t)from_sb.st_size; if (!changed) { p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0); if (p == MAP_FAILED) err(EX_OSERR, "mmap %s", from_name); q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0); if (q == MAP_FAILED) err(EX_OSERR, "mmap %s", to_name); changed = memcmp(p, q, tsize); munmap(p, tsize); munmap(q, tsize); } if (changed) { if (rename(from_name, to_name) < 0) err(EX_OSERR, "rename(%s, %s)", from_name, to_name); } else { if (unlink(from_name) < 0) err(EX_OSERR, "unlink(%s)", from_name); } } static void cleanheaders(char *p) { DIR *dirp; struct dirent *dp; struct file_list *fl; struct hdr_list *hl; size_t len; remember("y.tab.h"); remember("setdefs.h"); STAILQ_FOREACH(fl, &ftab, f_next) remember(fl->f_fn); /* * Scan the build directory and clean out stuff that looks like * it might have been a leftover NFOO header, etc. */ if ((dirp = opendir(p)) == NULL) err(EX_OSERR, "opendir %s", p); while ((dp = readdir(dirp)) != NULL) { len = strlen(dp->d_name); /* Skip non-headers */ if (len < 2 || dp->d_name[len - 2] != '.' || dp->d_name[len - 1] != 'h') continue; /* Skip special stuff, eg: bus_if.h, but check opt_*.h */ if (strchr(dp->d_name, '_') && strncmp(dp->d_name, "opt_", 4) != 0) continue; /* Check if it is a target file */ for (hl = htab; hl != NULL; hl = hl->h_next) { if (eq(dp->d_name, hl->h_name)) { break; } } if (hl) continue; printf("Removing stale header: %s\n", dp->d_name); if (unlink(path(dp->d_name)) == -1) warn("unlink %s", dp->d_name); } (void)closedir(dirp); } void remember(const char *file) { char *s; struct hdr_list *hl; if ((s = strrchr(file, '/')) != NULL) s = ns(s + 1); else s = ns(file); if (strchr(s, '_') && strncmp(s, "opt_", 4) != 0) { free(s); return; } for (hl = htab; hl != NULL; hl = hl->h_next) { if (eq(s, hl->h_name)) { free(s); return; } } hl = calloc(1, sizeof(*hl)); if (hl == NULL) err(EXIT_FAILURE, "calloc"); hl->h_name = s; hl->h_next = htab; htab = hl; } /* * This one is quick hack. Will be probably moved to elf(3) interface. * It takes kernel configuration file name, passes it as an argument to * elfdump -a, which output is parsed by some UNIX tools... */ static void kernconfdump(const char *file) { struct stat st; FILE *fp, *pp; int error, len, osz, r; unsigned int i, off, size, t1, t2, align; char *cmd, *o; r = open(file, O_RDONLY); if (r == -1) err(EXIT_FAILURE, "Couldn't open file '%s'", file); error = fstat(r, &st); if (error == -1) err(EXIT_FAILURE, "fstat() failed"); if (S_ISDIR(st.st_mode)) errx(EXIT_FAILURE, "'%s' is a directory", file); fp = fdopen(r, "r"); if (fp == NULL) err(EXIT_FAILURE, "fdopen() failed"); osz = 1024; o = calloc(1, osz); if (o == NULL) err(EXIT_FAILURE, "Couldn't allocate memory"); /* ELF note section header. */ asprintf(&cmd, "/usr/bin/elfdump -c %s | grep -A 8 kern_conf" "| tail -5 | cut -d ' ' -f 2 | paste - - - - -", file); if (cmd == NULL) errx(EXIT_FAILURE, "asprintf() failed"); pp = popen(cmd, "r"); if (pp == NULL) errx(EXIT_FAILURE, "popen() failed"); free(cmd); len = fread(o, osz, 1, pp); pclose(pp); r = sscanf(o, "%d%d%d%d%d", &off, &size, &t1, &t2, &align); free(o); if (r != 5) errx(EXIT_FAILURE, "File %s doesn't contain configuration " "file. Either unsupported, or not compiled with " "INCLUDE_CONFIG_FILE", file); r = fseek(fp, off, SEEK_CUR); if (r != 0) err(EXIT_FAILURE, "fseek() failed"); for (i = 0; i < size; i++) { r = fgetc(fp); if (r == EOF) break; /* * If '\0' is present in the middle of the configuration * string, this means something very weird is happening. * Make such case very visible. However, some architectures * pad the length of the section with NULs to a multiple of * sh_addralign, allow a NUL in that part of the section. */ if (r == '\0' && (size - i) < align) break; assert(r != '\0' && ("Char present in the configuration " "string mustn't be equal to 0")); fputc(r, stdout); } fclose(fp); } static void badversion(int versreq) { fprintf(stderr, "ERROR: version of config(8) does not match kernel!\n"); fprintf(stderr, "config version = %d, ", CONFIGVERS); fprintf(stderr, "version required = %d\n\n", versreq); fprintf(stderr, "Make sure that /usr/src/usr.sbin/config is in sync\n"); fprintf(stderr, "with your /usr/src/sys and install a new config binary\n"); fprintf(stderr, "before trying this again.\n\n"); fprintf(stderr, "If running the new config fails check your config\n"); fprintf(stderr, "file against the GENERIC or LINT config files for\n"); fprintf(stderr, "changes in config syntax, or option/device naming\n"); fprintf(stderr, "conventions\n\n"); exit(1); } static void checkversion(void) { FILE *ifp; char line[BUFSIZ]; int versreq; ifp = open_makefile_template(); while (fgets(line, BUFSIZ, ifp) != 0) { if (*line != '%') continue; if (strncmp(line, "%VERSREQ=", 9) != 0) continue; versreq = atoi(line + 9); if (MAJOR_VERS(versreq) == MAJOR_VERS(CONFIGVERS) && versreq <= CONFIGVERS) continue; badversion(versreq); } fclose(ifp); } Index: head/usr.sbin/fifolog/lib/getdate.y =================================================================== --- head/usr.sbin/fifolog/lib/getdate.y (revision 235788) +++ head/usr.sbin/fifolog/lib/getdate.y (revision 235789) @@ -1,889 +1,887 @@ %{ /* ** Originally written by Steven M. Bellovin while ** at the University of North Carolina at Chapel Hill. Later tweaked by ** a couple of people on Usenet. Completely overhauled by Rich $alz ** and Jim Berets in August, 1990; ** ** This grammar has 10 shift/reduce conflicts. ** ** This code is in the public domain and has no copyright. ** ** Picked up from CVS and slightly cleaned up by to WARNS=5 level by ** Poul-Henning Kamp ** ** $FreeBSD$ */ #include #include #include #include #include #include #include "libfifolog.h" -#define yyparse getdate_yyparse #define yylex getdate_yylex #define yyerror getdate_yyerror -static int yyparse(void); static int yylex(void); static int yyerror(const char *); #define EPOCH 1970 #define HOUR(x) ((time_t)(x) * 60) #define SECSPERDAY (24L * 60L * 60L) /* ** An entry in the lexical lookup table. */ typedef struct _TABLE { const char *name; int type; time_t value; } TABLE; /* ** Daylight-savings mode: on, off, or not yet known. */ typedef enum _DSTMODE { DSToff, DSTon, DSTmaybe } DSTMODE; /* ** Meridian: am, pm, or 24-hour style. */ typedef enum _MERIDIAN { MERam, MERpm, MER24 } MERIDIAN; /* ** Global variables. We could get rid of most of these by using a good ** union as the yacc stack. (This routine was originally written before ** yacc had the %union construct.) Maybe someday; right now we only use ** the %union very rarely. */ static char *yyInput; static DSTMODE yyDSTmode; static time_t yyDayOrdinal; static time_t yyDayNumber; static int yyHaveDate; static int yyHaveDay; static int yyHaveRel; static int yyHaveTime; static int yyHaveZone; static time_t yyTimezone; static time_t yyDay; static time_t yyHour; static time_t yyMinutes; static time_t yyMonth; static time_t yySeconds; static time_t yyYear; static MERIDIAN yyMeridian; static time_t yyRelMonth; static time_t yyRelSeconds; %} %union { time_t Number; enum _MERIDIAN Meridian; } %token tAGO tDAY tDAYZONE tID tMERIDIAN tMINUTE_UNIT tMONTH tMONTH_UNIT %token tSEC_UNIT tSNUMBER tUNUMBER tZONE tDST %type tDAY tDAYZONE tMINUTE_UNIT tMONTH tMONTH_UNIT %type tSEC_UNIT tSNUMBER tUNUMBER tZONE %type tMERIDIAN o_merid %% spec : /* NULL */ | spec item ; item : time { yyHaveTime++; } | zone { yyHaveZone++; } | date { yyHaveDate++; } | day { yyHaveDay++; } | rel { yyHaveRel++; } | cvsstamp { yyHaveTime++; yyHaveDate++; yyHaveZone++; } | number ; cvsstamp: tUNUMBER '.' tUNUMBER '.' tUNUMBER '.' tUNUMBER '.' tUNUMBER '.' tUNUMBER { yyYear = $1; if (yyYear < 100) yyYear += 1900; yyMonth = $3; yyDay = $5; yyHour = $7; yyMinutes = $9; yySeconds = $11; yyDSTmode = DSToff; yyTimezone = 0; } ; time : tUNUMBER tMERIDIAN { yyHour = $1; yyMinutes = 0; yySeconds = 0; yyMeridian = $2; } | tUNUMBER ':' tUNUMBER o_merid { yyHour = $1; yyMinutes = $3; yySeconds = 0; yyMeridian = $4; } | tUNUMBER ':' tUNUMBER tSNUMBER { yyHour = $1; yyMinutes = $3; yyMeridian = MER24; yyDSTmode = DSToff; yyTimezone = - ($4 % 100 + ($4 / 100) * 60); } | tUNUMBER ':' tUNUMBER ':' tUNUMBER o_merid { yyHour = $1; yyMinutes = $3; yySeconds = $5; yyMeridian = $6; } | tUNUMBER ':' tUNUMBER ':' tUNUMBER tSNUMBER { yyHour = $1; yyMinutes = $3; yySeconds = $5; yyMeridian = MER24; yyDSTmode = DSToff; yyTimezone = - ($6 % 100 + ($6 / 100) * 60); } ; zone : tZONE { yyTimezone = $1; yyDSTmode = DSToff; } | tDAYZONE { yyTimezone = $1; yyDSTmode = DSTon; } | tZONE tDST { yyTimezone = $1; yyDSTmode = DSTon; } ; day : tDAY { yyDayOrdinal = 1; yyDayNumber = $1; } | tDAY ',' { yyDayOrdinal = 1; yyDayNumber = $1; } | tUNUMBER tDAY { yyDayOrdinal = $1; yyDayNumber = $2; } ; date : tUNUMBER '/' tUNUMBER { yyMonth = $1; yyDay = $3; } | tUNUMBER '/' tUNUMBER '/' tUNUMBER { if ($1 >= 100) { yyYear = $1; yyMonth = $3; yyDay = $5; } else { yyMonth = $1; yyDay = $3; yyYear = $5; } } | tUNUMBER tSNUMBER tSNUMBER { /* ISO 8601 format. yyyy-mm-dd. */ yyYear = $1; yyMonth = -$2; yyDay = -$3; } | tUNUMBER tMONTH tSNUMBER { /* e.g. 17-JUN-1992. */ yyDay = $1; yyMonth = $2; yyYear = -$3; } | tMONTH tUNUMBER { yyMonth = $1; yyDay = $2; } | tMONTH tUNUMBER ',' tUNUMBER { yyMonth = $1; yyDay = $2; yyYear = $4; } | tUNUMBER tMONTH { yyMonth = $2; yyDay = $1; } | tUNUMBER tMONTH tUNUMBER { yyMonth = $2; yyDay = $1; yyYear = $3; } ; rel : relunit tAGO { yyRelSeconds = -yyRelSeconds; yyRelMonth = -yyRelMonth; } | relunit ; relunit : tUNUMBER tMINUTE_UNIT { yyRelSeconds += $1 * $2 * 60L; } | tSNUMBER tMINUTE_UNIT { yyRelSeconds += $1 * $2 * 60L; } | tMINUTE_UNIT { yyRelSeconds += $1 * 60L; } | tSNUMBER tSEC_UNIT { yyRelSeconds += $1; } | tUNUMBER tSEC_UNIT { yyRelSeconds += $1; } | tSEC_UNIT { yyRelSeconds++; } | tSNUMBER tMONTH_UNIT { yyRelMonth += $1 * $2; } | tUNUMBER tMONTH_UNIT { yyRelMonth += $1 * $2; } | tMONTH_UNIT { yyRelMonth += $1; } ; number : tUNUMBER { if (yyHaveTime && yyHaveDate && !yyHaveRel) yyYear = $1; else { if($1>10000) { yyHaveDate++; yyDay= ($1)%100; yyMonth= ($1/100)%100; yyYear = $1/10000; } else { yyHaveTime++; if ($1 < 100) { yyHour = $1; yyMinutes = 0; } else { yyHour = $1 / 100; yyMinutes = $1 % 100; } yySeconds = 0; yyMeridian = MER24; } } } ; o_merid : /* NULL */ { $$ = MER24; } | tMERIDIAN { $$ = $1; } ; %% /* Month and day table. */ static TABLE const MonthDayTable[] = { { "january", tMONTH, 1 }, { "february", tMONTH, 2 }, { "march", tMONTH, 3 }, { "april", tMONTH, 4 }, { "may", tMONTH, 5 }, { "june", tMONTH, 6 }, { "july", tMONTH, 7 }, { "august", tMONTH, 8 }, { "september", tMONTH, 9 }, { "sept", tMONTH, 9 }, { "october", tMONTH, 10 }, { "november", tMONTH, 11 }, { "december", tMONTH, 12 }, { "sunday", tDAY, 0 }, { "monday", tDAY, 1 }, { "tuesday", tDAY, 2 }, { "tues", tDAY, 2 }, { "wednesday", tDAY, 3 }, { "wednes", tDAY, 3 }, { "thursday", tDAY, 4 }, { "thur", tDAY, 4 }, { "thurs", tDAY, 4 }, { "friday", tDAY, 5 }, { "saturday", tDAY, 6 }, { NULL, 0, 0 } }; /* Time units table. */ static TABLE const UnitsTable[] = { { "year", tMONTH_UNIT, 12 }, { "month", tMONTH_UNIT, 1 }, { "fortnight", tMINUTE_UNIT, 14 * 24 * 60 }, { "week", tMINUTE_UNIT, 7 * 24 * 60 }, { "day", tMINUTE_UNIT, 1 * 24 * 60 }, { "hour", tMINUTE_UNIT, 60 }, { "minute", tMINUTE_UNIT, 1 }, { "min", tMINUTE_UNIT, 1 }, { "second", tSEC_UNIT, 1 }, { "sec", tSEC_UNIT, 1 }, { NULL, 0, 0 } }; /* Assorted relative-time words. */ static TABLE const OtherTable[] = { { "tomorrow", tMINUTE_UNIT, 1 * 24 * 60 }, { "yesterday", tMINUTE_UNIT, -1 * 24 * 60 }, { "today", tMINUTE_UNIT, 0 }, { "now", tMINUTE_UNIT, 0 }, { "last", tUNUMBER, -1 }, { "this", tMINUTE_UNIT, 0 }, { "next", tUNUMBER, 2 }, { "first", tUNUMBER, 1 }, /* { "second", tUNUMBER, 2 }, */ { "third", tUNUMBER, 3 }, { "fourth", tUNUMBER, 4 }, { "fifth", tUNUMBER, 5 }, { "sixth", tUNUMBER, 6 }, { "seventh", tUNUMBER, 7 }, { "eighth", tUNUMBER, 8 }, { "ninth", tUNUMBER, 9 }, { "tenth", tUNUMBER, 10 }, { "eleventh", tUNUMBER, 11 }, { "twelfth", tUNUMBER, 12 }, { "ago", tAGO, 1 }, { NULL, 0, 0 } }; /* The timezone table. */ /* Some of these are commented out because a time_t can't store a float. */ static TABLE const TimezoneTable[] = { { "gmt", tZONE, HOUR( 0) }, /* Greenwich Mean */ { "ut", tZONE, HOUR( 0) }, /* Universal (Coordinated) */ { "utc", tZONE, HOUR( 0) }, { "wet", tZONE, HOUR( 0) }, /* Western European */ { "bst", tDAYZONE, HOUR( 0) }, /* British Summer */ { "wat", tZONE, HOUR( 1) }, /* West Africa */ { "at", tZONE, HOUR( 2) }, /* Azores */ #if 0 /* For completeness. BST is also British Summer, and GST is * also Guam Standard. */ { "bst", tZONE, HOUR( 3) }, /* Brazil Standard */ { "gst", tZONE, HOUR( 3) }, /* Greenland Standard */ #endif #if 0 { "nft", tZONE, HOUR(3.5) }, /* Newfoundland */ { "nst", tZONE, HOUR(3.5) }, /* Newfoundland Standard */ { "ndt", tDAYZONE, HOUR(3.5) }, /* Newfoundland Daylight */ #endif { "ast", tZONE, HOUR( 4) }, /* Atlantic Standard */ { "adt", tDAYZONE, HOUR( 4) }, /* Atlantic Daylight */ { "est", tZONE, HOUR( 5) }, /* Eastern Standard */ { "edt", tDAYZONE, HOUR( 5) }, /* Eastern Daylight */ { "cst", tZONE, HOUR( 6) }, /* Central Standard */ { "cdt", tDAYZONE, HOUR( 6) }, /* Central Daylight */ { "mst", tZONE, HOUR( 7) }, /* Mountain Standard */ { "mdt", tDAYZONE, HOUR( 7) }, /* Mountain Daylight */ { "pst", tZONE, HOUR( 8) }, /* Pacific Standard */ { "pdt", tDAYZONE, HOUR( 8) }, /* Pacific Daylight */ { "yst", tZONE, HOUR( 9) }, /* Yukon Standard */ { "ydt", tDAYZONE, HOUR( 9) }, /* Yukon Daylight */ { "hst", tZONE, HOUR(10) }, /* Hawaii Standard */ { "hdt", tDAYZONE, HOUR(10) }, /* Hawaii Daylight */ { "cat", tZONE, HOUR(10) }, /* Central Alaska */ { "ahst", tZONE, HOUR(10) }, /* Alaska-Hawaii Standard */ { "nt", tZONE, HOUR(11) }, /* Nome */ { "idlw", tZONE, HOUR(12) }, /* International Date Line West */ { "cet", tZONE, -HOUR(1) }, /* Central European */ { "met", tZONE, -HOUR(1) }, /* Middle European */ { "mewt", tZONE, -HOUR(1) }, /* Middle European Winter */ { "mest", tDAYZONE, -HOUR(1) }, /* Middle European Summer */ { "swt", tZONE, -HOUR(1) }, /* Swedish Winter */ { "sst", tDAYZONE, -HOUR(1) }, /* Swedish Summer */ { "fwt", tZONE, -HOUR(1) }, /* French Winter */ { "fst", tDAYZONE, -HOUR(1) }, /* French Summer */ { "eet", tZONE, -HOUR(2) }, /* Eastern Europe, USSR Zone 1 */ { "bt", tZONE, -HOUR(3) }, /* Baghdad, USSR Zone 2 */ #if 0 { "it", tZONE, -HOUR(3.5) },/* Iran */ #endif { "zp4", tZONE, -HOUR(4) }, /* USSR Zone 3 */ { "zp5", tZONE, -HOUR(5) }, /* USSR Zone 4 */ #if 0 { "ist", tZONE, -HOUR(5.5) },/* Indian Standard */ #endif { "zp6", tZONE, -HOUR(6) }, /* USSR Zone 5 */ #if 0 /* For completeness. NST is also Newfoundland Stanard, and SST is * also Swedish Summer. */ { "nst", tZONE, -HOUR(6.5) },/* North Sumatra */ { "sst", tZONE, -HOUR(7) }, /* South Sumatra, USSR Zone 6 */ #endif /* 0 */ { "wast", tZONE, -HOUR(7) }, /* West Australian Standard */ { "wadt", tDAYZONE, -HOUR(7) }, /* West Australian Daylight */ #if 0 { "jt", tZONE, -HOUR(7.5) },/* Java (3pm in Cronusland!) */ #endif { "cct", tZONE, -HOUR(8) }, /* China Coast, USSR Zone 7 */ { "jst", tZONE, -HOUR(9) }, /* Japan Standard, USSR Zone 8 */ #if 0 { "cast", tZONE, -HOUR(9.5) },/* Central Australian Standard */ { "cadt", tDAYZONE, -HOUR(9.5) },/* Central Australian Daylight */ #endif { "east", tZONE, -HOUR(10) }, /* Eastern Australian Standard */ { "eadt", tDAYZONE, -HOUR(10) }, /* Eastern Australian Daylight */ { "gst", tZONE, -HOUR(10) }, /* Guam Standard, USSR Zone 9 */ { "nzt", tZONE, -HOUR(12) }, /* New Zealand */ { "nzst", tZONE, -HOUR(12) }, /* New Zealand Standard */ { "nzdt", tDAYZONE, -HOUR(12) }, /* New Zealand Daylight */ { "idle", tZONE, -HOUR(12) }, /* International Date Line East */ { NULL, 0, 0 } }; /* Military timezone table. */ static TABLE const MilitaryTable[] = { { "a", tZONE, HOUR( 1) }, { "b", tZONE, HOUR( 2) }, { "c", tZONE, HOUR( 3) }, { "d", tZONE, HOUR( 4) }, { "e", tZONE, HOUR( 5) }, { "f", tZONE, HOUR( 6) }, { "g", tZONE, HOUR( 7) }, { "h", tZONE, HOUR( 8) }, { "i", tZONE, HOUR( 9) }, { "k", tZONE, HOUR( 10) }, { "l", tZONE, HOUR( 11) }, { "m", tZONE, HOUR( 12) }, { "n", tZONE, HOUR(- 1) }, { "o", tZONE, HOUR(- 2) }, { "p", tZONE, HOUR(- 3) }, { "q", tZONE, HOUR(- 4) }, { "r", tZONE, HOUR(- 5) }, { "s", tZONE, HOUR(- 6) }, { "t", tZONE, HOUR(- 7) }, { "u", tZONE, HOUR(- 8) }, { "v", tZONE, HOUR(- 9) }, { "w", tZONE, HOUR(-10) }, { "x", tZONE, HOUR(-11) }, { "y", tZONE, HOUR(-12) }, { "z", tZONE, HOUR( 0) }, { NULL, 0, 0 } }; /* ARGSUSED */ static int yyerror(const char *s __unused) { return 0; } static time_t ToSeconds(time_t Hours, time_t Minutes, time_t Seconds, MERIDIAN Meridian) { if (Minutes < 0 || Minutes > 59 || Seconds < 0 || Seconds > 59) return -1; switch (Meridian) { case MER24: if (Hours < 0 || Hours > 23) return -1; return (Hours * 60L + Minutes) * 60L + Seconds; case MERam: if (Hours < 1 || Hours > 12) return -1; if (Hours == 12) Hours = 0; return (Hours * 60L + Minutes) * 60L + Seconds; case MERpm: if (Hours < 1 || Hours > 12) return -1; if (Hours == 12) Hours = 0; return ((Hours + 12) * 60L + Minutes) * 60L + Seconds; default: abort (); } /* NOTREACHED */ } /* Year is either * A negative number, which means to use its absolute value (why?) * A number from 0 to 99, which means a year from 1900 to 1999, or * The actual year (>=100). */ static time_t Convert(time_t Month, time_t Day, time_t Year, time_t Hours, time_t Minutes, time_t Seconds, MERIDIAN Meridian, DSTMODE DSTmode) { static int DaysInMonth[12] = { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; time_t tod; time_t Julian; int i; struct tm *ltm; if (Year < 0) Year = -Year; if (Year < 69) Year += 2000; else if (Year < 100) Year += 1900; DaysInMonth[1] = Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 29 : 28; /* Checking for 2038 bogusly assumes that time_t is 32 bits. But I'm too lazy to try to check for time_t overflow in another way. */ if (Year < EPOCH || Year > 2038 || Month < 1 || Month > 12 /* Lint fluff: "conversion from long may lose accuracy" */ || Day < 1 || Day > DaysInMonth[(int)--Month]) /* FIXME: * It would be nice to set a global error string here. * "February 30 is not a valid date" is much more informative than * "Can't parse date/time: 100 months" when the user input was * "100 months" and addition resolved that to February 30, for * example. See rcs2-7 in src/sanity.sh for more. */ return -1; for (Julian = Day - 1, i = 0; i < Month; i++) Julian += DaysInMonth[i]; for (i = EPOCH; i < Year; i++) Julian += 365 + (i % 4 == 0); Julian *= SECSPERDAY; Julian += yyTimezone * 60L; if ((tod = ToSeconds(Hours, Minutes, Seconds, Meridian)) < 0) return -1; Julian += tod; ltm = localtime(&Julian); fprintf(stderr, "DST %d TZ %s %d\n", DSTmode, ltm->tm_zone, ltm->tm_isdst); if (DSTmode == DSTon || (DSTmode == DSTmaybe && localtime(&Julian)->tm_isdst)) Julian -= 60 * 60; return Julian; } static time_t DSTcorrect(time_t Start, time_t Future) { time_t StartDay; time_t FutureDay; StartDay = (localtime(&Start)->tm_hour + 1) % 24; FutureDay = (localtime(&Future)->tm_hour + 1) % 24; return (Future - Start) + (StartDay - FutureDay) * 60L * 60L; } static time_t RelativeDate(time_t Start, time_t DayOrdinal, time_t DayNumber) { struct tm *tm; time_t now; now = Start; tm = localtime(&now); now += SECSPERDAY * ((DayNumber - tm->tm_wday + 7) % 7); now += 7 * SECSPERDAY * (DayOrdinal <= 0 ? DayOrdinal : DayOrdinal - 1); return DSTcorrect(Start, now); } static time_t RelativeMonth(time_t Start, time_t RelMonth) { struct tm *tm; time_t Month; time_t Year; if (RelMonth == 0) return 0; tm = localtime(&Start); Month = 12 * (tm->tm_year + 1900) + tm->tm_mon + RelMonth; Year = Month / 12; Month = Month % 12 + 1; return DSTcorrect(Start, Convert(Month, (time_t)tm->tm_mday, Year, (time_t)tm->tm_hour, (time_t)tm->tm_min, (time_t)tm->tm_sec, MER24, DSTmaybe)); } static int LookupWord(char *buff) { char *p; char *q; const TABLE *tp; int i; int abbrev; /* Make it lowercase. */ for (p = buff; *p; p++) if (isupper(*p)) *p = tolower(*p); if (strcmp(buff, "am") == 0 || strcmp(buff, "a.m.") == 0) { yylval.Meridian = MERam; return tMERIDIAN; } if (strcmp(buff, "pm") == 0 || strcmp(buff, "p.m.") == 0) { yylval.Meridian = MERpm; return tMERIDIAN; } /* See if we have an abbreviation for a month. */ if (strlen(buff) == 3) abbrev = 1; else if (strlen(buff) == 4 && buff[3] == '.') { abbrev = 1; buff[3] = '\0'; } else abbrev = 0; for (tp = MonthDayTable; tp->name; tp++) { if (abbrev) { if (strncmp(buff, tp->name, 3) == 0) { yylval.Number = tp->value; return tp->type; } } else if (strcmp(buff, tp->name) == 0) { yylval.Number = tp->value; return tp->type; } } for (tp = TimezoneTable; tp->name; tp++) if (strcmp(buff, tp->name) == 0) { yylval.Number = tp->value; return tp->type; } if (strcmp(buff, "dst") == 0) return tDST; for (tp = UnitsTable; tp->name; tp++) if (strcmp(buff, tp->name) == 0) { yylval.Number = tp->value; return tp->type; } /* Strip off any plural and try the units table again. */ i = strlen(buff) - 1; if (buff[i] == 's') { buff[i] = '\0'; for (tp = UnitsTable; tp->name; tp++) if (strcmp(buff, tp->name) == 0) { yylval.Number = tp->value; return tp->type; } buff[i] = 's'; /* Put back for "this" in OtherTable. */ } for (tp = OtherTable; tp->name; tp++) if (strcmp(buff, tp->name) == 0) { yylval.Number = tp->value; return tp->type; } /* Military timezones. */ if (buff[1] == '\0' && isalpha(*buff)) { for (tp = MilitaryTable; tp->name; tp++) if (strcmp(buff, tp->name) == 0) { yylval.Number = tp->value; return tp->type; } } /* Drop out any periods and try the timezone table again. */ for (i = 0, p = q = buff; *q; q++) if (*q != '.') *p++ = *q; else i++; *p = '\0'; if (i) for (tp = TimezoneTable; tp->name; tp++) if (strcmp(buff, tp->name) == 0) { yylval.Number = tp->value; return tp->type; } return tID; } static int yylex(void) { char c; char *p; char buff[20]; int Count; int sign; for ( ; ; ) { while (isspace(*yyInput)) yyInput++; if (isdigit(c = *yyInput) || c == '-' || c == '+') { if (c == '-' || c == '+') { sign = c == '-' ? -1 : 1; if (!isdigit(*++yyInput)) /* skip the '-' sign */ continue; } else sign = 0; for (yylval.Number = 0; isdigit(c = *yyInput++); ) yylval.Number = 10 * yylval.Number + c - '0'; yyInput--; if (sign < 0) yylval.Number = -yylval.Number; return sign ? tSNUMBER : tUNUMBER; } if (isalpha(c)) { for (p = buff; isalpha(c = *yyInput++) || c == '.'; ) if (p < &buff[sizeof buff - 1]) *p++ = c; *p = '\0'; yyInput--; return LookupWord(buff); } if (c != '(') return *yyInput++; Count = 0; do { c = *yyInput++; if (c == '\0') return c; if (c == '(') Count++; else if (c == ')') Count--; } while (Count > 0); } } #define TM_YEAR_ORIGIN 1900 time_t get_date(char *p) { struct tm *tm, gmt; time_t Start; time_t tod; time_t nowtime; struct tm *gmt_ptr; yyInput = p; (void)time (&nowtime); gmt_ptr = gmtime (&nowtime); if (gmt_ptr != NULL) { /* Make a copy, in case localtime modifies *tm (I think that comment now applies to *gmt_ptr, but I am too lazy to dig into how gmtime and locatime allocate the structures they return pointers to). */ gmt = *gmt_ptr; } if (! (tm = localtime (&nowtime))) return -1; tm = localtime(&nowtime); yyYear = tm->tm_year + 1900; yyMonth = tm->tm_mon + 1; yyDay = tm->tm_mday; yyTimezone = tm->tm_gmtoff; yyDSTmode = DSTmaybe; yyHour = 0; yyMinutes = 0; yySeconds = 0; yyMeridian = MER24; yyRelSeconds = 0; yyRelMonth = 0; yyHaveDate = 0; yyHaveDay = 0; yyHaveRel = 0; yyHaveTime = 0; yyHaveZone = 0; if (yyparse() || yyHaveTime > 1 || yyHaveZone > 1 || yyHaveDate > 1 || yyHaveDay > 1) return -1; if (yyHaveDate || yyHaveTime || yyHaveDay) { Start = Convert(yyMonth, yyDay, yyYear, yyHour, yyMinutes, yySeconds, yyMeridian, yyDSTmode); if (Start < 0) return -1; } else { Start = nowtime; if (!yyHaveRel) Start -= ((tm->tm_hour * 60L + tm->tm_min) * 60L) + tm->tm_sec; } Start += yyRelSeconds; Start += RelativeMonth(Start, yyRelMonth); if (yyHaveDay && !yyHaveDate) { tod = RelativeDate(Start, yyDayOrdinal, yyDayNumber); Start += tod; } /* Have to do *something* with a legitimate -1 so it's distinguishable * from the error return value. (Alternately could set errno on error.) */ return Start == -1 ? 0 : Start; } Index: head/usr.sbin/jail/config.c =================================================================== --- head/usr.sbin/jail/config.c (revision 235788) +++ head/usr.sbin/jail/config.c (revision 235789) @@ -1,831 +1,833 @@ /*- * Copyright (c) 2011 James Gritton * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include "jailp.h" struct ipspec { const char *name; unsigned flags; }; extern FILE *yyin; extern int yynerrs; +extern int yyparse(void); + struct cfjails cfjails = TAILQ_HEAD_INITIALIZER(cfjails); static void free_param(struct cfparams *pp, struct cfparam *p); static void free_param_strings(struct cfparam *p); static const struct ipspec intparams[] = { [IP_ALLOW_DYING] = {"allow.dying", PF_INTERNAL | PF_BOOL}, [IP_COMMAND] = {"command", PF_INTERNAL}, [IP_DEPEND] = {"depend", PF_INTERNAL}, [IP_EXEC_CLEAN] = {"exec.clean", PF_INTERNAL | PF_BOOL}, [IP_EXEC_CONSOLELOG] = {"exec.consolelog", PF_INTERNAL}, [IP_EXEC_FIB] = {"exec.fib", PF_INTERNAL | PF_INT}, [IP_EXEC_JAIL_USER] = {"exec.jail_user", PF_INTERNAL}, [IP_EXEC_POSTSTART] = {"exec.poststart", PF_INTERNAL}, [IP_EXEC_POSTSTOP] = {"exec.poststop", PF_INTERNAL}, [IP_EXEC_PRESTART] = {"exec.prestart", PF_INTERNAL}, [IP_EXEC_PRESTOP] = {"exec.prestop", PF_INTERNAL}, [IP_EXEC_START] = {"exec.start", PF_INTERNAL}, [IP_EXEC_STOP] = {"exec.stop", PF_INTERNAL}, [IP_EXEC_SYSTEM_JAIL_USER]= {"exec.system_jail_user", PF_INTERNAL | PF_BOOL}, [IP_EXEC_SYSTEM_USER] = {"exec.system_user", PF_INTERNAL}, [IP_EXEC_TIMEOUT] = {"exec.timeout", PF_INTERNAL | PF_INT}, #if defined(INET) || defined(INET6) [IP_INTERFACE] = {"interface", PF_INTERNAL}, [IP_IP_HOSTNAME] = {"ip_hostname", PF_INTERNAL | PF_BOOL}, #endif [IP_MOUNT] = {"mount", PF_INTERNAL}, [IP_MOUNT_DEVFS] = {"mount.devfs", PF_INTERNAL | PF_BOOL}, [IP_MOUNT_FSTAB] = {"mount.fstab", PF_INTERNAL}, [IP_STOP_TIMEOUT] = {"stop.timeout", PF_INTERNAL | PF_INT}, [IP_VNET_INTERFACE] = {"vnet.interface", PF_INTERNAL}, #ifdef INET [IP__IP4_IFADDR] = {"ip4.addr", PF_INTERNAL | PF_CONV}, #endif #ifdef INET6 [IP__IP6_IFADDR] = {"ip6.addr", PF_INTERNAL | PF_CONV}, #endif [IP__MOUNT_FROM_FSTAB] = {"mount.fstab", PF_INTERNAL | PF_CONV}, [IP__OP] = {NULL, PF_CONV}, [KP_ALLOW_CHFLAGS] = {"allow.chflags", 0}, [KP_ALLOW_MOUNT] = {"allow.mount", 0}, [KP_ALLOW_RAW_SOCKETS] = {"allow.raw_sockets", 0}, [KP_ALLOW_SET_HOSTNAME]= {"allow.set_hostname", 0}, [KP_ALLOW_SOCKET_AF] = {"allow.socket_af", 0}, [KP_ALLOW_SYSVIPC] = {"allow.sysvipc", 0}, [KP_DEVFS_RULESET] = {"devfs_ruleset", 0}, [KP_ENFORCE_STATFS] = {"enforce_statfs", 0}, [KP_HOST_HOSTNAME] = {"host.hostname", 0}, #ifdef INET [KP_IP4_ADDR] = {"ip4.addr", 0}, #endif #ifdef INET6 [KP_IP6_ADDR] = {"ip6.addr", 0}, #endif [KP_JID] = {"jid", 0}, [KP_NAME] = {"name", 0}, [KP_PATH] = {"path", 0}, [KP_PERSIST] = {"persist", 0}, [KP_SECURELEVEL] = {"securelevel", 0}, [KP_VNET] = {"vnet", 0}, }; /* * Parse the jail configuration file. */ void load_config(void) { struct cfjails wild; struct cfparams opp; struct cfjail *j, *tj, *wj; struct cfparam *p, *vp, *tp; struct cfstring *s, *vs, *ns; struct cfvar *v; char *ep; size_t varoff; int did_self, jseq, pgen; if (!strcmp(cfname, "-")) { cfname = "STDIN"; yyin = stdin; } else { yyin = fopen(cfname, "r"); if (!yyin) err(1, "%s", cfname); } if (yyparse() || yynerrs) exit(1); /* Separate the wildcard jails out from the actual jails. */ jseq = 0; TAILQ_INIT(&wild); TAILQ_FOREACH_SAFE(j, &cfjails, tq, tj) { j->seq = ++jseq; if (wild_jail_name(j->name)) requeue(j, &wild); } TAILQ_FOREACH(j, &cfjails, tq) { /* Set aside the jail's parameters. */ TAILQ_INIT(&opp); TAILQ_CONCAT(&opp, &j->params, tq); /* * The jail name implies its "name" or "jid" parameter, * though they may also be explicitly set later on. */ add_param(j, NULL, strtol(j->name, &ep, 10) && !*ep ? KP_JID : KP_NAME, j->name); /* * Collect parameters for the jail, global parameters/variables, * and any matching wildcard jails. */ did_self = 0; TAILQ_FOREACH(wj, &wild, tq) { if (j->seq < wj->seq && !did_self) { TAILQ_FOREACH(p, &opp, tq) add_param(j, p, 0, NULL); did_self = 1; } if (wild_jail_match(j->name, wj->name)) TAILQ_FOREACH(p, &wj->params, tq) add_param(j, p, 0, NULL); } if (!did_self) TAILQ_FOREACH(p, &opp, tq) add_param(j, p, 0, NULL); /* Resolve any variable substitutions. */ pgen = 0; TAILQ_FOREACH(p, &j->params, tq) { p->gen = ++pgen; find_vars: TAILQ_FOREACH(s, &p->val, tq) { varoff = 0; while ((v = STAILQ_FIRST(&s->vars))) { TAILQ_FOREACH(vp, &j->params, tq) if (!strcmp(vp->name, v->name)) break; if (!vp) { jail_warnx(j, "%s: variable \"%s\" not found", p->name, v->name); bad_var: j->flags |= JF_FAILED; TAILQ_FOREACH(vp, &j->params, tq) if (vp->gen == pgen) vp->flags |= PF_BAD; goto free_var; } if (vp->flags & PF_BAD) goto bad_var; if (vp->gen == pgen) { jail_warnx(j, "%s: variable loop", v->name); goto bad_var; } TAILQ_FOREACH(vs, &vp->val, tq) if (!STAILQ_EMPTY(&vs->vars)) { vp->gen = pgen; TAILQ_REMOVE(&j->params, vp, tq); TAILQ_INSERT_BEFORE(p, vp, tq); p = vp; goto find_vars; } vs = TAILQ_FIRST(&vp->val); if (TAILQ_NEXT(vs, tq) != NULL && (s->s[0] != '\0' || STAILQ_NEXT(v, tq))) { jail_warnx(j, "%s: array cannot be " "substituted inline", p->name); goto bad_var; } s->s = erealloc(s->s, s->len + vs->len + 1); memmove(s->s + v->pos + varoff + vs->len, s->s + v->pos + varoff, s->len - (v->pos + varoff) + 1); memcpy(s->s + v->pos + varoff, vs->s, vs->len); varoff += vs->len; s->len += vs->len; while ((vs = TAILQ_NEXT(vs, tq))) { ns = emalloc(sizeof(struct cfstring)); ns->s = estrdup(vs->s); ns->len = vs->len; STAILQ_INIT(&ns->vars); TAILQ_INSERT_AFTER(&p->val, s, ns, tq); s = ns; } free_var: free(v->name); STAILQ_REMOVE_HEAD(&s->vars, tq); free(v); } } } /* Free the jail's original parameter list and any variables. */ while ((p = TAILQ_FIRST(&opp))) free_param(&opp, p); TAILQ_FOREACH_SAFE(p, &j->params, tq, tp) if (p->flags & PF_VAR) free_param(&j->params, p); } while ((wj = TAILQ_FIRST(&wild))) { free(wj->name); while ((p = TAILQ_FIRST(&wj->params))) free_param(&wj->params, p); TAILQ_REMOVE(&wild, wj, tq); } } /* * Create a new jail record. */ struct cfjail * add_jail(void) { struct cfjail *j; j = emalloc(sizeof(struct cfjail)); memset(j, 0, sizeof(struct cfjail)); TAILQ_INIT(&j->params); STAILQ_INIT(&j->dep[DEP_FROM]); STAILQ_INIT(&j->dep[DEP_TO]); j->queue = &cfjails; TAILQ_INSERT_TAIL(&cfjails, j, tq); return j; } /* * Add a parameter to a jail. */ void add_param(struct cfjail *j, const struct cfparam *p, enum intparam ipnum, const char *value) { struct cfstrings nss; struct cfparam *dp, *np; struct cfstring *s, *ns; struct cfvar *v, *nv; const char *name; char *cs, *tname; unsigned flags; if (j == NULL) { /* Create a single anonymous jail if one doesn't yet exist. */ j = TAILQ_LAST(&cfjails, cfjails); if (j == NULL) j = add_jail(); } TAILQ_INIT(&nss); if (p != NULL) { name = p->name; flags = p->flags; /* * Make a copy of the parameter's string list, * which may be freed if it's overridden later. */ TAILQ_FOREACH(s, &p->val, tq) { ns = emalloc(sizeof(struct cfstring)); ns->s = estrdup(s->s); ns->len = s->len; STAILQ_INIT(&ns->vars); STAILQ_FOREACH(v, &s->vars, tq) { nv = emalloc(sizeof(struct cfvar)); nv->name = strdup(v->name); nv->pos = v->pos; STAILQ_INSERT_TAIL(&ns->vars, nv, tq); } TAILQ_INSERT_TAIL(&nss, ns, tq); } } else { flags = PF_APPEND; if (ipnum != IP__NULL) { name = intparams[ipnum].name; flags |= intparams[ipnum].flags; } else if ((cs = strchr(value, '='))) { tname = alloca(cs - value + 1); strlcpy(tname, value, cs - value + 1); name = tname; value = cs + 1; } else { name = value; value = NULL; } if (value != NULL) { ns = emalloc(sizeof(struct cfstring)); ns->s = estrdup(value); ns->len = strlen(value); STAILQ_INIT(&ns->vars); TAILQ_INSERT_TAIL(&nss, ns, tq); } } /* See if this parameter has already been added. */ if (ipnum != IP__NULL) dp = j->intparams[ipnum]; else TAILQ_FOREACH(dp, &j->params, tq) if (!(dp->flags & PF_CONV) && equalopts(dp->name, name)) break; if (dp != NULL) { /* Found it - append or replace. */ if (strcmp(dp->name, name)) { free(dp->name); dp->name = estrdup(name); } if (!(flags & PF_APPEND) || TAILQ_EMPTY(&nss)) free_param_strings(dp); TAILQ_CONCAT(&dp->val, &nss, tq); dp->flags |= flags; } else { /* Not found - add it. */ np = emalloc(sizeof(struct cfparam)); np->name = estrdup(name); TAILQ_INIT(&np->val); TAILQ_CONCAT(&np->val, &nss, tq); np->flags = flags; np->gen = 0; TAILQ_INSERT_TAIL(&j->params, np, tq); if (ipnum != IP__NULL) j->intparams[ipnum] = np; else for (ipnum = IP__NULL + 1; ipnum < IP_NPARAM; ipnum++) if (!(intparams[ipnum].flags & PF_CONV) && equalopts(name, intparams[ipnum].name)) { j->intparams[ipnum] = np; np->flags |= intparams[ipnum].flags; break; } } } /* * Return if a boolean parameter exists and is true. */ int bool_param(const struct cfparam *p) { const char *cs; if (p == NULL) return 0; cs = strrchr(p->name, '.'); return !strncmp(cs ? cs + 1 : p->name, "no", 2) ^ (TAILQ_EMPTY(&p->val) || !strcasecmp(TAILQ_LAST(&p->val, cfstrings)->s, "true") || (strtol(TAILQ_LAST(&p->val, cfstrings)->s, NULL, 10))); } /* * Set an integer if a parameter if it exists. */ int int_param(const struct cfparam *p, int *ip) { if (p == NULL || TAILQ_EMPTY(&p->val)) return 0; *ip = strtol(TAILQ_LAST(&p->val, cfstrings)->s, NULL, 10); return 1; } /* * Return the string value of a scalar parameter if it exists. */ const char * string_param(const struct cfparam *p) { return (p && !TAILQ_EMPTY(&p->val) ? TAILQ_LAST(&p->val, cfstrings)->s : NULL); } /* * Check syntax and values of internal parameters. Set some internal * parameters based on the values of others. */ int check_intparams(struct cfjail *j) { struct cfparam *p; struct cfstring *s; FILE *f; const char *val; char *cs, *ep, *ln; size_t lnlen; int error; #if defined(INET) || defined(INET6) struct addrinfo hints; struct addrinfo *ai0, *ai; const char *hostname; int gicode, defif, prefix; #endif #ifdef INET struct in_addr addr4; int ip4ok; char avalue4[INET_ADDRSTRLEN]; #endif #ifdef INET6 struct in6_addr addr6; int ip6ok; char avalue6[INET6_ADDRSTRLEN]; #endif error = 0; /* Check format of boolan and integer values. */ TAILQ_FOREACH(p, &j->params, tq) { if (!TAILQ_EMPTY(&p->val) && (p->flags & (PF_BOOL | PF_INT))) { val = TAILQ_LAST(&p->val, cfstrings)->s; if (p->flags & PF_BOOL) { if (strcasecmp(val, "false") && strcasecmp(val, "true") && ((void)strtol(val, &ep, 10), *ep)) { jail_warnx(j, "%s: unknown boolean value \"%s\"", p->name, val); error = -1; } } else { (void)strtol(val, &ep, 10); if (ep == val || *ep) { jail_warnx(j, "%s: non-integer value \"%s\"", p->name, val); error = -1; } } } } #if defined(INET) || defined(INET6) /* * The ip_hostname parameter looks up the hostname, and adds parameters * for any IP addresses it finds. */ if (((j->flags & JF_OP_MASK) != JF_STOP || j->intparams[IP_INTERFACE] != NULL) && bool_param(j->intparams[IP_IP_HOSTNAME]) && (hostname = string_param(j->intparams[KP_HOST_HOSTNAME]))) { j->intparams[IP_IP_HOSTNAME] = NULL; /* * Silently ignore unsupported address families from * DNS lookups. */ #ifdef INET ip4ok = feature_present("inet"); #endif #ifdef INET6 ip6ok = feature_present("inet6"); #endif if ( #if defined(INET) && defined(INET6) ip4ok || ip6ok #elif defined(INET) ip4ok #elif defined(INET6) ip6ok #endif ) { /* Look up the hostname (or get the address) */ memset(&hints, 0, sizeof(hints)); hints.ai_socktype = SOCK_STREAM; hints.ai_family = #if defined(INET) && defined(INET6) ip4ok ? (ip6ok ? PF_UNSPEC : PF_INET) : PF_INET6; #elif defined(INET) PF_INET; #elif defined(INET6) PF_INET6; #endif gicode = getaddrinfo(hostname, NULL, &hints, &ai0); if (gicode != 0) { jail_warnx(j, "host.hostname %s: %s", hostname, gai_strerror(gicode)); error = -1; } else { /* * Convert the addresses to ASCII so jailparam * can convert them back. Errors are not * expected here. */ for (ai = ai0; ai; ai = ai->ai_next) switch (ai->ai_family) { #ifdef INET case AF_INET: memcpy(&addr4, &((struct sockaddr_in *) (void *)ai->ai_addr)-> sin_addr, sizeof(addr4)); if (inet_ntop(AF_INET, &addr4, avalue4, INET_ADDRSTRLEN) == NULL) err(1, "inet_ntop"); add_param(j, NULL, KP_IP4_ADDR, avalue4); break; #endif #ifdef INET6 case AF_INET6: memcpy(&addr6, &((struct sockaddr_in6 *) (void *)ai->ai_addr)-> sin6_addr, sizeof(addr6)); if (inet_ntop(AF_INET6, &addr6, avalue6, INET6_ADDRSTRLEN) == NULL) err(1, "inet_ntop"); add_param(j, NULL, KP_IP6_ADDR, avalue6); break; #endif } freeaddrinfo(ai0); } } } /* * IP addresses may include an interface to set that address on, * and a netmask/suffix for that address. */ defif = string_param(j->intparams[IP_INTERFACE]) != NULL; #ifdef INET if (j->intparams[KP_IP4_ADDR] != NULL) { TAILQ_FOREACH(s, &j->intparams[KP_IP4_ADDR]->val, tq) { cs = strchr(s->s, '|'); if (cs || defif) add_param(j, NULL, IP__IP4_IFADDR, s->s); if (cs) { strcpy(s->s, cs + 1); s->len -= cs + 1 - s->s; } if ((cs = strchr(s->s, '/'))) { prefix = strtol(cs + 1, &ep, 10); if (*ep == '.' ? inet_pton(AF_INET, cs + 1, &addr4) != 1 : *ep || prefix < 0 || prefix > 32) { jail_warnx(j, "ip4.addr: bad netmask \"%s\"", cs); error = -1; } *cs = '\0'; s->len = cs - s->s + 1; } } } #endif #ifdef INET6 if (j->intparams[KP_IP6_ADDR] != NULL) { TAILQ_FOREACH(s, &j->intparams[KP_IP6_ADDR]->val, tq) { cs = strchr(s->s, '|'); if (cs || defif) add_param(j, NULL, IP__IP6_IFADDR, s->s); if (cs) { strcpy(s->s, cs + 1); s->len -= cs + 1 - s->s; } if ((cs = strchr(s->s, '/'))) { prefix = strtol(cs + 1, &ep, 10); if (*ep || prefix < 0 || prefix > 128) { jail_warnx(j, "ip6.addr: bad prefixlen \"%s\"", cs); error = -1; } *cs = '\0'; s->len = cs - s->s + 1; } } } #endif #endif /* * Read mount.fstab file(s), and treat each line as its own mount * parameter. */ if (j->intparams[IP_MOUNT_FSTAB] != NULL) { TAILQ_FOREACH(s, &j->intparams[IP_MOUNT_FSTAB]->val, tq) { if (s->len == 0) continue; f = fopen(s->s, "r"); if (f == NULL) { jail_warnx(j, "mount.fstab: %s: %s", s->s, strerror(errno)); error = -1; continue; } while ((ln = fgetln(f, &lnlen))) { if ((cs = memchr(ln, '#', lnlen - 1))) lnlen = cs - ln + 1; if (ln[lnlen - 1] == '\n' || ln[lnlen - 1] == '#') ln[lnlen - 1] = '\0'; else { cs = alloca(lnlen + 1); strlcpy(cs, ln, lnlen + 1); ln = cs; } add_param(j, NULL, IP__MOUNT_FROM_FSTAB, ln); } fclose(f); } } if (error) failed(j); return error; } /* * Import parameters into libjail's binary jailparam format. */ int import_params(struct cfjail *j) { struct cfparam *p; struct cfstring *s, *ts; struct jailparam *jp; char *value, *cs; size_t vallen; int error; error = 0; j->njp = 0; TAILQ_FOREACH(p, &j->params, tq) if (!(p->flags & PF_INTERNAL)) j->njp++; j->jp = jp = emalloc(j->njp * sizeof(struct jailparam)); TAILQ_FOREACH(p, &j->params, tq) { if (p->flags & PF_INTERNAL) continue; if (jailparam_init(jp, p->name) < 0) { error = -1; jail_warnx(j, "%s", jail_errmsg); continue; } if (TAILQ_EMPTY(&p->val)) value = NULL; else if (!jp->jp_elemlen || !TAILQ_NEXT(TAILQ_FIRST(&p->val), tq)) { /* * Scalar parameters silently discard multiple (array) * values, keeping only the last value added. This * lets values added from the command line append to * arrays wthout pre-checking the type. */ value = TAILQ_LAST(&p->val, cfstrings)->s; } else { /* * Convert arrays into comma-separated strings, which * jailparam_import will then convert back into arrays. */ vallen = 0; TAILQ_FOREACH(s, &p->val, tq) vallen += s->len + 1; value = alloca(vallen); cs = value; TAILQ_FOREACH_SAFE(s, &p->val, tq, ts) { strcpy(cs, s->s); if (ts != NULL) { cs += s->len + 1; cs[-1] = ','; } } } if (jailparam_import(jp, value) < 0) { error = -1; jail_warnx(j, "%s", jail_errmsg); } jp++; } if (error) { jailparam_free(j->jp, j->njp); free(j->jp); j->jp = NULL; failed(j); } return error; } /* * Check if options are equal (with or without the "no" prefix). */ int equalopts(const char *opt1, const char *opt2) { char *p; /* "opt" vs. "opt" or "noopt" vs. "noopt" */ if (strcmp(opt1, opt2) == 0) return (1); /* "noopt" vs. "opt" */ if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0) return (1); /* "opt" vs. "noopt" */ if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0) return (1); while ((p = strchr(opt1, '.')) != NULL && !strncmp(opt1, opt2, ++p - opt1)) { opt2 += p - opt1; opt1 = p; /* "foo.noopt" vs. "foo.opt" */ if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0) return (1); /* "foo.opt" vs. "foo.noopt" */ if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0) return (1); } return (0); } /* * See if a jail name matches a wildcard. */ int wild_jail_match(const char *jname, const char *wname) { const char *jc, *jd, *wc, *wd; /* * A non-final "*" component in the wild name matches a single jail * component, and a final "*" matches one or more jail components. */ for (jc = jname, wc = wname; (jd = strchr(jc, '.')) && (wd = strchr(wc, '.')); jc = jd + 1, wc = wd + 1) if (strncmp(jc, wc, jd - jc + 1) && strncmp(wc, "*.", 2)) return 0; return (!strcmp(jc, wc) || !strcmp(wc, "*")); } /* * Return if a jail name is a wildcard. */ int wild_jail_name(const char *wname) { const char *wc; for (wc = strchr(wname, '*'); wc; wc = strchr(wc + 1, '*')) if ((wc == wname || wc[-1] == '.') && (wc[1] == '\0' || wc[1] == '.')) return 1; return 0; } /* * Free a parameter record and all its strings and variables. */ static void free_param(struct cfparams *pp, struct cfparam *p) { free(p->name); free_param_strings(p); TAILQ_REMOVE(pp, p, tq); free(p); } static void free_param_strings(struct cfparam *p) { struct cfstring *s; struct cfvar *v; while ((s = TAILQ_FIRST(&p->val))) { free(s->s); while ((v = STAILQ_FIRST(&s->vars))) { free(v->name); STAILQ_REMOVE_HEAD(&s->vars, tq); free(v); } TAILQ_REMOVE(&p->val, s, tq); free(s); } } Index: head/usr.sbin/jail/jailp.h =================================================================== --- head/usr.sbin/jail/jailp.h (revision 235788) +++ head/usr.sbin/jail/jailp.h (revision 235789) @@ -1,233 +1,232 @@ /*- * Copyright (c) 2011 James Gritton. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include #include #include #include #include #include #define CONF_FILE "/etc/jail.conf" #define DEP_FROM 0 #define DEP_TO 1 #define DF_SEEN 0x01 /* Dependency has been followed */ #define DF_LIGHT 0x02 /* Implied dependency on jail existence only */ #define DF_NOFAIL 0x04 /* Don't propigate failed jails */ #define PF_VAR 0x01 /* This is a variable, not a true parameter */ #define PF_APPEND 0x02 /* Append to existing parameter list */ #define PF_BAD 0x04 /* Unable to resolve parameter value */ #define PF_INTERNAL 0x08 /* Internal parameter, not passed to kernel */ #define PF_BOOL 0x10 /* Boolean parameter */ #define PF_INT 0x20 /* Integer parameter */ #define PF_CONV 0x40 /* Parameter duplicated in converted form */ #define JF_START 0x0001 /* -c */ #define JF_SET 0x0002 /* -m */ #define JF_STOP 0x0004 /* -r */ #define JF_DEPEND 0x0008 /* Operation required by dependency */ #define JF_WILD 0x0010 /* Not specified on the command line */ #define JF_FAILED 0x0020 /* Operation failed */ #define JF_PARAMS 0x0040 /* Parameters checked and imported */ #define JF_RDTUN 0x0080 /* Create-only parameter check has been done */ #define JF_PERSIST 0x0100 /* Jail is temporarily persistent */ #define JF_TIMEOUT 0x0200 /* A command (or process kill) timed out */ #define JF_SLEEPQ 0x0400 /* Waiting on a command and/or timeout */ #define JF_OP_MASK (JF_START | JF_SET | JF_STOP) #define JF_RESTART (JF_START | JF_STOP) #define JF_START_SET (JF_START | JF_SET) #define JF_SET_RESTART (JF_SET | JF_STOP) #define JF_START_SET_RESTART (JF_START | JF_SET | JF_STOP) #define JF_DO_STOP(js) (((js) & (JF_SET | JF_STOP)) == JF_STOP) enum intparam { IP__NULL = 0, /* Null command */ IP_ALLOW_DYING, /* Allow making changes to a dying jail */ IP_COMMAND, /* Command run inside jail at creation */ IP_DEPEND, /* Jail starts after (stops before) another */ IP_EXEC_CLEAN, /* Run commands in a clean environment */ IP_EXEC_CONSOLELOG, /* Redirect optput for commands run in jail */ IP_EXEC_FIB, /* Run jailed commands with this FIB */ IP_EXEC_JAIL_USER, /* Run jailed commands as this user */ IP_EXEC_POSTSTART, /* Commands run outside jail after creating */ IP_EXEC_POSTSTOP, /* Commands run outside jail after removing */ IP_EXEC_PRESTART, /* Commands run outside jail before creating */ IP_EXEC_PRESTOP, /* Commands run outside jail before removing */ IP_EXEC_START, /* Commands run inside jail on creation */ IP_EXEC_STOP, /* Commands run inside jail on removal */ IP_EXEC_SYSTEM_JAIL_USER,/* Get jail_user from system passwd file */ IP_EXEC_SYSTEM_USER, /* Run non-jailed commands as this user */ IP_EXEC_TIMEOUT, /* Time to wait for a command to complete */ #if defined(INET) || defined(INET6) IP_INTERFACE, /* Add IP addresses to this interface */ IP_IP_HOSTNAME, /* Get jail IP address(es) from hostname */ #endif IP_MOUNT, /* Mount points in fstab(5) form */ IP_MOUNT_DEVFS, /* Mount /dev under prison root */ IP_MOUNT_FSTAB, /* A standard fstab(5) file */ IP_STOP_TIMEOUT, /* Time to wait after sending SIGTERM */ IP_VNET_INTERFACE, /* Assign interface(s) to vnet jail */ #ifdef INET IP__IP4_IFADDR, /* Copy of ip4.addr with interface/netmask */ #endif #ifdef INET6 IP__IP6_IFADDR, /* Copy of ip6.addr with interface/prefixlen */ #endif IP__MOUNT_FROM_FSTAB, /* Line from mount.fstab file */ IP__OP, /* Placeholder for requested operation */ KP_ALLOW_CHFLAGS, KP_ALLOW_MOUNT, KP_ALLOW_RAW_SOCKETS, KP_ALLOW_SET_HOSTNAME, KP_ALLOW_SOCKET_AF, KP_ALLOW_SYSVIPC, KP_DEVFS_RULESET, KP_ENFORCE_STATFS, KP_HOST_HOSTNAME, #ifdef INET KP_IP4_ADDR, #endif #ifdef INET6 KP_IP6_ADDR, #endif KP_JID, KP_NAME, KP_PATH, KP_PERSIST, KP_SECURELEVEL, KP_VNET, IP_NPARAM }; STAILQ_HEAD(cfvars, cfvar); struct cfvar { STAILQ_ENTRY(cfvar) tq; char *name; size_t pos; }; TAILQ_HEAD(cfstrings, cfstring); struct cfstring { TAILQ_ENTRY(cfstring) tq; char *s; size_t len; struct cfvars vars; }; TAILQ_HEAD(cfparams, cfparam); struct cfparam { TAILQ_ENTRY(cfparam) tq; char *name; struct cfstrings val; unsigned flags; int gen; }; TAILQ_HEAD(cfjails, cfjail); STAILQ_HEAD(cfdepends, cfdepend); struct cfjail { TAILQ_ENTRY(cfjail) tq; char *name; char *comline; struct cfparams params; struct cfdepends dep[2]; struct cfjails *queue; struct cfparam *intparams[IP_NPARAM]; struct cfstring *comstring; struct jailparam *jp; struct timespec timeout; const enum intparam *comparam; unsigned flags; int jid; int seq; int pstatus; int ndeps; int njp; int nprocs; }; struct cfdepend { STAILQ_ENTRY(cfdepend) tq[2]; struct cfjail *j[2]; unsigned flags; }; extern void *emalloc(size_t); extern void *erealloc(void *, size_t); extern char *estrdup(const char *); extern int create_jail(struct cfjail *j); extern void failed(struct cfjail *j); extern void jail_note(const struct cfjail *j, const char *fmt, ...); extern void jail_warnx(const struct cfjail *j, const char *fmt, ...); extern int next_command(struct cfjail *j); extern int finish_command(struct cfjail *j); extern struct cfjail *next_proc(int nonblock); extern void load_config(void); extern struct cfjail *add_jail(void); extern void add_param(struct cfjail *j, const struct cfparam *p, enum intparam ipnum, const char *value); extern int bool_param(const struct cfparam *p); extern int int_param(const struct cfparam *p, int *ip); extern const char *string_param(const struct cfparam *p); extern int check_intparams(struct cfjail *j); extern int import_params(struct cfjail *j); extern int equalopts(const char *opt1, const char *opt2); extern int wild_jail_name(const char *wname); extern int wild_jail_match(const char *jname, const char *wname); extern void dep_setup(int docf); extern int dep_check(struct cfjail *j); extern void dep_done(struct cfjail *j, unsigned flags); extern void dep_reset(struct cfjail *j); extern struct cfjail *next_jail(void); extern int start_state(const char *target, int docf, unsigned state, int running); extern void requeue(struct cfjail *j, struct cfjails *queue); extern void yyerror(const char *); extern int yylex(void); -extern int yyparse(void); extern struct cfjails cfjails; extern struct cfjails ready; extern struct cfjails depend; extern const char *cfname; extern int note_remove; extern int paralimit; extern int verbose;