xref: /titanic_41/usr/src/cmd/hal/tools/hal-storage-cleanup-mountpoint.c (revision 74e20cfe817b82802b16fac8690dadcda76f54f5)
1 /***************************************************************************
2  * CVSID: $Id: hal-storage-mount.c,v 1.7 2006/06/21 00:44:03 david Exp $
3  *
4  * hal-storage-cleanup-mountpoint.c : Cleanup mount point when hald detects
5  * that an unmount not done through Unmount()
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 (const char *mount_point)
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 	gboolean found;
59 
60 	hal_mtab_orig = fopen ("/media/.hal-mtab", "r");
61 	if (hal_mtab_orig == NULL) {
62 		unknown_error ("Cannot open /media/.hal-mtab");
63 	}
64 	if (fseek (hal_mtab_orig, 0L, SEEK_END) != 0) {
65 		unknown_error ("Cannot seek to end of /media/.hal-mtab");
66 	}
67 	hal_mtab_orig_len = ftell (hal_mtab_orig);
68 	if (hal_mtab_orig_len < 0) {
69 		unknown_error ("Cannot determine size of /media/.hal-mtab");
70 	}
71 	rewind (hal_mtab_orig);
72 	hal_mtab_buf = g_new0 (char, hal_mtab_orig_len + 1);
73 	num_read = fread (hal_mtab_buf, 1, hal_mtab_orig_len, hal_mtab_orig);
74 	if (num_read != hal_mtab_orig_len) {
75 		unknown_error ("Cannot read from /media/.hal-mtab");
76 	}
77 	fclose (hal_mtab_orig);
78 
79 #ifdef DEBUG
80 	printf ("hal_mtab = '%s'\n", hal_mtab_buf);
81 #endif
82 
83 	lines = g_strsplit (hal_mtab_buf, "\n", 0);
84 	g_free (hal_mtab_buf);
85 
86 	/* find the entry we're going to unmount */
87 	found = FALSE;
88 	for (i = 0; lines[i] != NULL && !found; i++) {
89 		char **line_elements;
90 
91 #ifdef DEBUG
92 		printf (" line = '%s'\n", lines[i]);
93 #endif
94 
95 		if ((lines[i])[0] == '#')
96 			continue;
97 
98 		line_elements = g_strsplit (lines[i], "\t", 6);
99 		if (g_strv_length (line_elements) == 6) {
100 
101 #ifdef DEBUG
102 			printf ("  devfile     = '%s'\n", line_elements[0]);
103 			printf ("  uid         = '%s'\n", line_elements[1]);
104 			printf ("  session id  = '%s'\n", line_elements[2]);
105 			printf ("  fs          = '%s'\n", line_elements[3]);
106 			printf ("  options     = '%s'\n", line_elements[4]);
107 			printf ("  mount_point = '%s'\n", line_elements[5]);
108 #endif
109 
110 			if (strcmp (line_elements[5], mount_point) == 0) {
111 				char *line_to_free;
112 
113 				found = TRUE;
114 
115 				line_to_free = lines[i];
116 				for (j = i; lines[j] != NULL; j++) {
117 					lines[j] = lines[j+1];
118 				}
119 				lines[j] = NULL;
120 				g_free (line_to_free);
121 			}
122 		}
123 
124 		g_strfreev (line_elements);
125 	}
126 
127 	if (!found) {
128 		unknown_error ("mount point is not /media/.hal-mtab");
129 	}
130 
131 #ifdef DEBUG
132 	printf ("Found entry for mount point '%s' in /media/.hal-mtab", mount_point);
133 #endif
134 
135 	/* create new .hal-mtab~ file without the entry we're going to unmount */
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 (i > 0) {
142 			char anewl[2] = "\n\0";
143 			if (fwrite (anewl, 1, 1, hal_mtab_new) != 1) {
144 				unknown_error ("Cannot write to /media/.hal-mtab~");
145 			}
146 		}
147 
148 		if (fwrite (lines[i], 1, strlen (lines[i]), hal_mtab_new) != strlen (lines[i])) {
149 			unknown_error ("Cannot write to /media/.hal-mtab~");
150 		}
151 
152 	}
153 	fclose (hal_mtab_new);
154 
155 	g_strfreev (lines);
156 
157 	/* remove directory */
158 	if (g_rmdir (mount_point) != 0) {
159 		unlink ("/media/.hal-mtab~");
160 		unknown_error ("Cannot remove directory");
161 	}
162 
163 	/* set new .hal-mtab file */
164 	if (rename ("/media/.hal-mtab~", "/media/.hal-mtab") != 0) {
165 		unlink ("/media/.hal-mtab~");
166 		unknown_error ("Cannot rename /media/.hal-mtab~ to /media/.hal-mtab");
167 	}
168 
169 }
170 
171 int
172 main (int argc, char *argv[])
173 {
174 	char *mount_point;
175 
176 	if (!lock_hal_mtab ()) {
177 		unknown_error ("Cannot obtain lock on /media/.hal-mtab");
178 	}
179 
180 	mount_point = getenv ("HALD_CLEANUP");
181 	if (mount_point == NULL)
182 		usage ();
183 
184 #ifdef DEBUG
185 	printf ("in hal-storage-cleanup-mountpoint for mount point '%s'\n", mount_point);
186 #endif
187 	do_cleanup (mount_point);
188 
189 
190 	unlock_hal_mtab ();
191 	return 0;
192 }
193