~ubuntu-branches/ubuntu/precise/autofs5/precise

« back to all changes in this revision

Viewing changes to .pc/autofs-5.0.5-make-verbose-mode-a-little-less-verbose.patch/modules/mount_changer.c

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2011-07-03 14:35:46 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110703143546-nej26krjij0rf792
Tags: 5.0.6-0ubuntu1
* New upstream release:
  - Dropped upstream patches 
  - Refreshed debian/patches/17ld.patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* ----------------------------------------------------------------------- *
2
 
 *   
3
 
 *  mount_changer.c - module for Linux automountd to mount filesystems
4
 
 *                    from cd changers
5
 
 *
6
 
 *   Copyright 1999 Toby Jaffey - All Rights Reserved
7
 
 *   CD swapping code from linux kernel in Documentation/cdrom/ide-cd
8
 
 * Based on code originally from Gerhard Zuber <zuber@berlin.snafu.de>.
9
 
 * Changer status information, and rewrite for the new Uniform CDROM driver
10
 
 * interface by Erik Andersen <andersee@debian.org>.
11
 
 *
12
 
 *   This program is free software; you can redistribute it and/or modify
13
 
 *   it under the terms of the GNU General Public License as published by
14
 
 *   the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
15
 
 *   USA; either version 2 of the License, or (at your option) any later
16
 
 *   version; incorporated herein by reference.
17
 
 *
18
 
 * ----------------------------------------------------------------------- */
19
 
 
20
 
#include <stdio.h>
21
 
#include <malloc.h>
22
 
#include <string.h>
23
 
#include <sys/param.h>
24
 
#include <sys/types.h>
25
 
#include <sys/stat.h>
26
 
#include <stdlib.h>
27
 
#include <sys/ioctl.h>
28
 
#include <linux/cdrom.h>
29
 
 
30
 
#define MODULE_MOUNT
31
 
#include "automount.h"
32
 
 
33
 
#define MODPREFIX "mount(changer): "
34
 
 
35
 
int mount_version = AUTOFS_MOUNT_VERSION;       /* Required by protocol */
36
 
 
37
 
int swapCD(const char *device, const char *slotName);
38
 
 
39
 
int mount_init(void **context)
40
 
{
41
 
        return 0;
42
 
}
43
 
 
44
 
int mount_mount(struct autofs_point *ap, const char *root, const char *name, int name_len,
45
 
                const char *what, const char *fstype, const char *options, void *context)
46
 
