82
82
#define MAX_OPEN_FILES 10240
84
int aquire_flag_file(void);
85
void release_flag_file(void);
84
86
static int umount_all(struct autofs_point *ap, int force);
86
extern pthread_mutex_t master_mutex;
87
88
extern struct master *master_list;
89
90
static int do_mkdir(const char *parent, const char *path, mode_t mode)
245
246
int, void *), int incl, unsigned logopt, void *arg)
247
248
char buf[PATH_MAX + 1];
250
if (lstat(base, &st) != -1 && (fn) (logopt, base, &st, 0, arg)) {
249
struct stat st, *pst = &st;
252
if (!is_mounted(_PATH_MOUNTED, base, MNTS_REAL))
253
ret = lstat(base, pst);
259
if (ret != -1 && (fn) (logopt, base, pst, 0, arg)) {
251
260
if (S_ISDIR(st.st_mode)) {
252
261
struct dirent **de;
368
380
static void check_rm_dirs(struct autofs_point *ap, const char *path, int incl)
383
* If we're a submount the kernel can't know we're trying to
384
* shutdown and so cannot block processes walking into the
385
* mount point directory. If this is the call to umount_multi()
386
* made during shutdown (incl == 0) we have to leave any mount
387
* point directories in place so we can recover if needed. The
388
* umount itself will clean these directories up for us
391
if (!incl && ap->submount)
394
if ((!(ap->flags & MOUNT_FLAG_GHOST)) ||
371
395
(ap->state == ST_SHUTDOWN_PENDING ||
372
396
ap->state == ST_SHUTDOWN_FORCE ||
373
397
ap->state == ST_SHUTDOWN))
374
398
rm_unwanted(ap->logopt, path, incl, ap->dev);
375
else if (ap->ghost && (ap->type == LKP_INDIRECT))
399
else if ((ap->flags & MOUNT_FLAG_GHOST) && (ap->type == LKP_INDIRECT))
376
400
rm_unwanted(ap->logopt, path, 0, ap->dev);
467
488
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cur_state);
468
489
/* Lock the closest parent nesting point for umount */
469
cache_multi_lock(me->parent);
470
if (umount_multi_triggers(ap, root, me, base)) {
490
cache_multi_writelock(me->parent);
491
if (umount_multi_triggers(ap, me, root, base)) {
472
493
"some offset mounts still present under %s", path);
572
588
if (ap->type == LKP_INDIRECT) {
573
589
if (umount_all(ap, force) && !force)
575
ret = umount_autofs_indirect(ap);
591
ret = umount_autofs_indirect(ap, root);
577
593
ret = umount_autofs_direct(ap);
582
int send_ready(unsigned logopt, int ioctlfd, unsigned int wait_queue_token)
584
char buf[MAX_ERR_BUF];
586
if (wait_queue_token == 0)
589
debug(logopt, "token = %d", wait_queue_token);
591
if (ioctl(ioctlfd, AUTOFS_IOC_READY, wait_queue_token) < 0) {
592
char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
593
logerr("AUTOFS_IOC_READY: error %s", estr);
599
int send_fail(unsigned logopt, int ioctlfd, unsigned int wait_queue_token)
601
char buf[MAX_ERR_BUF];
603
if (wait_queue_token == 0)
606
debug(logopt, "token = %d", wait_queue_token);
608
if (ioctl(ioctlfd, AUTOFS_IOC_FAIL, wait_queue_token) < 0) {
609
char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
610
logerr("AUTOFS_IOC_FAIL: error %s", estr);
616
598
static size_t get_kpkt_len(void)
618
600
size_t pkt_len = sizeof(struct autofs_v5_packet);
918
900
if (fds[1].revents & POLLIN) {
919
enum states next_state, post_state;
901
enum states next_state;
920
902
size_t read_size = sizeof(next_state);
923
next_state = post_state = ST_INVAL;
905
next_state = ST_INVAL;
925
state_mutex_lock(ap);
927
909
state_pipe = ap->state_pipe[0];
929
911
if (fullread(state_pipe, &next_state, read_size)) {
930
state_mutex_unlock(ap);
934
if (next_state != ST_INVAL && next_state != ap->state) {
935
if (next_state != ST_SHUTDOWN)
936
post_state = next_state;
938
ap->state = ST_SHUTDOWN;
941
state_mutex_unlock(ap);
943
if (post_state != ST_INVAL) {
944
if (post_state == ST_SHUTDOWN_PENDING ||
945
post_state == ST_SHUTDOWN_FORCE) {
949
st_add_task(ap, post_state);
952
918
if (next_state == ST_SHUTDOWN)
984
950
info(ap->logopt, "expiring path %s", buf);
952
pthread_cleanup_push(master_source_lock_cleanup, ap->entry);
953
master_source_readlock(ap->entry);
986
954
ret = umount_multi(ap, buf, 1);
988
956
info(ap->logopt, "expired %s", buf);
990
958
warn(ap->logopt, "couldn't complete expire of %s", buf);
959
pthread_cleanup_pop(1);
1062
1031
if (ap->type == LKP_DIRECT)
1063
1032
status = mount_autofs_direct(ap);
1065
status = mount_autofs_indirect(ap);
1034
status = mount_autofs_indirect(ap, root);
1067
1036
if (status < 0)
1070
ap->state = ST_READY;
1039
st_add_task(ap, ST_READY);
1075
1044
static int handle_packet(struct autofs_point *ap)
1077
union autofs_packet_union pkt;
1046
union autofs_v5_packet_union pkt;
1079
1048
if (get_pkt(ap, &pkt))
1411
static void mutex_operation_wait(pthread_mutex_t *mutex)
1416
* Unlock a mutex, but wait for a pending operation
1417
* if one is in progress
1419
status = pthread_mutex_trylock(mutex);
1421
if (status == EBUSY) {
1422
/* Mutex locked - do we own it */
1423
status = pthread_mutex_unlock(mutex);
1425
if (status != EPERM)
1430
status = pthread_mutex_lock(mutex);
1436
/* Operation complete, release it */
1437
status = pthread_mutex_unlock(mutex);
1441
status = pthread_mutex_unlock(mutex);
1393
int handle_mounts_startup_cond_init(struct startup_cond *suc)
1397
status = pthread_mutex_init(&suc->mutex, NULL);
1401
status = pthread_cond_init(&suc->cond, NULL);
1403
status = pthread_mutex_destroy(&suc->mutex);
1409
status = pthread_mutex_lock(&suc->mutex);
1411
status = pthread_mutex_destroy(&suc->mutex);
1414
status = pthread_cond_destroy(&suc->cond);
1422
void handle_mounts_startup_cond_destroy(void *arg)
1424
struct startup_cond *suc = (struct startup_cond *) arg;
1427
status = pthread_mutex_unlock(&suc->mutex);
1431
status = pthread_mutex_destroy(&suc->mutex);
1435
status = pthread_cond_destroy(&suc->cond);
1459
1452
submount = ap->submount;
1461
1454
strcpy(path, ap->path);
1462
if (!submount && strcmp(ap->path, "/-") && ap->dir_created)
1455
if (!submount && strcmp(ap->path, "/-") &&
1456
ap->flags & MOUNT_FLAG_DIR_CREATED)
1465
/* If we have been canceled then we may hold the state mutex. */
1466
mutex_operation_wait(&ap->state_mutex);
1469
st_remove_tasks(ap);
1471
umount_autofs(ap, 1);
1460
/* We are finishing up */
1461
ap->parent->submnt_count--;
1462
list_del_init(&ap->mounts);
1465
master_remove_mapent(ap->entry);
1466
master_source_unlock(ap->entry);
1469
mounts_mutex_unlock(ap->parent);
1470
master_source_unlock(ap->parent->entry);
1472
master_mutex_unlock();
1473
1474
destroy_logpri_fifo(ap);
1474
master_signal_submount(ap, MASTER_SUBMNT_JOIN);
1475
master_remove_mapent(ap->entry);
1476
1475
master_free_mapent_sources(ap->entry, 1);
1477
1476
master_free_mapent(ap->entry);
1482
1479
if (rmdir(path) == -1) {
1483
1480
char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
1498
1495
void *handle_mounts(void *arg)
1497
struct startup_cond *suc;
1500
1498
struct autofs_point *ap;
1501
1499
int cancel_state, status = 0;
1503
ap = (struct autofs_point *) arg;
1505
pthread_cleanup_push(return_start_status, &suc);
1502
suc = (struct startup_cond *) arg;
1505
root = strdup(suc->root);
1507
pthread_cleanup_push(return_start_status, suc);
1506
1508
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
1508
state_mutex_lock(ap);
1510
status = pthread_mutex_lock(&suc.mutex);
1510
status = pthread_mutex_lock(&suc->mutex);
1512
1512
logerr("failed to lock startup condition mutex!");
1516
if (mount_autofs(ap) < 0) {
1517
crit(ap->logopt, "failed to alloc string root");
1519
pthread_setcancelstate(cancel_state, NULL);
1523
if (mount_autofs(ap, root) < 0) {
1517
1524
crit(ap->logopt, "mount of %s failed!", ap->path);
1519
state_mutex_unlock(ap);
1520
umount_autofs(ap, 1);
1526
umount_autofs(ap, root, 1);
1521
1528
pthread_setcancelstate(cancel_state, NULL);
1522
1529
pthread_exit(NULL);
1525
if (ap->ghost && ap->type != LKP_DIRECT)
1534
if (ap->flags & MOUNT_FLAG_GHOST && ap->type != LKP_DIRECT)
1526
1535
info(ap->logopt, "ghosting enabled");
1529
1538
pthread_cleanup_pop(1);
1531
1540
/* We often start several automounters at the same time. Add some
1533
1542
if (!ap->submount && ap->exp_timeout)
1534
1543
alarm_add(ap, ap->exp_runfreq + rand() % ap->exp_runfreq);
1536
pthread_cleanup_push(handle_mounts_cleanup, ap);
1537
1545
pthread_setcancelstate(cancel_state, NULL);
1539
state_mutex_unlock(ap);
1541
1547
while (ap->state != ST_SHUTDOWN) {
1542
1548
if (handle_packet(ap)) {
1545
state_mutex_lock(ap);
1552
* If we're a submount we need to ensure our parent
1553
* doesn't try to mount us again until our shutdown
1554
* is complete and that any outstanding mounts are
1555
* completed before we try to shutdown.
1557
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cur_state);
1559
master_mutex_lock();
1562
master_source_writelock(ap->parent->entry);
1563
mounts_mutex_lock(ap->parent);
1566
master_source_writelock(ap->entry);
1568
if (ap->state != ST_SHUTDOWN) {
1570
alarm_add(ap, ap->exp_runfreq);
1571
/* Return to ST_READY is done immediately */
1572
st_add_task(ap, ST_READY);
1573
master_source_unlock(ap->entry);
1575
mounts_mutex_unlock(ap->parent);
1576
master_source_unlock(ap->parent->entry);
1579
master_mutex_unlock();
1581
pthread_setcancelstate(cur_state, NULL);
1586
st_remove_tasks(ap);
1587
st_wait_task(ap, ST_ANY, 0);
1547
1590
* For a direct mount map all mounts have already gone
1548
* by the time we get here.
1591
* by the time we get here and since we only ever
1592
* umount direct mounts at shutdown there is no need
1593
* to check for possible recovery.
1550
1595
if (ap->type == LKP_DIRECT) {
1552
state_mutex_unlock(ap);
1596
umount_autofs(ap, NULL, 1);
1557
* If the ioctl fails assume the kernel doesn't have
1558
* AUTOFS_IOC_ASKUMOUNT and just continue.
1601
* If umount_autofs returns non-zero it wasn't able
1602
* to complete the umount and has left the mount intact
1603
* so we can continue. This can happen if a lookup
1604
* occurs while we're trying to umount.
1560
ret = ioctl(ap->ioctlfd, AUTOFS_IOC_ASKUMOUNT, &result);
1562
state_mutex_unlock(ap);
1606
ret = umount_autofs(ap, NULL, 1);
1567
if (ap->state == ST_SHUTDOWN) {
1569
state_mutex_unlock(ap);
1572
#ifdef ENABLE_IGNORE_BUSY_MOUNTS
1574
* There weren't any active mounts but if the
1575
* filesystem is busy there may be a mount
1576
* request in progress so return to the ready
1577
* state unless a shutdown has been explicitly
1581
state_mutex_unlock(ap);
1587
1610
/* Failed shutdown returns to ready */
1588
1611
warn(ap->logopt,
1591
1614
if (!ap->submount)
1592
1615
alarm_add(ap, ap->exp_runfreq);
1593
nextstate(ap->state_pipe[1], ST_READY);
1595
state_mutex_unlock(ap);
1616
/* Return to ST_READY is done immediately */
1617
st_add_task(ap, ST_READY);
1618
master_source_unlock(ap->entry);
1620
mounts_mutex_unlock(ap->parent);
1621
master_source_unlock(ap->parent->entry);
1624
master_mutex_unlock();
1626
pthread_setcancelstate(cur_state, NULL);
1599
pthread_cleanup_pop(1);
1631
handle_mounts_cleanup(ap);
1620
static int is_automount_running(void)
1624
struct dirent entry;
1625
struct dirent *result;
1626
char path[PATH_MAX + 1], buf[PATH_MAX];
1628
if ((dir = opendir("/proc")) == NULL) {
1629
fprintf(stderr, "cannot opendir(/proc)\n");
1633
while (readdir_r(dir, &entry, &result) == 0) {
1634
int path_len, pid = 0;
1639
if (*entry.d_name == '.')
1642
if (!strcmp(entry.d_name, "self"))
1645
if (!isdigit(*entry.d_name))
1648
pid = atoi(entry.d_name);
1649
if (pid == getpid())
1652
path_len = sprintf(path, "/proc/%s/cmdline", entry.d_name);
1653
if (path_len >= PATH_MAX) {
1655
"buffer to small for /proc path\n");
1658
path[path_len] = '\0';
1660
fp = fopen(path, "r");
1664
while (len < 127 && (c = fgetc(fp)) != EOF && c)
1668
if (strstr(buf, "automount"))
1678
1651
static void usage(void)
1680
1653
fprintf(stderr,
1694
1667
" specify global mount options\n"
1695
1668
" -l --set-log-priority priority path [path,...]\n"
1696
1669
" set daemon log verbosity\n"
1670
" -C --dont-check-daemon\n"
1671
" don't check if daemon is already running\n"
1697
1672
" -V --version print version, build config and exit\n"
1833
1808
{"global-options", 1, 0, 'O'},
1834
1809
{"version", 0, 0, 'V'},
1835
1810
{"set-log-priority", 1, 0, 'l'},
1811
{"dont-check-daemon", 0, 0, 'C'},