auto_subr.c revision dd51520e127b452179a2ce4ea3bd8dee949f9afe
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * CDDL HEADER START
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The contents of this file are subject to the terms of the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Common Development and Distribution License (the "License").
199767f8919635c4928607450d9e0abb932109ceToomas Soome * You may not use this file except in compliance with the License.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * or http://www.opensolaris.org/os/licensing.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * See the License for the specific language governing permissions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * and limitations under the License.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * When distributing Covered Code, include this CDDL HEADER in each
199767f8919635c4928607450d9e0abb932109ceToomas Soome * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If applicable, add the following below this CDDL HEADER, with the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * fields enclosed by brackets "[]" replaced with your own identifying
199767f8919635c4928607450d9e0abb932109ceToomas Soome * information: Portions Copyright [yyyy] [name of copyright owner]
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * CDDL HEADER END
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <ctype.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stdio.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stdlib.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <unistd.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <locale.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <syslog.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <errno.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <string.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stdarg.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <dirent.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <limits.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <thread.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/param.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/time.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/vfs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/types.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/stat.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/mnttab.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/mntent.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/mount.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/signal.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/utsname.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/systeminfo.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/tiuser.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/utsname.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <rpc/rpc.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <rpcsvc/nfs_prot.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <rpcsvc/daemon_utils.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <assert.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "automount.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <deflt.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <zone.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <priv.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <fcntl.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <libshare.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <libscf.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "smfcfg.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic char *check_hier(char *);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int arch(char *, size_t, bool_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int cpu(char *, size_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int natisa(char *, size_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int platform(char *, size_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct mntlist *current_mounts;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic bool_t nodirect_map = FALSE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If the system is labeled then we need to
199767f8919635c4928607450d9e0abb932109ceToomas Soome * have a uniquely-named auto_home map for each zone.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The maps are made unique by appending the zonename.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The home directory is made unique by prepending /zone/<zonename>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * for each zone that is dominated by the current zone.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The current zone's home directory mount point is not changed.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * For each auto_home_<zonename> a default template map is created
199767f8919635c4928607450d9e0abb932109ceToomas Soome * only if it doesn't exist yet. The default entry is used to declare
199767f8919635c4928607450d9e0abb932109ceToomas Soome * local home directories created within each zone. For example:
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * +auto_home_public
199767f8919635c4928607450d9e0abb932109ceToomas Soome * * -fstype=lofs :/zone/public/export/home/&
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomeloadzone_maps(char *mntpnt, char *map, char *opts, char **stack, char ***stkptr)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome zoneid_t *zids = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome zoneid_t my_zoneid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint_t nzents_saved;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint_t nzents;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!priv_ineffect(PRIV_SYS_MOUNT))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zone_list(NULL, &nzents) != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome my_zoneid = getzoneid();
199767f8919635c4928607450d9e0abb932109ceToomas Soomeagain:
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (nzents == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome zids = malloc(nzents * sizeof (zoneid_t));
199767f8919635c4928607450d9e0abb932109ceToomas Soome nzents_saved = nzents;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zone_list(zids, &nzents) != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(zids);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (nzents != nzents_saved) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* list changed, try again */
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(zids);
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto again;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < nzents; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome char zonename[ZONENAME_MAX];
199767f8919635c4928607450d9e0abb932109ceToomas Soome char zoneroot[MAXPATHLEN];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (getzonenamebyid(zids[i], zonename, ZONENAME_MAX) != -1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome char appended_map[MAXPATHLEN];
199767f8919635c4928607450d9e0abb932109ceToomas Soome char prepended_mntpnt[MAXPATHLEN];
199767f8919635c4928607450d9e0abb932109ceToomas Soome char map_path[MAXPATHLEN];
199767f8919635c4928607450d9e0abb932109ceToomas Soome int fd;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome (void) snprintf(appended_map, sizeof (appended_map),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "%s_%s", map, zonename);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* for current zone, leave mntpnt alone */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zids[i] != my_zoneid) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome (void) snprintf(prepended_mntpnt,
199767f8919635c4928607450d9e0abb932109ceToomas Soome sizeof (prepended_mntpnt),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "/zone/%s%s", zonename, mntpnt);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zone_getattr(zids[i], ZONE_ATTR_ROOT,
199767f8919635c4928607450d9e0abb932109ceToomas Soome zoneroot, sizeof (zoneroot)) == -1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome (void) strcpy(prepended_mntpnt, mntpnt);
199767f8919635c4928607450d9e0abb932109ceToomas Soome zoneroot[0] = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome dirinit(prepended_mntpnt, appended_map, opts, 0, stack,
199767f8919635c4928607450d9e0abb932109ceToomas Soome stkptr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Next create auto_home_<zone> maps for each zone
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome (void) snprintf(map_path, sizeof (map_path),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "/etc/%s", appended_map);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If the map file doesn't exist create a template
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL,
199767f8919635c4928607450d9e0abb932109ceToomas Soome S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome int len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char map_rec[MAXPATHLEN];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = snprintf(map_rec, sizeof (map_rec),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome appended_map, zoneroot);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (len <= sizeof (map_rec))
199767f8919635c4928607450d9e0abb932109ceToomas Soome (void) write(fd, map_rec, len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome (void) close(fd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(zids);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soomedirinit(char *mntpnt, char *map, char *opts, int direct, char **stack,
199767f8919635c4928607450d9e0abb932109ceToomas Soome char ***stkptr)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct autodir *dir;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strcmp(map, "-null") == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strcmp(mntpnt, "/-") == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome nodirect_map = TRUE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto enter;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = mntpnt + (strlen(mntpnt) - 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p == '/')
199767f8919635c4928607450d9e0abb932109ceToomas Soome *p = '\0'; /* trim trailing / */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*mntpnt != '/') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome pr_msg("dir %s must start with '/'", mntpnt);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (p = check_hier(mntpnt)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome pr_msg("hierarchical mountpoint: %s and %s",
199767f8919635c4928607450d9e0abb932109ceToomas Soome p, mntpnt);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If it's a direct map then call dirinit
199767f8919635c4928607450d9e0abb932109ceToomas Soome * for every map entry.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((strcmp(mntpnt, "/-") == 0) && !(nodirect_map)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome (void) loaddirect_map(map, map, opts, stack, stkptr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Home directories are polyinstantiated on
199767f8919635c4928607450d9e0abb932109ceToomas Soome * labeled systems.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (is_system_labeled() &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome (strcmp(mntpnt, "/home") == 0) &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome (strcmp(map, "auto_home") == 0)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome (void) loadzone_maps(mntpnt, map, opts, stack, stkptr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soomeenter:
199767f8919635c4928607450d9e0abb932109ceToomas Soome dir = (struct autodir *)malloc(sizeof (*dir));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dir == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto alloc_failed;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dir->dir_name = strdup(mntpnt);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dir->dir_name == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto alloc_failed;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dir->dir_map = strdup(map);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dir->dir_map == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto alloc_failed;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dir->dir_opts = strdup(opts);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dir->dir_opts == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto alloc_failed;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dir->dir_direct = direct;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dir->dir_remount = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dir->dir_next = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Append to dir chain
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dir_head == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome dir_head = dir;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome dir_tail->dir_next = dir;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome dir->dir_prev = dir_tail;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dir_tail = dir;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomealloc_failed:
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dir != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dir->dir_opts)
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(dir->dir_opts);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dir->dir_map)
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(dir->dir_map);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dir->dir_name)
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(dir->dir_name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(dir);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome pr_msg("dirinit: memory allocation failed");
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Check whether the mount point is a
199767f8919635c4928607450d9e0abb932109ceToomas Soome * subdirectory or a parent directory
199767f8919635c4928607450d9e0abb932109ceToomas Soome * of any previously mounted automount
199767f8919635c4928607450d9e0abb932109ceToomas Soome * mount point.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic char *
199767f8919635c4928607450d9e0abb932109ceToomas Soomecheck_hier(mntpnt)
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *mntpnt;
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome register struct autodir *dir;
199767f8919635c4928607450d9e0abb932109ceToomas Soome register char *p, *q;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (dir = dir_head; dir; dir = dir->dir_next) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = dir->dir_name;
199767f8919635c4928607450d9e0abb932109ceToomas Soome q = mntpnt;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (; *p == *q; p++, q++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p == '\0')
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p == '/' && *q == '\0')
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (dir->dir_name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p == '\0' && *q == '/')
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (dir->dir_name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p == '\0' && *q == '\0')
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (NULL); /* it's not a subdir or parent */
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Gets the next token from the string "p" and copies it into "w". The "wq" is
199767f8919635c4928607450d9e0abb932109ceToomas Soome * a quote vector for "w" and is derived from "pq", which is a quote vector for
199767f8919635c4928607450d9e0abb932109ceToomas Soome * "p". Delim is the character to be used as a delimiter for the scan. A space
199767f8919635c4928607450d9e0abb932109ceToomas Soome * means "whitespace". The call to getword must provide buffers w and wq of size
199767f8919635c4928607450d9e0abb932109ceToomas Soome * at least wordsz. getword() will pass strings of maximum length (wordsz-1),
199767f8919635c4928607450d9e0abb932109ceToomas Soome * since it needs to null terminate the string.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Returns 0 on ok and -1 on error.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomegetword(char *w, char *wq, char **p, char **pq, char delim, int wordsz)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *tmp = w;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *tmpq = wq;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int count = wordsz;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (wordsz <= 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (verbose)
199767f8919635c4928607450d9e0abb932109ceToomas Soome syslog(LOG_ERR,
199767f8919635c4928607450d9e0abb932109ceToomas Soome "getword: input word size %d must be > 0", wordsz);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome while ((delim == ' ' ? isspace(**p) : **p == delim) && **pq == ' ')
199767f8919635c4928607450d9e0abb932109ceToomas Soome (*p)++, (*pq)++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (**p &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome !((delim == ' ' ? isspace(**p) : **p == delim) &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome **pq == ' ')) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (--count <= 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome *tmp = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome *tmpq = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome syslog(LOG_ERR,
199767f8919635c4928607450d9e0abb932109ceToomas Soome "maximum word length (%d) exceeded", wordsz);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome *w++ = *(*p)++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *wq++ = *(*pq)++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome *w = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome *wq = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * get_line attempts to get a line from the map, upto LINESZ. A line in
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the map is a concatenation of lines if the continuation symbol '\'
199767f8919635c4928607450d9e0abb932109ceToomas Soome * is used at the end of the line. Returns line on success, a NULL on
199767f8919635c4928607450d9e0abb932109ceToomas Soome * EOF, and an empty string on lines > linesz.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomechar *
199767f8919635c4928607450d9e0abb932109ceToomas Soomeget_line(FILE *fp, char *map, char *line, int linesz)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome register char *p = line;
199767f8919635c4928607450d9e0abb932109ceToomas Soome register int len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int excess = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome *p = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (;;) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fgets(p, linesz - (p-line), fp) == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (*line ? line : NULL); /* EOF */
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = strlen(line);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (len <= 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = line;
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = &line[len - 1];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Is input line too long?
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p != '\n') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome excess = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Perhaps last char read was '\'. Reinsert it
199767f8919635c4928607450d9e0abb932109ceToomas Soome * into the stream to ease the parsing when we
199767f8919635c4928607450d9e0abb932109ceToomas Soome * read the rest of the line to discard.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome (void) ungetc(*p, fp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soometrim:
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* trim trailing white space */
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (p >= line && isspace(*(uchar_t *)p))
199767f8919635c4928607450d9e0abb932109ceToomas Soome *p-- = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (p < line) { /* empty line */
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = line;
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p == '\\') { /* continuation */
199767f8919635c4928607450d9e0abb932109ceToomas Soome *p = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Ignore comments. Comments start with '#'
199767f8919635c4928607450d9e0abb932109ceToomas Soome * which must be preceded by a whitespace, unless
199767f8919635c4928607450d9e0abb932109ceToomas Soome * if '#' is the first character in the line.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = line;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (p = strchr(p, '#')) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (p == line || isspace(*(p-1))) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome *p-- = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto trim;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome p++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (excess) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome int c;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * discard rest of line and return an empty string.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * done to set the stream to the correct place when
199767f8919635c4928607450d9e0abb932109ceToomas Soome * we are done with this line.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome while ((c = getc(fp)) != EOF) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome *p = c;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p == '\n') /* end of the long line */
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (*p == '\\') { /* continuation */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (getc(fp) == EOF) /* ignore next char */
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome syslog(LOG_ERR,
199767f8919635c4928607450d9e0abb932109ceToomas Soome "map %s: line too long (max %d chars)",
199767f8919635c4928607450d9e0abb932109ceToomas Soome map, linesz-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome *line = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (line);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Gets the retry=n entry from opts.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Returns 0 if retry=n is not present in option string,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * retry=n is invalid, or when option string is NULL.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomeget_retry(char *opts)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int retry = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char buf[MAXOPTSLEN];
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *p, *pb, *lasts;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (opts == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (retry);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome (void) strcpy(buf, opts);
199767f8919635c4928607450d9e0abb932109ceToomas Soome pb = buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (p = (char *)strtok_r(pb, ",", &lasts)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome pb = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strncmp(p, "retry=", 6) == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome retry = atoi(p+6);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (retry > 0 ? retry : 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Returns zero if "opt" is found in mnt->mnt_opts, setting
199767f8919635c4928607450d9e0abb932109ceToomas Soome * *sval to whatever follows the equal sign after "opt".
199767f8919635c4928607450d9e0abb932109ceToomas Soome * str_opt allocates a string long enough to store the value of
199767f8919635c4928607450d9e0abb932109ceToomas Soome * "opt" plus a terminating null character and returns it as *sval.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * It is the responsability of the caller to deallocate *sval.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * *sval will be equal to NULL upon return if either "opt=" is not found,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * or "opt=" has no value associated with it.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * stropt will return -1 on error.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomestr_opt(struct mnttab *mnt, char *opt, char **sval)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *str, *comma;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * is "opt" in the options field?
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (str = hasmntopt(mnt, opt)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome str += strlen(opt);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*str++ != '=' ||
199767f8919635c4928607450d9e0abb932109ceToomas Soome (*str == ',' || *str == '\0')) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome syslog(LOG_ERR, "Bad option field");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome comma = strchr(str, ',');
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (comma != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *comma = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome *sval = strdup(str);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (comma != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *comma = ',';
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*sval == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else
199767f8919635c4928607450d9e0abb932109ceToomas Soome *sval = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Performs text expansions in the string "pline".
199767f8919635c4928607450d9e0abb932109ceToomas Soome * "plineq" is the quote vector for "pline".
199767f8919635c4928607450d9e0abb932109ceToomas Soome * An identifier prefixed by "$" is replaced by the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * corresponding environment variable string. A "&"
199767f8919635c4928607450d9e0abb932109ceToomas Soome * is replaced by the key string for the map entry.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This routine will return an error (non-zero) if *size* would be
199767f8919635c4928607450d9e0abb932109ceToomas Soome * exceeded after expansion, indicating that the macro_expand failed.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This is to prevent writing past the end of pline and plineq.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Both pline and plineq are left untouched in such error case.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomemacro_expand(key, pline, plineq, size)
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *key, *pline, *plineq;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome register char *p, *q;
199767f8919635c4928607450d9e0abb932109ceToomas Soome register char *bp, *bq;
199767f8919635c4928607450d9e0abb932109ceToomas Soome register char *s;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char buffp[LINESZ], buffq[LINESZ];
199767f8919635c4928607450d9e0abb932109ceToomas Soome char namebuf[64], *pn;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int expand = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct utsname name;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char procbuf[SYS_NMLN];
199767f8919635c4928607450d9e0abb932109ceToomas Soome char isaname[64];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = pline; q = plineq;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bp = buffp; bq = buffq;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (*p) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p == '&' && *q == ' ') { /* insert key */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * make sure we don't overflow buffer
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((int)((bp - buffp) + strlen(key)) < size) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (s = key; *s; s++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome *bp++ = *s;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *bq++ = ' ';
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome expand++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome p++; q++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * line too long...
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p == '$' && *q == ' ') { /* insert env var */
199767f8919635c4928607450d9e0abb932109ceToomas Soome p++; q++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome pn = namebuf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p == '{') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome p++; q++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (*p && *p != '}') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome *pn++ = *p++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome q++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome p++; q++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (*p && (*p == '_' || isalnum(*p))) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome *pn++ = *p++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome q++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome *pn = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome
s = getenv(namebuf);
if (!s) {
/* not found in env */
if (strcmp(namebuf, "ARCH") == 0) {
if (arch(procbuf, sizeof (procbuf),
FALSE))
s = procbuf;
} else if (strcmp(namebuf, "CPU") == 0) {
if (cpu(procbuf, sizeof (procbuf)))
s = procbuf;
} else if (strcmp(namebuf, "HOST") == 0) {
(void) uname(&name);
s = name.nodename;
} else if (strcmp(namebuf, "KARCH") == 0) {
if (arch(procbuf, sizeof (procbuf),
TRUE))
s = procbuf;
} else if (strcmp(namebuf, "OSREL") == 0) {
(void) uname(&name);
s = name.release;
} else if (strcmp(namebuf, "OSNAME") == 0) {
(void) uname(&name);
s = name.sysname;
} else if (strcmp(namebuf, "OSVERS") == 0) {
(void) uname(&name);
s = name.version;
} else if (strcmp(namebuf, "NATISA") == 0) {
if (natisa(isaname, sizeof (isaname)))
s = isaname;
} else if (strcmp(namebuf, "PLATFORM") == 0) {
if (platform(procbuf, sizeof (procbuf)))
s = procbuf;
}
}
if (s) {
if ((int)((bp - buffp) + strlen(s)) < size) {
while (*s) {
*bp++ = *s++;
*bq++ = ' ';
}
} else {
/*
* line too long...
*/
return (1);
}
}
expand++;
continue;
}
/*
* Since buffp needs to be null terminated, we need to
* check that there's still room in the buffer to
* place at least two more characters, *p and the
* terminating null.
*/
if (bp - buffp == size - 1) {
/*
* There was not enough room for at least two more
* characters, return with an error.
*/
return (1);
}
/*
* The total number of characters so far better be less
* than the size of buffer passed in.
*/
*bp++ = *p++;
*bq++ = *q++;
}
if (!expand)
return (0);
*bp = '\0';
*bq = '\0';
/*
* We know buffp/buffq will fit in pline/plineq since we
* processed at most size characters.
*/
(void) strcpy(pline, buffp);
(void) strcpy(plineq, buffq);
return (0);
}
/*
* Removes backslashes, quotes and brackets from the string "str"
* and returns the quoting information in "qbuf". Character is
* considered escaped when it is
*
* preceded with '\' e.g. \a
* within quotes e.g. "string"
* a ':' in brackets e.g. [an:ip:6::ad::d:re:s:s]
*
* original str: 'the "brown" f\ox said: [fe80::20a:e4ff:fe35:8b0d]'
* unquoted str: 'the brown fox said: [fe80::20a:e4ff:fe35:8b0d]'
* and the qbuf: ' ^^^^^ ^ ^^ ^ ^ ^ '
*/
void
unquote(str, qbuf)
char *str, *qbuf;
{
register int escaped, inquote, inbracket, quoted;
register char *ip, *bp, *qp;
char buf[LINESZ];
escaped = inquote = inbracket = quoted = 0;
for (ip = str, bp = buf, qp = qbuf; *ip; ip++) {
if (!escaped) {
if (*ip == '\\') {
escaped = 1;
quoted++;
continue;
} else
if (*ip == '"') {
inquote = !inquote;
quoted++;
continue;
} else
if (*ip == '[') {
inbracket++;
quoted++;
} else
if (*ip == ']') {
if (inbracket > 0) inbracket--;
}
}
*bp++ = *ip;
*qp++ = (inquote || escaped) ? '^'
: ((inbracket && (*ip == ':')) ? '^' : ' ');
escaped = 0;
}
*bp = '\0';
*qp = '\0';
if (quoted)
(void) strcpy(str, buf);
}
/*
* If str is enclosed in [brackets], trim them off.
*/
void
unbracket(s)
char **s;
{
char *b = *s + strlen(*s) - 1;
if (*b == ']')
*b = '\0';
if (**s == '[')
(*s)++;
}
/*
* Removes trailing spaces from string "s".
*/
void
trim(s)
char *s;
{
char *p = &s[strlen(s) - 1];
while (p >= s && isspace(*(uchar_t *)p))
*p-- = '\0';
}
/*
* try to allocate memory using malloc, if malloc fails, then flush the
* rddir caches, and retry. If the second allocation after the readdir
* caches have been flushed fails too, then return NULL to indicate
* memory could not be allocated.
*/
char *
auto_rddir_malloc(unsigned nbytes)
{
char *p;
int again = 0;
if ((p = malloc(nbytes)) == NULL) {
/*
* No memory, free rddir caches and try again
*/
mutex_lock(&cleanup_lock);
cond_signal(&cleanup_start_cv);
if (cond_wait(&cleanup_done_cv, &cleanup_lock)) {
mutex_unlock(&cleanup_lock);
syslog(LOG_ERR, "auto_rddir_malloc interrupted\n");
} else {
mutex_unlock(&cleanup_lock);
again = 1;
}
}
if (again)
p = malloc(nbytes);
return (p);
}
/*
* try to strdup a string, if it fails, then flush the rddir caches,
* and retry. If the second strdup fails, return NULL to indicate failure.
*/
char *
auto_rddir_strdup(const char *s1)
{
char *s2;
int again = 0;
if ((s2 = strdup(s1)) == NULL) {
/*
* No memory, free rddir caches and try again
*/
mutex_lock(&cleanup_lock);
cond_signal(&cleanup_start_cv);
if (cond_wait(&cleanup_done_cv, &cleanup_lock)) {
mutex_unlock(&cleanup_lock);
syslog(LOG_ERR, "auto_rddir_strdup interrupted\n");
} else {
mutex_unlock(&cleanup_lock);
again = 1;
}
}
if (again)
s2 = strdup(s1);
return (s2);
}
/*
* Returns a pointer to the entry corresponding to 'name' if found,
* otherwise it returns NULL.
*/
struct dir_entry *
btree_lookup(struct dir_entry *head, char *name)
{
register struct dir_entry *p;
register int direction;
for (p = head; p != NULL; ) {
direction = strcmp(name, p->name);
if (direction == 0)
return (p);
if (direction > 0)
p = p->right;
else p = p->left;
}
return (NULL);
}
/*
* Add entry to binary tree
* Duplicate entries are not added
*/
void
btree_enter(struct dir_entry **head, struct dir_entry *ent)
{
register struct dir_entry *p, *prev = NULL;
register int direction;
ent->right = ent->left = NULL;
if (*head == NULL) {
*head = ent;
return;
}
for (p = *head; p != NULL; ) {
prev = p;
direction = strcmp(ent->name, p->name);
if (direction == 0) {
/*
* entry already in btree
*/
return;
}
if (direction > 0)
p = p->right;
else p = p->left;
}
assert(prev != NULL);
if (direction > 0)
prev->right = ent;
else prev->left = ent;
}
/*
* If entry doesn't exist already, add it to the linear list
* after '*last' and to the binary tree list.
* If '*last == NULL' then the list is walked till the end.
* *last is always set to the new element after successful completion.
* if entry already exists '*last' is only updated if not previously
* provided.
*/
int
add_dir_entry(char *name, struct dir_entry **list, struct dir_entry **last)
{
struct dir_entry *e, *l;
if ((*list != NULL) && (*last == NULL)) {
/*
* walk the list to find last element
*/
for (l = *list; l != NULL; l = l->next)
*last = l;
}
if (btree_lookup(*list, name) == NULL) {
/*
* not a duplicate, add it to list
*/
/* LINTED pointer alignment */
e = (struct dir_entry *)
auto_rddir_malloc(sizeof (struct dir_entry));
if (e == NULL)
return (ENOMEM);
(void) memset((char *)e, 0, sizeof (*e));
e->name = auto_rddir_strdup(name);
if (e->name == NULL) {
free(e);
return (ENOMEM);
}
e->next = NULL;
if (*list == NULL) {
/*
* list is empty
*/
*list = *last = e;
} else {
/*
* append to end of list
*/
assert(*last != NULL);
(*last)->next = e;
*last = e;
}
/*
* add to binary tree
*/
btree_enter(list, e);
}
return (0);
}
/*
* Print trace output.
* Like fprintf(stderr, fmt, ...) except that if "id" is nonzero, the output
* is preceeded by the ID of the calling thread.
*/
#define FMT_BUFSIZ 1024
void
trace_prt(int id, char *fmt, ...)
{
va_list args;
char buf[FMT_BUFSIZ];
if (id) {
(void) sprintf(buf, "t%u\t%s", thr_self(), fmt);
fmt = buf;
}
va_start(args, fmt);
(void) vfprintf(stderr, fmt, args);
va_end(args);
}
/*
* Extract the isalist(5) for userland from the kernel.
*/
static char *
isalist(void)
{
char *buf;
size_t bufsize = BUFSIZ; /* wild guess */
long ret;
buf = malloc(bufsize);
do {
ret = sysinfo(SI_ISALIST, buf, bufsize);
if (ret == -1l)
return (NULL);
if (ret > bufsize) {
bufsize = ret;
buf = realloc(buf, bufsize);
} else
break;
} while (buf != NULL);
return (buf);
}
/*
* Classify isa's as to bitness of the corresponding ABIs.
* isa's which have no "official" system ABI are returned
* unrecognised i.e. zero bits.
*/
static int
bitness(char *isaname)
{
if (strcmp(isaname, "sparc") == 0 ||
strcmp(isaname, "i386") == 0)
return (32);
if (strcmp(isaname, "sparcv9") == 0 ||
strcmp(isaname, "amd64") == 0)
return (64);
return (0);
}
/*
* Determine the application architecture (derived from uname -m) to expand
* the $ARCH and $KARCH macros.
*
* Like arch(1), we need to substitute "sun4" for "sun4u", "sun4v", ... for
* backward compatibility. When kflag is set (like arch -k), the unmodifed
* value is returned instead.
*/
static int
arch(char *buf, size_t bufsize, bool_t karch)
{
long ret;
ret = sysinfo(SI_MACHINE, buf, bufsize);
if (ret == -1L)
return (0);
if (!karch && strncmp(buf, "sun4", 4) == 0)
(void) strlcpy(buf, "sun4", bufsize);
return (1);
}
/*
* Determine the basic ISA (uname -p) to expand the $CPU macro.
*/
static int
cpu(char *buf, size_t bufsize)
{
long ret;
ret = sysinfo(SI_ARCHITECTURE, buf, bufsize);
if (ret == -1L)
return (0);
else
return (1);
}
/*
* Find the left-most element in the isalist that matches our idea of a
* system ABI.
*
* On machines with only one ABI, this is usually the same as uname -p.
*/
static int
natisa(char *buf, size_t bufsize)
{
int bits;
char *isa, *list;
char *lasts;
if ((list = isalist()) == NULL)
return (0);
for (isa = strtok_r(list, " ", &lasts);
isa; isa = strtok_r(0, " ", &lasts))
if ((bits = bitness(isa)) != 0)
break; /* ignore "extension" architectures */
if (isa == 0 || bits == 0) {
free(list);
return (0); /* can't figure it out :( */
}
(void) strncpy(buf, isa, bufsize);
free(list);
return (1);
}
/*
* Determine the platform (uname -i) to expand the $PLATFORM macro.
*/
static int
platform(char *buf, size_t bufsize)
{
long ret;
ret = sysinfo(SI_PLATFORM, buf, bufsize);
if (ret == -1L)
return (0);
else
return (1);
}
/*
* Set environment variables
*/
void
put_automountd_env(void)
{
char defval[PATH_MAX], *p, *a, *c;
int ret = 0, bufsz = PATH_MAX;
ret = autofs_smf_get_prop("environment", defval, DEFAULT_INSTANCE,
SCF_TYPE_ASTRING, AUTOMOUNTD, &bufsz);
if (ret == SA_OK) {
a = c = defval;
if (*a == NULL)
return;
/*
* Environment variables can have more than one value
* seperated by a comma and there can be multiple
* environment variables. * a=b\,c,d=e. For multiple
* valued environment variable, values are seperated
* with an escape character.
*/
while ((p = strchr(c, ',')) != NULL) {
if (*(p - 1) == '\\') {
c = p + 1;
continue;
}
*p = '\0';
if ((c = strchr(a, '=')) != NULL)
putenv(strdup(a));
a = c = p + 1;
}
if (*a != NULL) {
if ((c = strchr(a, '=')) != NULL)
putenv(strdup(a));
}
}
}