8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / hal / tools / hal-storage-cleanup-mountpoint.c
blobf4973a82db0003453926ad391c85925c092b84ed
1 /***************************************************************************
2 * CVSID: $Id: hal-storage-mount.c,v 1.7 2006/06/21 00:44:03 david Exp $
4 * hal-storage-cleanup-mountpoint.c : Cleanup mount point when hald detects
5 * that an unmount not done through Unmount()
7 * Copyright (C) 2006 David Zeuthen, <david@fubar.dk>
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.
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.
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
23 **************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
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>
36 #include "hal-storage-shared.h"
38 /*#define DEBUG*/
39 #define DEBUG
41 static void
42 usage (void)
44 fprintf (stderr, "This program should only be started by hald.\n");
45 exit (1);
48 static void
49 do_cleanup (const char *mount_point)
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;
60 hal_mtab_orig = fopen ("/media/.hal-mtab", "r");
61 if (hal_mtab_orig == NULL) {
62 unknown_error ("Cannot open /media/.hal-mtab");
64 if (fseek (hal_mtab_orig, 0L, SEEK_END) != 0) {
65 unknown_error ("Cannot seek to end of /media/.hal-mtab");
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");
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");
77 fclose (hal_mtab_orig);
79 #ifdef DEBUG
80 printf ("hal_mtab = '%s'\n", hal_mtab_buf);
81 #endif
83 lines = g_strsplit (hal_mtab_buf, "\n", 0);
84 g_free (hal_mtab_buf);
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;
91 #ifdef DEBUG
92 printf (" line = '%s'\n", lines[i]);
93 #endif
95 if ((lines[i])[0] == '#')
96 continue;
98 line_elements = g_strsplit (lines[i], "\t", 6);
99 if (g_strv_length (line_elements) == 6) {
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
110 if (strcmp (line_elements[5], mount_point) == 0) {
111 char *line_to_free;
113 found = TRUE;
115 line_to_free = lines[i];
116 for (j = i; lines[j] != NULL; j++) {
117 lines[j] = lines[j+1];
119 lines[j] = NULL;
120 g_free (line_to_free);
124 g_strfreev (line_elements);
127 if (!found) {
128 unknown_error ("mount point is not /media/.hal-mtab");
131 #ifdef DEBUG
132 printf ("Found entry for mount point '%s' in /media/.hal-mtab", mount_point);
133 #endif
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~");
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~");
148 if (fwrite (lines[i], 1, strlen (lines[i]), hal_mtab_new) != strlen (lines[i])) {
149 unknown_error ("Cannot write to /media/.hal-mtab~");
153 fclose (hal_mtab_new);
155 g_strfreev (lines);
157 /* remove directory */
158 if (g_rmdir (mount_point) != 0) {
159 unlink ("/media/.hal-mtab~");
160 unknown_error ("Cannot remove directory");
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");
172 main (int argc, char *argv[])
174 char *mount_point;
176 if (!lock_hal_mtab ()) {
177 unknown_error ("Cannot obtain lock on /media/.hal-mtab");
180 mount_point = getenv ("HALD_CLEANUP");
181 if (mount_point == NULL)
182 usage ();
184 #ifdef DEBUG
185 printf ("in hal-storage-cleanup-mountpoint for mount point '%s'\n", mount_point);
186 #endif
187 do_cleanup (mount_point);
190 unlock_hal_mtab ();
191 return 0;