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