{
47
 
        char fullpath[PATH_MAX];
48
 
        char buf[MAX_ERR_BUF];
49
 
        int err;
50
 
        int len, status, existed = 1;
51
 
 
52
 
        if (ap->flags & MOUNT_FLAG_REMOUNT)
53
 
                return 0;
54
 
 
55
 
        fstype = "iso9660";
56
 
 
57
 
        /* Root offset of multi-mount */
58
 
        len = strlen(root);
59
 
        if (root[len - 1] == '/') {
60
 
                len = snprintf(fullpath, len, "%s", root);
61
 
        } else if (*name == '/') {
62
 
                /*
63
 
                 * Direct or offset mount, name is absolute path so
64
 
                 * don't use root (but with move mount changes root
65
 
                 * is now the same as name).
66
 
                 */
67
 
                len = sprintf(fullpath, "%s", root);
68
 
        } else {
69
 
                len = sprintf(fullpath, "%s/%s", root, name);
70
 
        }
71
 
        fullpath[len] = '\0';
72
 
 
73
 
        debug(ap->logopt, MODPREFIX "calling umount %s", what);
74
 
 
75
 
        err = spawn_umount(ap->logopt, what, NULL);
76
 
        if (err) {
77
 
                error(ap->logopt,
78
 
                      MODPREFIX
79
 
                      "umount of %s failed (all may be unmounted)",
80
 
                      what);
81
 
        }
82
 
 
83
 
        debug(ap->logopt, MODPREFIX "calling mkdir_path %s", fullpath);
84
 
 
85
 
        status = mkdir_path(fullpath, 0555);
86
 
        if (status && errno != EEXIST) {
87
 
                char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
88
 
                error(ap->logopt,
89
 
                      MODPREFIX "mkdir_path %s failed: %s", fullpath, estr);
90
 
                return 1;
91
 
        }
92
 
 
93
 
        if (!status)
94
 
                existed = 0;
95
 
 
96
 
        debug(ap->logopt, MODPREFIX "Swapping CD to slot %s", name);
97
 
 
98
 
        err = swapCD(what, name);
99
 
        if (err) {
100
 
                error(ap->logopt,
101
 
                      MODPREFIX "failed to swap CD to slot %s", name);
102
 
                return 1;
103
 
        }
104
 
 
105
 
        if (options && options[0]) {
106
 
                debug(ap->logopt,
107
 
                      MODPREFIX "calling mount -t %s " SLOPPY "-o %s %s %s",
108
 
                      fstype, options, what, fullpath);
109
 
 
110
 
                err = spawn_mount(ap->logopt, "-t", fstype,
111
 
                             SLOPPYOPT "-o", options, what, fullpath, NULL);
112
 
        } else {
113
 
                debug(ap->logopt,
114
 
                      MODPREFIX "calling mount -t %s %s %s",
115
 
                      fstype, what, fullpath);
116
 
 
117
 
                err = spawn_mount(ap->logopt, "-t", fstype, what, fullpath, NULL);
118
 
        }
119
 
 
120
 
        if (err) {
121
 
                info(ap->logopt, MODPREFIX "failed to mount %s (type %s) on %s",
122
 
                    what, fstype, fullpath);
123
 
 
124
 
                if (ap->type != LKP_INDIRECT)
125
 
                        return 1;
126
 
 
127
 
                if ((!(ap->flags & MOUNT_FLAG_GHOST) && name_len) || !existed)
128
 
                        rmdir_path(ap, fullpath, ap->dev);
129
 
 
130
 
                return 1;
131
 
        } else {
132
 
                info(ap->logopt, MODPREFIX "mounted %s type %s on %s",
133
 
                    what, fstype, fullpath);
134
 
                return 0;
135
 
        }
136
 
}
137
 
 
138
 
int mount_done(void *context)
139
 
{
140
 
        return 0;
141
 
}
142
 
 
143
 
int swapCD(const char *device, const char *slotName)
144
 
{
145
 
        int fd;                 /* file descriptor for CD-ROM device */
146
 
        int status;             /* return status for system calls */
147
 
        int slot = -1;
148
 
        int total_slots_available;
149
 
 
150
 
        slot = atoi(slotName) - 1;
151
 
 
152
 
        /* open device */
153
 
        fd = open_fd(device, O_RDONLY | O_NONBLOCK);
154
 
        if (fd < 0) {
155
 
                logerr(MODPREFIX "Opening device %s failed : %s",
156
 
                      device, strerror(errno));
157
 
                return 1;
158
 
        }
159
 
 
160
 
        /* Check CD player status */
161
 
        total_slots_available = ioctl(fd, CDROM_CHANGER_NSLOTS);
162
 
        if (total_slots_available <= 1) {
163
 
                logerr(MODPREFIX
164
 
                      "Device %s is not an ATAPI compliant CD changer.",
165
 
                      device);
166
 
                close(fd);
167
 
                return 1;
168
 
        }
169
 
 
170
 
        /* load */
171
 
        slot = ioctl(fd, CDROM_SELECT_DISC, slot);
172
 
        if (slot < 0) {
173
 
                logerr(MODPREFIX "CDROM_SELECT_DISC failed");
174
 
                close(fd);
175
 
                return 1;
176
 
        }
177
 
 
178
 
        /* close device */
179
 
        status = close(fd);
180
 
        if (status != 0) {
181
 
                logerr(MODPREFIX "close failed for `%s': %s",
182
 
                      device, strerror(errno));
183
 
                return 1;
184
 
        }
185
 
        return 0;
186
 
}