1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
/* ----------------------------------------------------------------------- *
*
* mount_autofs.c - Module for recursive autofs mounts.
*
* Copyright 1997 Transmeta Corporation - All Rights Reserved
* Copyright 2006 Ian Kent <raven@themaw.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
* USA; either version 2 of the License, or (at your option) any later
* version; incorporated herein by reference.
*
* ----------------------------------------------------------------------- */
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <signal.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#define MODULE_MOUNT
#include "automount.h"
#define MODPREFIX "mount(autofs): "
/* Attributes to create handle_mounts() thread */
extern pthread_attr_t th_attr;
extern struct startup_cond suc;
int mount_version = AUTOFS_MOUNT_VERSION; /* Required by protocol */
int mount_init(void **context)
{
return 0;
}
int mount_reinit(void **context)
{
return 0;
}
int mount_mount(struct autofs_point *ap, const char *root, const char *name,
int name_len, const char *what, const char *fstype,
const char *c_options, void *context)
{
struct startup_cond suc;
pthread_t thid;
char mountpoint[PATH_MAX + 1];
const char **argv;
int argc, status;
int nobind = ap->flags & MOUNT_FLAG_NOBIND;
int ghost = ap->flags & MOUNT_FLAG_GHOST;
int symlnk = ap->flags & MOUNT_FLAG_SYMLINK;
int strictexpire = ap->flags & MOUNT_FLAG_STRICTEXPIRE;
time_t timeout = get_exp_timeout(ap, ap->entry->maps);
unsigned logopt = ap->logopt;
struct map_type_info *info;
struct master *master;
struct master_mapent *entry;
struct map_source *source;
struct autofs_point *nap;
struct mnt_list *mnt;
char buf[MAX_ERR_BUF];
char *options, *p;
int err, ret;
int hosts = 0;
/* Root offset of multi-mount */
if (root[strlen(root) - 1] == '/') {
err = snprintf(mountpoint, PATH_MAX + 1, "%s", root);
if (err > PATH_MAX) {
error(ap->logopt, MODPREFIX "string too long for mountpoint");
return 1;
}
mountpoint[err - 1] = 0;
} else if (*name == '/') {
if (ap->flags & MOUNT_FLAG_REMOUNT) {
err = snprintf(mountpoint, PATH_MAX + 1, "%s", name);
if (err > PATH_MAX) {
error(ap->logopt, MODPREFIX "string too long for mountpoint");
return 1;
}
} else {
err = snprintf(mountpoint, PATH_MAX + 1, "%s", root);
if (err > PATH_MAX) {
error(ap->logopt, MODPREFIX "string too long for mountpoint");
return 1;
}
}
} else {
err = snprintf(mountpoint, PATH_MAX + 1, "%s/%s", root, name);
if (err > PATH_MAX) {
error(ap->logopt, MODPREFIX "string too long for mountpoint");
return 1;
}
}
options = NULL;
if (c_options) {
char *noptions;
const char *comma;
char *np;
int len = strlen(c_options) + 1;
noptions = np = alloca(len);
if (!np) {
char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
error(ap->logopt, MODPREFIX "alloca: %s", estr);
return 1;
}
memset(np, 0, len);
/* Grab the autofs specific options */
for (comma = c_options; *comma != '\0';) {
const char *cp;
while (*comma == ',')
comma++;
cp = comma;
while (*comma != '\0' && *comma != ',')
comma++;
if (_strncmp("nobrowse", cp, 8) == 0 ||
_strncmp("nobrowsable", cp, 11) == 0)
ghost = 0;
else if (_strncmp("nobind", cp, 6) == 0)
nobind = 1;
else if (_strncmp("browse", cp, 6) == 0 ||
_strncmp("browsable", cp, 9) == 0)
ghost = 1;
else if (_strncmp("symlink", cp, 7) == 0)
symlnk = 1;
else if (_strncmp("strictexpire", cp, 12) == 0)
strictexpire = 1;
else if (_strncmp("hosts", cp, 5) == 0)
hosts = 1;
else if (_strncmp("timeout=", cp, 8) == 0) {
char *val = strchr(cp, '=');
unsigned tout;
if (val) {
int ret = sscanf(cp, "timeout=%u", &tout);
if (ret)
timeout = tout;
}
} else {
memcpy(np, cp, comma - cp + 1);
np += comma - cp + 1;
}
}
options = noptions;
}
debug(ap->logopt,
MODPREFIX "mountpoint=%s what=%s options=%s",
mountpoint, what, options);
master = ap->entry->master;
entry = master_new_mapent(master, mountpoint, ap->entry->age);
if (!entry) {
error(ap->logopt,
MODPREFIX "failed to malloc master_mapent struct");
return 1;
}
ret = master_add_autofs_point(entry, logopt, nobind, ghost, 1);
if (!ret) {
error(ap->logopt,
MODPREFIX "failed to add autofs_point to entry");
master_free_mapent(entry);
return 1;
}
nap = entry->ap;
nap->parent = ap;
if (symlnk)
nap->flags |= MOUNT_FLAG_SYMLINK;
if (strictexpire)
nap->flags |= MOUNT_FLAG_STRICTEXPIRE;
if (hosts)
argc = 0;
else
argc = 1;
if (options) {
char *t = options;
do {
argc++;
if (*t == ',')
t++;
} while ((t = strchr(t, ',')) != NULL);
}
argv = (const char **) alloca((argc + 1) * sizeof(char *));
if (hosts)
argc = 0;
else
argc = 1;
/*
* If a mount of a hosts map is being requested it will come
* ro us via the options. Catch that below when processing the
* option and create type info struct then.
*/
if (hosts)
info = parse_map_type_info("hosts:");
else
info = parse_map_type_info(what);
if (!info) {
error(ap->logopt, MODPREFIX "failed to parse map info");
master_free_mapent(entry);
return 1;
}
if (info->map)
argv[0] = info->map;
if (options) {
p = options;
do {
if (*p == ',') {
*p = '\0';
p++;
}
argv[argc++] = p;
} while ((p = strchr(p, ',')) != NULL);
}
argv[argc] = NULL;
/*
* For amd type "auto" the map is often re-used so check
* if the the parent map can be used and use it if it
* matches.
*
* Also if the parent map format is amd and the format
* isn't specified in the map entry set it from the parent
* map source.
*/
source = NULL;
if (ap->entry->maps && ap->entry->maps->flags & MAP_FLAG_FORMAT_AMD) {
struct map_source *s = ap->entry->maps;
/*
* For amd maps, if the format and source type aren't
* specified try and set them from the parent.
*/
if (!info->format) {
info->format = strdup("amd");
if (!info->format)
warn(ap->logopt, MODPREFIX
"failed to set amd map format");
if (!info->type && s->type) {
info->type = strdup(s->type);
if (!info->type)
warn(ap->logopt, MODPREFIX
"failed to set amd map type");
}
}
source = master_get_map_source(ap->entry,
info->type, info->format,
argc, argv);
if (source)
entry->maps = source;
}
if (!source)
source = master_add_map_source(entry,
info->type, info->format,
monotonic_time(NULL),
argc, argv);
if (!source) {
error(ap->logopt,
MODPREFIX "failed to add map source to entry");
master_free_mapent(entry);
free_map_type_info(info);
return 1;
}
free_map_type_info(info);
set_exp_timeout(nap, NULL, timeout);
nap->exp_runfreq = (timeout + CHECK_RATIO - 1) / CHECK_RATIO;
if (source->flags & MAP_FLAG_FORMAT_AMD) {
struct mnt_list *mnt;
mnt = mnts_find_amdmount(entry->path);
if (mnt) {
if (mnt->amd_pref) {
nap->pref = mnt->amd_pref;
mnt->amd_pref = NULL;
}
if (mnt->amd_cache_opts & AMD_CACHE_OPTION_ALL)
nap->flags |= MOUNT_FLAG_AMD_CACHE_ALL;
mnts_put_mount(mnt);
}
}
if (handle_mounts_startup_cond_init(&suc)) {
crit(ap->logopt, MODPREFIX
"failed to init startup cond for mount %s", entry->path);
master_free_map_source(source, 1);
master_free_mapent(entry);
return 1;
}
mnt = mnts_add_submount(nap);
if (!mnt) {
crit(ap->logopt,
MODPREFIX "failed to allocate mount %s", mountpoint);
handle_mounts_startup_cond_destroy(&suc);
master_free_map_source(source, 1);
master_free_mapent(entry);
return 1;
}
suc.ap = nap;
suc.done = 0;
suc.status = 0;
if (pthread_create(&thid, &th_attr, handle_mounts, &suc)) {
crit(ap->logopt,
MODPREFIX
"failed to create mount handler thread for %s",
mountpoint);
handle_mounts_startup_cond_destroy(&suc);
mnts_remove_submount(nap->path);
master_free_map_source(source, 1);
master_free_mapent(entry);
return 1;
}
while (!suc.done) {
status = pthread_cond_wait(&suc.cond, &suc.mutex);
if (status) {
handle_mounts_startup_cond_destroy(&suc);
mnts_remove_submount(nap->path);
master_free_map_source(source, 1);
master_free_mapent(entry);
fatal(status);
}
}
if (suc.status) {
crit(ap->logopt,
MODPREFIX "failed to create submount for %s", mountpoint);
handle_mounts_startup_cond_destroy(&suc);
mnts_remove_submount(nap->path);
master_free_map_source(source, 1);
master_free_mapent(entry);
return 1;
}
nap->thid = thid;
handle_mounts_startup_cond_destroy(&suc);
return 0;
}
int mount_done(void *context)
{
return 0;
}
|