1*2912Sartem /***************************************************************************
2*2912Sartem * CVSID: $Id: hal-storage-mount.c,v 1.7 2006/06/21 00:44:03 david Exp $
3*2912Sartem *
4*2912Sartem * hal-storage-cleanup-mountpoint.c : Cleanup mount point when hald detects
5*2912Sartem * that an unmount not done through Unmount()
6*2912Sartem *
7*2912Sartem * Copyright (C) 2006 David Zeuthen, <david@fubar.dk>
8*2912Sartem *
9*2912Sartem * This program is free software; you can redistribute it and/or modify
10*2912Sartem * it under the terms of the GNU General Public License as published by
11*2912Sartem * the Free Software Foundation; either version 2 of the License, or
12*2912Sartem * (at your option) any later version.
13*2912Sartem *
14*2912Sartem * This program is distributed in the hope that it will be useful,
15*2912Sartem * but WITHOUT ANY WARRANTY; without even the implied warranty of
16*2912Sartem * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17*2912Sartem * GNU General Public License for more details.
18*2912Sartem *
19*2912Sartem * You should have received a copy of the GNU General Public License
20*2912Sartem * along with this program; if not, write to the Free Software
21*2912Sartem * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22*2912Sartem *
23*2912Sartem **************************************************************************/
24*2912Sartem
25*2912Sartem #ifdef HAVE_CONFIG_H
26*2912Sartem # include <config.h>
27*2912Sartem #endif
28*2912Sartem
29*2912Sartem #include <unistd.h>
30*2912Sartem #include <stdio.h>
31*2912Sartem #include <stdlib.h>
32*2912Sartem #include <string.h>
33*2912Sartem #include <glib.h>
34*2912Sartem #include <glib/gstdio.h>
35*2912Sartem
36*2912Sartem #include "hal-storage-shared.h"
37*2912Sartem
38*2912Sartem /*#define DEBUG*/
39*2912Sartem #define DEBUG
40*2912Sartem
41*2912Sartem static void
usage(void)42*2912Sartem usage (void)
43*2912Sartem {
44*2912Sartem fprintf (stderr, "This program should only be started by hald.\n");
45*2912Sartem exit (1);
46*2912Sartem }
47*2912Sartem
48*2912Sartem static void
do_cleanup(const char * mount_point)49*2912Sartem do_cleanup (const char *mount_point)
50*2912Sartem {
51*2912Sartem int i, j;
52*2912Sartem FILE *hal_mtab_orig;
53*2912Sartem int hal_mtab_orig_len;
54*2912Sartem int num_read;
55*2912Sartem char *hal_mtab_buf;
56*2912Sartem char **lines;
57*2912Sartem FILE *hal_mtab_new;
58*2912Sartem gboolean found;
59*2912Sartem
60*2912Sartem hal_mtab_orig = fopen ("/media/.hal-mtab", "r");
61*2912Sartem if (hal_mtab_orig == NULL) {
62*2912Sartem unknown_error ("Cannot open /media/.hal-mtab");
63*2912Sartem }
64*2912Sartem if (fseek (hal_mtab_orig, 0L, SEEK_END) != 0) {
65*2912Sartem unknown_error ("Cannot seek to end of /media/.hal-mtab");
66*2912Sartem }
67*2912Sartem hal_mtab_orig_len = ftell (hal_mtab_orig);
68*2912Sartem if (hal_mtab_orig_len < 0) {
69*2912Sartem unknown_error ("Cannot determine size of /media/.hal-mtab");
70*2912Sartem }
71*2912Sartem rewind (hal_mtab_orig);
72*2912Sartem hal_mtab_buf = g_new0 (char, hal_mtab_orig_len + 1);
73*2912Sartem num_read = fread (hal_mtab_buf, 1, hal_mtab_orig_len, hal_mtab_orig);
74*2912Sartem if (num_read != hal_mtab_orig_len) {
75*2912Sartem unknown_error ("Cannot read from /media/.hal-mtab");
76*2912Sartem }
77*2912Sartem fclose (hal_mtab_orig);
78*2912Sartem
79*2912Sartem #ifdef DEBUG
80*2912Sartem printf ("hal_mtab = '%s'\n", hal_mtab_buf);
81*2912Sartem #endif
82*2912Sartem
83*2912Sartem lines = g_strsplit (hal_mtab_buf, "\n", 0);
84*2912Sartem g_free (hal_mtab_buf);
85*2912Sartem
86*2912Sartem /* find the entry we're going to unmount */
87*2912Sartem found = FALSE;
88*2912Sartem for (i = 0; lines[i] != NULL && !found; i++) {
89*2912Sartem char **line_elements;
90*2912Sartem
91*2912Sartem #ifdef DEBUG
92*2912Sartem printf (" line = '%s'\n", lines[i]);
93*2912Sartem #endif
94*2912Sartem
95*2912Sartem if ((lines[i])[0] == '#')
96*2912Sartem continue;
97*2912Sartem
98*2912Sartem line_elements = g_strsplit (lines[i], "\t", 6);
99*2912Sartem if (g_strv_length (line_elements) == 6) {
100*2912Sartem
101*2912Sartem #ifdef DEBUG
102*2912Sartem printf (" devfile = '%s'\n", line_elements[0]);
103*2912Sartem printf (" uid = '%s'\n", line_elements[1]);
104*2912Sartem printf (" session id = '%s'\n", line_elements[2]);
105*2912Sartem printf (" fs = '%s'\n", line_elements[3]);
106*2912Sartem printf (" options = '%s'\n", line_elements[4]);
107*2912Sartem printf (" mount_point = '%s'\n", line_elements[5]);
108*2912Sartem #endif
109*2912Sartem
110*2912Sartem if (strcmp (line_elements[5], mount_point) == 0) {
111*2912Sartem char *line_to_free;
112*2912Sartem
113*2912Sartem found = TRUE;
114*2912Sartem
115*2912Sartem line_to_free = lines[i];
116*2912Sartem for (j = i; lines[j] != NULL; j++) {
117*2912Sartem lines[j] = lines[j+1];
118*2912Sartem }
119*2912Sartem lines[j] = NULL;
120*2912Sartem g_free (line_to_free);
121*2912Sartem }
122*2912Sartem }
123*2912Sartem
124*2912Sartem g_strfreev (line_elements);
125*2912Sartem }
126*2912Sartem
127*2912Sartem if (!found) {
128*2912Sartem unknown_error ("mount point is not /media/.hal-mtab");
129*2912Sartem }
130*2912Sartem
131*2912Sartem #ifdef DEBUG
132*2912Sartem printf ("Found entry for mount point '%s' in /media/.hal-mtab", mount_point);
133*2912Sartem #endif
134*2912Sartem
135*2912Sartem /* create new .hal-mtab~ file without the entry we're going to unmount */
136*2912Sartem hal_mtab_new = fopen ("/media/.hal-mtab~", "w");
137*2912Sartem if (hal_mtab_new == NULL) {
138*2912Sartem unknown_error ("Cannot create /media/.hal-mtab~");
139*2912Sartem }
140*2912Sartem for (i = 0; lines[i] != NULL; i++) {
141*2912Sartem if (i > 0) {
142*2912Sartem char anewl[2] = "\n\0";
143*2912Sartem if (fwrite (anewl, 1, 1, hal_mtab_new) != 1) {
144*2912Sartem unknown_error ("Cannot write to /media/.hal-mtab~");
145*2912Sartem }
146*2912Sartem }
147*2912Sartem
148*2912Sartem if (fwrite (lines[i], 1, strlen (lines[i]), hal_mtab_new) != strlen (lines[i])) {
149*2912Sartem unknown_error ("Cannot write to /media/.hal-mtab~");
150*2912Sartem }
151*2912Sartem
152*2912Sartem }
153*2912Sartem fclose (hal_mtab_new);
154*2912Sartem
155*2912Sartem g_strfreev (lines);
156*2912Sartem
157*2912Sartem /* remove directory */
158*2912Sartem if (g_rmdir (mount_point) != 0) {
159*2912Sartem unlink ("/media/.hal-mtab~");
160*2912Sartem unknown_error ("Cannot remove directory");
161*2912Sartem }
162*2912Sartem
163*2912Sartem /* set new .hal-mtab file */
164*2912Sartem if (rename ("/media/.hal-mtab~", "/media/.hal-mtab") != 0) {
165*2912Sartem unlink ("/media/.hal-mtab~");
166*2912Sartem unknown_error ("Cannot rename /media/.hal-mtab~ to /media/.hal-mtab");
167*2912Sartem }
168*2912Sartem
169*2912Sartem }
170*2912Sartem
171*2912Sartem int
main(int argc,char * argv[])172*2912Sartem main (int argc, char *argv[])
173*2912Sartem {
174*2912Sartem char *mount_point;
175*2912Sartem
176*2912Sartem if (!lock_hal_mtab ()) {
177*2912Sartem unknown_error ("Cannot obtain lock on /media/.hal-mtab");
178*2912Sartem }
179*2912Sartem
180*2912Sartem mount_point = getenv ("HALD_CLEANUP");
181*2912Sartem if (mount_point == NULL)
182*2912Sartem usage ();
183*2912Sartem
184*2912Sartem #ifdef DEBUG
185*2912Sartem printf ("in hal-storage-cleanup-mountpoint for mount point '%s'\n", mount_point);
186*2912Sartem #endif
187*2912Sartem do_cleanup (mount_point);
188*2912Sartem
189*2912Sartem
190*2912Sartem unlock_hal_mtab ();
191*2912Sartem return 0;
192*2912Sartem }
193