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 **************************************************************************/
34 #include <glib/gstdio.h>
36 #include "hal-storage-shared.h"
44 fprintf (stderr
, "This program should only be started by hald.\n");
49 do_cleanup (const char *mount_point
)
53 int hal_mtab_orig_len
;
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
);
80 printf ("hal_mtab = '%s'\n", hal_mtab_buf
);
83 lines
= g_strsplit (hal_mtab_buf
, "\n", 0);
84 g_free (hal_mtab_buf
);
86 /* find the entry we're going to unmount */
88 for (i
= 0; lines
[i
] != NULL
&& !found
; i
++) {
92 printf (" line = '%s'\n", lines
[i
]);
95 if ((lines
[i
])[0] == '#')
98 line_elements
= g_strsplit (lines
[i
], "\t", 6);
99 if (g_strv_length (line_elements
) == 6) {
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]);
110 if (strcmp (line_elements
[5], mount_point
) == 0) {
115 line_to_free
= lines
[i
];
116 for (j
= i
; lines
[j
] != NULL
; j
++) {
117 lines
[j
] = lines
[j
+1];
120 g_free (line_to_free
);
124 g_strfreev (line_elements
);
128 unknown_error ("mount point is not /media/.hal-mtab");
132 printf ("Found entry for mount point '%s' in /media/.hal-mtab", mount_point
);
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
++) {
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
);
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
[])
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
)
185 printf ("in hal-storage-cleanup-mountpoint for mount point '%s'\n", mount_point
);
187 do_cleanup (mount_point
);