#pragma ident "@(#)auto_subr.c 1.49 05/06/08 SMI"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <locale.h>
#include <syslog.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <dirent.h>
#include <pthread.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <sys/signal.h>
#include <sys/utsname.h>
#include <assert.h>
#include "autofs.h"
#include "automount.h"
static char *check_hier(char *);
static int natisa(char *, size_t);
struct mntlist *current_mounts;
static bool_t nodirect_map = FALSE;
void
dirinit(char *mntpnt, char *map, char *opts, int direct, char **stack,
char ***stkptr)
{
struct autodir *dir;
size_t mntpntlen;
char *p;
if (strcmp(map, "-null") == 0) {
if (strcmp(mntpnt, "/-") == 0)
nodirect_map = TRUE;
goto enter;
}
mntpntlen = strlen(mntpnt);
if (mntpntlen == 0) {
pr_msg("dir is empty string");
return;
}
p = mntpnt + (mntpntlen - 1);
if (*p == '/')
*p = '\0';
if (*mntpnt != '/') {
pr_msg("dir %s must start with '/'", mntpnt);
return;
}
if ((p = check_hier(mntpnt)) != NULL) {
pr_msg("hierarchical mountpoint: %s and %s",
p, mntpnt);
return;
}
if ((strcmp(mntpnt, "/-") == 0) && !(nodirect_map)) {
(void) loaddirect_map(map, map, opts, stack, stkptr);
return;
}
enter:
dir = (struct autodir *)malloc(sizeof (*dir));
if (dir == NULL)
goto alloc_failed;
dir->dir_name = strdup(mntpnt);
if (dir->dir_name == NULL)
goto alloc_failed;
dir->dir_map = strdup(map);
if (dir->dir_map == NULL)
goto alloc_failed;
dir->dir_opts = strdup(opts);
if (dir->dir_opts == NULL)
goto alloc_failed;
dir->dir_direct = direct;
dir->dir_next = NULL;
if (dir_head == NULL)
dir_head = dir;
else
dir_tail->dir_next = dir;
dir->dir_prev = dir_tail;
dir_tail = dir;
return;
alloc_failed:
if (dir != NULL) {
if (dir->dir_opts)
free(dir->dir_opts);
if (dir->dir_map)
free(dir->dir_map);
if (dir->dir_name)
free(dir->dir_name);
free(dir);
}
pr_msg("dirinit: memory allocation failed");
}
static char *
check_hier(mntpnt)
char *mntpnt;
{
register struct autodir *dir;
register char *p, *q;
for (dir = dir_head; dir; dir = dir->dir_next) {
p = dir->dir_name;
q = mntpnt;
for (; *p == *q; p++, q++)
if (*p == '\0')
break;
if (*p == '/' && *q == '\0')
return (dir->dir_name);
if (*p == '\0' && *q == '/')
return (dir->dir_name);
if (*p == '\0' && *q == '\0')
return (NULL);
}
return (NULL);
}
int
getword(char *w, char *wq, char **p, char **pq, char delim, int wordsz)
{
char *tmp = w;
char *tmpq = wq;
int count = wordsz;
if (wordsz <= 0) {
if (verbose)
syslog(LOG_ERR,
"getword: input word size %d must be > 0", wordsz);
return (-1);
}
while ((delim == ' ' ? isspace(**p) : **p == delim) && **pq == ' ')
(*p)++, (*pq)++;
while (**p &&
!((delim == ' ' ? isspace(**p) : **p == delim) &&
**pq == ' ')) {
if (--count <= 0) {
*tmp = '\0';
*tmpq = '\0';
syslog(LOG_ERR,
"maximum word length (%d) exceeded", wordsz);
return (-1);
}
*w++ = *(*p)++;
*wq++ = *(*pq)++;
}
*w = '\0';
*wq = '\0';
return (0);
}
char *
get_line(FILE *fp, char *map, char *line, int linesz)
{
register char *p = line;
register int len;
int excess = 0;
*p = '\0';
for (;;) {
if (fgets(p, linesz - (p-line), fp) == NULL) {
return (*line ? line : NULL);
}
len = strlen(line);
if (len <= 0) {
p = line;
continue;
}
p = &line[len - 1];
if (*p != '\n') {
excess = 1;
(void) ungetc(*p, fp);
break;
}
trim:
while (p >= line && isspace(*(uchar_t *)p))
*p-- = '\0';
if (p < line) {
p = line;
continue;
}
if (*p == '\\') {
*p = '\0';
continue;
}
p = line;
while ((p = strchr(p, '#')) != NULL) {
if (p == line || isspace(*(p-1))) {
*p-- = '\0';
goto trim;
}
p++;
}
break;
}
if (excess) {
int c;
while ((c = getc(fp)) != EOF) {
*p = c;
if (*p == '\n')
break;
else if (*p == '\\') {
if (getc(fp) == EOF)
break;
}
}
syslog(LOG_ERR,
"map %s: line too long (max %d chars)",
map, linesz-1);
*line = '\0';
}
return (line);
}
int
get_retry(char *opts)
{
int retry = 0;
char buf[MAXOPTSLEN];
char *p, *pb, *lasts;
if (opts == NULL)
return (retry);
CHECK_STRCPY(buf, opts, sizeof (buf));
pb = buf;
while ((p = (char *)strtok_r(pb, ",", &lasts)) != NULL) {
pb = NULL;
if (strncmp(p, "retry=", 6) == 0)
retry = atoi(p+6);
}
return (retry > 0 ? retry : 0);
}
#if 0
int
str_opt(struct mnttab *mnt, char *opt, char **sval)
{
char *str, *comma;
if (str = hasmntopt(mnt, opt)) {
str += strlen(opt);
if (*str++ != '=' ||
(*str == ',' || *str == '\0')) {
syslog(LOG_ERR, "Bad option field");
return (-1);
}
comma = strchr(str, ',');
if (comma != NULL)
*comma = '\0';
*sval = strdup(str);
if (comma != NULL)
*comma = ',';
if (*sval == NULL)
return (-1);
} else
*sval = NULL;
return (0);
}
#endif
int
macro_expand(key, pline, plineq, size)
char *key, *pline, *plineq;
int size;
{
register char *p, *q;
register char *bp, *bq;
register char *s;
char buffp[LINESZ], buffq[LINESZ];
char namebuf[64], *pn;
int expand = 0;
struct utsname name;
char isaname[64];
p = pline; q = plineq;
bp = buffp; bq = buffq;
while (*p) {
if (*p == '&' && *q == ' ') {
if ((int)((bp - buffp) + strlen(key)) < size) {
for (s = key; *s; s++) {
*bp++ = *s;
*bq++ = ' ';
}
expand++;
p++; q++;
continue;
} else {
return (1);
}
}
if (*p == '$' && *q == ' ') {
p++; q++;
pn = namebuf;
if (*p == '{') {
p++; q++;
while (*p && *p != '}') {
*pn++ = *p++;
q++;
}
if (*p) {
p++; q++;
}
} else {
while (*p && (*p == '_' || isalnum(*p))) {
*pn++ = *p++;
q++;
}
}
*pn = '\0';
s = getenv(namebuf);
if (!s) {
if (strcmp(namebuf, "HOST") == 0) {
(void) uname(&name);
s = name.nodename;
} 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;
}
}
if (s) {
if ((int)((bp - buffp) + strlen(s)) < size) {
while (*s) {
*bp++ = *s++;
*bq++ = ' ';
}
} else {
return (1);
}
}
expand++;
continue;
}
if (bp - buffp == size - 1) {
return (1);
}
*bp++ = *p++;
*bq++ = *q++;
}
if (!expand)
return (0);
*bp = '\0';
*bq = '\0';
(void) strcpy(pline, buffp);
(void) strcpy(plineq, buffq);
return (0);
}
void
unquote(str, qbuf)
char *str, *qbuf;
{
register int escaped, inquote, quoted;
register char *ip, *bp, *qp;
char buf[LINESZ];
escaped = inquote = 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;
}
}
*bp++ = *ip;
*qp++ = (inquote || escaped) ? '^' : ' ';
escaped = 0;
}
*bp = '\0';
*qp = '\0';
if (quoted)
(void) strcpy(str, buf);
}
void
trim(s)
char *s;
{
size_t slen;
char *p;
slen = strlen(s);
if (slen == 0)
return;
p = &s[slen - 1];
while (p >= s && isspace(*(uchar_t *)p))
*p-- = '\0';
}
char *
auto_rddir_malloc(unsigned nbytes)
{
char *p;
int again = 0;
if ((p = malloc(nbytes)) == NULL) {
pthread_mutex_lock(&cleanup_lock);
pthread_cond_signal(&cleanup_start_cv);
if (pthread_cond_wait(&cleanup_done_cv, &cleanup_lock)) {
pthread_mutex_unlock(&cleanup_lock);
syslog(LOG_ERR, "auto_rddir_malloc interrupted\n");
} else {
pthread_mutex_unlock(&cleanup_lock);
again = 1;
}
}
if (again)
p = malloc(nbytes);
return (p);
}
char *
auto_rddir_strdup(const char *s1)
{
char *s2;
int again = 0;
if ((s2 = strdup(s1)) == NULL) {
pthread_mutex_lock(&cleanup_lock);
pthread_cond_signal(&cleanup_start_cv);
if (pthread_cond_wait(&cleanup_done_cv, &cleanup_lock)) {
pthread_mutex_unlock(&cleanup_lock);
syslog(LOG_ERR, "auto_rddir_strdup interrupted\n");
} else {
pthread_mutex_unlock(&cleanup_lock);
again = 1;
}
}
if (again)
s2 = strdup(s1);
return (s2);
}
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);
}
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) {
return;
}
if (direction > 0)
p = p->right;
else p = p->left;
}
assert(prev != NULL);
if (direction > 0)
prev->right = ent;
else prev->left = ent;
}
int
add_dir_entry(char *name, struct dir_entry **list, struct dir_entry **last)
{
struct dir_entry *e, *l;
if ((*list != NULL) && (*last == NULL)) {
for (l = *list; l != NULL; l = l->next)
*last = l;
}
if (btree_lookup(*list, name) == NULL) {
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 = *last = e;
} else {
assert(*last != NULL);
(*last)->next = e;
*last = e;
}
btree_enter(list, e);
}
return (0);
}
#define FMT_BUFSIZ 1024
void
trace_prt(int id, char *fmt, ...)
{
va_list args;
char buf[FMT_BUFSIZ];
if (id) {
if (snprintf(buf, sizeof (buf), "t%p\t%s", pthread_self(), fmt)
>= (int)sizeof (buf))
return;
fmt = buf;
}
va_start(args, fmt);
(void) vfprintf(stderr, fmt, args);
va_end(args);
}
static char *
isalist(void)
{
#if defined(__ppc__)
return (strdup("powerpc"));
#elif defined(__i386__)
return (strdup("i386"));
#else
return (NULL);
#endif
}
static int
bitness(char *isaname)
{
if (strcmp(isaname, "sparc") == 0 ||
strcmp(isaname, "powerpc") == 0 ||
strcmp(isaname, "i386") == 0)
return (32);
if (strcmp(isaname, "sparcv9") == 0 ||
strcmp(isaname, "ppc64") == 0 ||
strcmp(isaname, "x86_64") == 0)
return (64);
return (0);
}
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;
if (isa == 0 || bits == 0) {
free(list);
return (0);
}
(void) strncpy(buf, isa, bufsize);
free(list);
return (1);
}