aboutsummaryrefslogtreecommitdiffstats
path: root/modules/mount_changer.c
blob: 2303ba0f10a7919d5d25e7a11cc732616c50f06b (plain)
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
/* ----------------------------------------------------------------------- *
 *   
 *  mount_changer.c - module for Linux automountd to mount filesystems
 *                    from cd changers
 *
 *   Copyright 1999 Toby Jaffey - All Rights Reserved
 *   CD swapping code from linux kernel in Documentation/cdrom/ide-cd
 * Based on code originally from Gerhard Zuber <zuber@berlin.snafu.de>.
 * Changer status information, and rewrite for the new Uniform CDROM driver
 * interface by Erik Andersen <andersee@debian.org>.
 *
 *   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 <malloc.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <linux/cdrom.h>

#define MODULE_MOUNT
#include "automount.h"

#define MODPREFIX "mount(changer): "
int mount_version = AUTOFS_MOUNT_VERSION; /* Required by protocol */

int swapCD(const char *device, const char *slotName);

int mount_init(void **context)
{
return 0;
}

int mount_mount(const char *root, const char *name, int name_len,
		const char *what, const char *fstype, const char *options,
		void *context)
{
  char *fullpath;
  int err;

  fstype = "iso9660";

  fullpath = alloca(strlen(root)+name_len+2);
  if ( !fullpath ) {
    syslog(LOG_ERR, MODPREFIX "alloca: %m");
    return 1;
  }
  sprintf(fullpath, "%s/%s", root, name);

  syslog(LOG_DEBUG, MODPREFIX "calling umount %s", what);
  err = spawnl(LOG_DEBUG, "/bin/umount", "/bin/umount", what, NULL);
  if ( err ) {
    syslog(LOG_DEBUG, MODPREFIX "umount of %s failed (all may be unmounted)", what);
  }

  syslog(LOG_DEBUG, MODPREFIX "calling mkdir %s", fullpath);
  if ( mkdir(fullpath, 0555) && errno != EEXIST ) {
    syslog(LOG_NOTICE, MODPREFIX "mkdir %s failed: %m", name);
    return 1;
  }

  syslog(LOG_NOTICE, MODPREFIX "Swapping CD to slot %s", name);
  err = swapCD(what, name);
  if ( err ) {
    syslog(LOG_NOTICE, MODPREFIX "failed to swap CD to slot %s", name);
    return 1;
  }
  if ( options ) {
    syslog(LOG_DEBUG, MODPREFIX "calling mount -t %s " SLOPPY "-o %s %s %s",
	   fstype, options, what, fullpath);
    err = spawnl(LOG_DEBUG, PATH_MOUNT, PATH_MOUNT, "-t", fstype, what, SLOPPYOPT "-o", options, what, fullpath, NULL);
  } else {
    syslog(LOG_DEBUG, MODPREFIX "calling mount -t %s %s %s",
	   fstype, what, fullpath);
      err = spawnl(LOG_DEBUG, PATH_MOUNT, PATH_MOUNT, "-t", fstype, what, fullpath, NULL);
  }
  if ( err ) {
    rmdir(fullpath);
    syslog(LOG_NOTICE, MODPREFIX "failed to mount %s (type %s) on %s",
	   what, fstype, fullpath);
    return 1;
  } else {
    syslog(LOG_DEBUG, MODPREFIX "mounted %s type %s on %s",
	   what, fstype, fullpath);
    return 0;
  }
}

int mount_done(void *context)
{
  return 0;
}

int swapCD(const char *device, const char *slotName)
{
int fd;           /* file descriptor for CD-ROM device */
int status;       /* return status for system calls */
int slot=-1;
int total_slots_available;

slot = atoi (slotName) - 1;

/* open device */ 
fd = open(device, O_RDONLY | O_NONBLOCK);
if (fd < 0)
	{
	syslog(LOG_NOTICE, MODPREFIX "Opening device %s failed : %m", device);
	return 1;
	}

/* Check CD player status */ 
total_slots_available = ioctl (fd, CDROM_CHANGER_NSLOTS);
if (total_slots_available <= 1 )
	{
	syslog(LOG_NOTICE, MODPREFIX "Device %s is not an ATAPI compliant CD changer.\n", device);
	return 1;
	}

/* load */ 
slot=ioctl (fd, CDROM_SELECT_DISC, slot);
if (slot<0)
	{
	syslog(LOG_NOTICE, MODPREFIX "CDROM_SELECT_DISC failed");
	return 1;
	}

/* close device */
status = close (fd);
if (status != 0)
	{
	syslog(LOG_NOTICE, MODPREFIX "close failed for `%s': %m\n", device);
	return 1;
	}
return 0;
}