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