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-all-mountpoints.c : Cleanup all mount points in
5*2912Sartem * /media/.hal-mtab that is currently unused
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(void)49*2912Sartem do_cleanup (void)
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
59*2912Sartem hal_mtab_orig = fopen ("/media/.hal-mtab", "r");
60*2912Sartem if (hal_mtab_orig == NULL) {
61*2912Sartem unknown_error ("Cannot open /media/.hal-mtab");
62*2912Sartem }
63*2912Sartem if (fseek (hal_mtab_orig, 0L, SEEK_END) != 0) {
64*2912Sartem unknown_error ("Cannot seek to end of /media/.hal-mtab");
65*2912Sartem }
66*2912Sartem hal_mtab_orig_len = ftell (hal_mtab_orig);
67*2912Sartem if (hal_mtab_orig_len < 0) {
68*2912Sartem unknown_error ("Cannot determine size of /media/.hal-mtab");
69*2912Sartem }
70*2912Sartem rewind (hal_mtab_orig);
71*2912Sartem hal_mtab_buf = g_new0 (char, hal_mtab_orig_len + 1);
72*2912Sartem num_read = fread (hal_mtab_buf, 1, hal_mtab_orig_len, hal_mtab_orig);
73*2912Sartem if (num_read != hal_mtab_orig_len) {
74*2912Sartem unknown_error ("Cannot read from /media/.hal-mtab");
75*2912Sartem }
76*2912Sartem fclose (hal_mtab_orig);
77*2912Sartem
78*2912Sartem #ifdef DEBUG
79*2912Sartem printf ("hal_mtab = '%s'\n", hal_mtab_buf);
80*2912Sartem #endif
81*2912Sartem
82*2912Sartem lines = g_strsplit (hal_mtab_buf, "\n", 0);
83*2912Sartem g_free (hal_mtab_buf);
84*2912Sartem
85*2912Sartem /* find the entry we're going to unmount */
86*2912Sartem for (i = 0; lines[i] != NULL; i++) {
87*2912Sartem char **line_elements;
88*2912Sartem
89*2912Sartem #ifdef DEBUG
90*2912Sartem printf (" line = '%s'\n", lines[i]);
91*2912Sartem #endif
92*2912Sartem
93*2912Sartem if ((lines[i])[0] == '#')
94*2912Sartem continue;
95*2912Sartem
96*2912Sartem line_elements = g_strsplit (lines[i], "\t", 6);
97*2912Sartem if (g_strv_length (line_elements) == 6) {
98*2912Sartem char *mount_point;
99*2912Sartem
100*2912Sartem #ifdef DEBUG
101*2912Sartem printf (" devfile = '%s'\n", line_elements[0]);
102*2912Sartem printf (" uid = '%s'\n", line_elements[1]);
103*2912Sartem printf (" session id = '%s'\n", line_elements[2]);
104*2912Sartem printf (" fs = '%s'\n", line_elements[3]);
105*2912Sartem printf (" options = '%s'\n", line_elements[4]);
106*2912Sartem printf (" mount_point = '%s'\n", line_elements[5]);
107*2912Sartem #endif
108*2912Sartem
109*2912Sartem /* just try to rmdir the entry; if it's non-empty or something is mounted on it,
110*2912Sartem * this will fail
111*2912Sartem */
112*2912Sartem mount_point = line_elements[5];
113*2912Sartem
114*2912Sartem /* remove directory */
115*2912Sartem if (g_rmdir (mount_point) == 0) {
116*2912Sartem char *line_to_free;
117*2912Sartem
118*2912Sartem printf ("Removed mount_point '%s'", mount_point);
119*2912Sartem
120*2912Sartem line_to_free = lines[i];
121*2912Sartem for (j = i; lines[j] != NULL; j++) {
122*2912Sartem lines[j] = lines[j+1];
123*2912Sartem }
124*2912Sartem lines[j] = NULL;
125*2912Sartem g_free (line_to_free);
126*2912Sartem
127*2912Sartem /* we've moved the lines one back, so make sure we don't advance to next line */
128*2912Sartem i--;
129*2912Sartem }
130*2912Sartem }
131*2912Sartem
132*2912Sartem g_strfreev (line_elements);
133*2912Sartem }
134*2912Sartem
135*2912Sartem /* create new .hal-mtab~ file without the entries we've removed */
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 (strlen (lines[i]) > 0) {
142*2912Sartem char anewl[2] = "\n\0";
143*2912Sartem if (fwrite (lines[i], 1, strlen (lines[i]), hal_mtab_new) != strlen (lines[i])) {
144*2912Sartem unknown_error ("Cannot write to /media/.hal-mtab~");
145*2912Sartem }
146*2912Sartem if (fwrite (anewl, 1, 1, hal_mtab_new) != 1) {
147*2912Sartem unknown_error ("Cannot write to /media/.hal-mtab~");
148*2912Sartem }
149*2912Sartem }
150*2912Sartem }
151*2912Sartem fclose (hal_mtab_new);
152*2912Sartem
153*2912Sartem g_strfreev (lines);
154*2912Sartem
155*2912Sartem /* set new .hal-mtab file */
156*2912Sartem if (rename ("/media/.hal-mtab~", "/media/.hal-mtab") != 0) {
157*2912Sartem unlink ("/media/.hal-mtab~");
158*2912Sartem unknown_error ("Cannot rename /media/.hal-mtab~ to /media/.hal-mtab");
159*2912Sartem }
160*2912Sartem }
161*2912Sartem
162*2912Sartem int
main(int argc,char * argv[])163*2912Sartem main (int argc, char *argv[])
164*2912Sartem {
165*2912Sartem if (!lock_hal_mtab ()) {
166*2912Sartem unknown_error ("Cannot obtain lock on /media/.hal-mtab");
167*2912Sartem }
168*2912Sartem
169*2912Sartem if (getenv ("HAL_PROP_INFO_UDI") == NULL)
170*2912Sartem usage ();
171*2912Sartem
172*2912Sartem #ifdef DEBUG
173*2912Sartem printf ("in hal-storage-cleanup-all-mountpoints\n");
174*2912Sartem #endif
175*2912Sartem do_cleanup ();
176*2912Sartem
177*2912Sartem
178*2912Sartem unlock_hal_mtab ();
179*2912Sartem return 0;
180*2912Sartem }
181