File: cdpinger.c

package info (click to toggle)
ltspfs 1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 396 kB
  • sloc: ansic: 2,104; sh: 276; python: 149; makefile: 34
file content (170 lines) | stat: -rw-r--r-- 4,718 bytes parent folder | download
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
/*
 * Copyright Scott Balneaves, sbalneav@ltsp.org, 2007, 2008
 *           Vagrant Cascadian, vagrant@freegeek.org, 2008
 *           Gideon Romm, gadi@ltsp.org, 2009

 * 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; either version 2 of the
 * License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, you can find it on the World Wide
 * Web at http://www.gnu.org/copyleft/gpl.html, or write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
 * MA 02110-1301, USA.

 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/cdrom.h>
#include <glib.h>
#include <syslog.h>

#define POLL_INTERVAL 3				/* Check cdrom every 3 secs */

/*
 * enum for our mount states
 */

typedef enum {
    CDP_CDROM_MOUNT,
    CDP_CDROM_UMOUNT
} CdpMountType;

char *device;

/*
 * check_cd_status:  Switch mounted and unmounted states in the cdrom, and
 * call out our shell scripts to handle the actual work of plumbing the
 * ltspfs connection.
 */

gboolean
check_cd_status(void)
{
    char dev[BUFSIZ], cmd[BUFSIZ];
    int cdrom, status;
    static CdpMountType last = CDP_CDROM_UMOUNT;

    snprintf(dev, sizeof dev, "/dev/%s", device);

    /*
     * Try to open the cdrom device.  It might not exist, for a usb cdrom,
     * so simply return false so we exit.  Device has been unplugged.
     * Make sure it's unmounted, and exit.
     */

    if ((cdrom = open(dev, O_RDONLY|O_NONBLOCK)) < 0) {
	syslog (LOG_ERR, "Device %s disappeared.  Exiting.", device);
        if (last == CDP_CDROM_MOUNT) {
            snprintf(cmd, sizeof cmd,
                    "/lib/udev/ltspfs_entry remove_disc %s", device);
            g_spawn_command_line_sync(cmd, NULL, NULL, NULL, NULL);
        }
        return FALSE;
    }

    /*
     * Talk to the cdrom, get the status, and close it.
     */

    ioctl(cdrom, CDROM_LOCKDOOR, 0);
    status = ioctl (cdrom, CDROM_DRIVE_STATUS, 0);
    close(cdrom);

    /*
     * Something badly wrong with the cdrom.  Don't bother talking to it
     * anymore.
     */

    if (status < 0) {
	syslog (LOG_ERR, "ioctl(CDROM_DRIVE_STATUS) failed.  Exiting.");
        snprintf(cmd, sizeof cmd,
                "/lib/udev/ltspfs_entry remove_disc %s", device);
        return g_spawn_command_line_sync(cmd, NULL, NULL, NULL, NULL);
        return FALSE;
    }

    /*
     * Look at cdrom, see if we've got a disk or not, and mount or umount
     * appropriately
     */

    switch(status) {
        case CDS_NO_DISC:
        case CDS_TRAY_OPEN:
            if (last == CDP_CDROM_MOUNT) {
                syslog (LOG_INFO, "Tray open.  Unmounting");
                snprintf(cmd, sizeof cmd,
                        "/lib/udev/ltspfs_entry remove_disc %s", device);
                last = CDP_CDROM_UMOUNT;
                return g_spawn_command_line_sync(cmd, NULL, NULL, NULL, NULL);
            }
            break;
        case CDS_DISC_OK:
            if (last == CDP_CDROM_UMOUNT) {
                syslog (LOG_INFO, "Disk detected.  Mounting");
                snprintf(cmd, sizeof cmd,
                        "/lib/udev/ltspfs_entry add_disc %s iso9660,udf", device);
                last = CDP_CDROM_MOUNT;
                return g_spawn_command_line_sync(cmd, NULL, NULL, NULL, NULL);
            }
            break;
    }

    return TRUE;
}

/*
 * end_cd_watch: Callback routine error routine for the timeout.  If
 * check_cd_status has returned false, somthing's happened, like, say
 * the USB cdrom being unplugged, so just exit the main loop.
 */

void
end_cd_watch(gpointer loop)
{
    g_main_loop_quit((GMainLoop *)loop);
}

int
main(int argc, char *argv[])
{
    GMainLoop *loop;

    if (argc != 2)
        exit(1);

    openlog (argv[0], LOG_PID | LOG_NOWAIT, LOG_DAEMON);
    g_type_init();

    device = argv[1];

    if (daemon(0,0))
        exit(1);

    syslog (LOG_INFO, "starting, device = %s", argv[1]);

    loop = g_main_loop_new (NULL, FALSE);

    /* Add a timeout */
    g_timeout_add_full(G_PRIORITY_DEFAULT, POLL_INTERVAL * 1000,
            (GSourceFunc)check_cd_status, NULL,
	    (GDestroyNotify)end_cd_watch);

    g_main_loop_run(loop);

    closelog ();
    return 0;
}