8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / hal / tools / hal-storage-cleanup-all-mountpoints.c
blobaa8d657481375b8c46b88bb789735a6cc4f92709
1 /***************************************************************************
2 * CVSID: $Id: hal-storage-mount.c,v 1.7 2006/06/21 00:44:03 david Exp $
4 * hal-storage-cleanup-all-mountpoints.c : Cleanup all mount points in
5 * /media/.hal-mtab that is currently unused
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 (void)
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;
59 hal_mtab_orig = fopen ("/media/.hal-mtab", "r");
60 if (hal_mtab_orig == NULL) {
61 unknown_error ("Cannot open /media/.hal-mtab");
63 if (fseek (hal_mtab_orig, 0L, SEEK_END) != 0) {
64 unknown_error ("Cannot seek to end of /media/.hal-mtab");
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");
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");
76 fclose (hal_mtab_orig);
78 #ifdef DEBUG
79 printf ("hal_mtab = '%s'\n", hal_mtab_buf);
80 #endif
82 lines = g_strsplit (hal_mtab_buf, "\n", 0);
83 g_free (hal_mtab_buf);
85 /* find the entry we're going to unmount */
86 for (i = 0; lines[i] != NULL; i++) {
87 char **line_elements;
89 #ifdef DEBUG
90 printf (" line = '%s'\n", lines[i]);
91 #endif
93 if ((lines[i])[0] == '#')
94 continue;
96 line_elements = g_strsplit (lines[i], "\t", 6);
97 if (g_strv_length (line_elements) == 6) {
98 char *mount_point;
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
109 /* just try to rmdir the entry; if it's non-empty or something is mounted on it,
110 * this will fail
112 mount_point = line_elements[5];
114 /* remove directory */
115 if (g_rmdir (mount_point) == 0) {
116 char *line_to_free;
118 printf ("Removed mount_point '%s'", mount_point);
120 line_to_free = lines[i];
121 for (j = i; lines[j] != NULL; j++) {
122 lines[j] = lines[j+1];
124 lines[j] = NULL;
125 g_free (line_to_free);
127 /* we've moved the lines one back, so make sure we don't advance to next line */
128 i--;
132 g_strfreev (line_elements);
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~");
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~");
146 if (fwrite (anewl, 1, 1, hal_mtab_new) != 1) {
147 unknown_error ("Cannot write to /media/.hal-mtab~");
151 fclose (hal_mtab_new);
153 g_strfreev (lines);
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");
163 main (int argc, char *argv[])
165 if (!lock_hal_mtab ()) {
166 unknown_error ("Cannot obtain lock on /media/.hal-mtab");
169 if (getenv ("HAL_PROP_INFO_UDI") == NULL)
170 usage ();
172 #ifdef DEBUG
173 printf ("in hal-storage-cleanup-all-mountpoints\n");
174 #endif
175 do_cleanup ();
178 unlock_hal_mtab ();
179 return 0;