summaryrefslogtreecommitdiff
path: root/utils/umount.c
blob: cf6be220a6f88dc3b47aaff93a231b101694d538 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* Roughly Unix/Linux-compatible `umount' frontend for Hurd translators.

   Copyright (C) 2013 Free Software Foundation, Inc.
   Written by Justus Winter <4winter@informatik.uni-hamburg.de>

   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   The GNU Hurd is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include <argp.h>
#include <argz.h>
#include <error.h>
#include <fcntl.h>
#include <hurd/fshelp.h>
#include <hurd/fsys.h>
#include <hurd/paths.h>
#include <hurd/process.h>
#include <stdlib.h>
#include <unistd.h>

#include "match-options.h"
#include "../sutils/fstab.h"

/* XXX fix libc */
#undef _PATH_MOUNTED
#define _PATH_MOUNTED "/etc/mtab"

static char *targets;
static size_t targets_len;
static int readonly;
static int verbose;
static int active_flags = FS_TRANS_SET;
static int goaway_flags;
static int source_goaway;
static int fake;

static struct fstab_argp_params fstab_params;

#define FAKE_KEY 0x80 /* !isascii (FAKE_KEY), so no short option.  */

static const struct argp_option argp_opts[] =
{
  {NULL, 'd', 0, 0, "Also ask the source translator to go away"},
  {"fake", FAKE_KEY, 0, 0, "Do not actually umount, just pretend"},
  {"force", 'f', 0, 0, "Force umount by killing the translator"},
  {"no-mtab", 'n', 0, 0, "Do not update /etc/mtab"},
  {"read-only", 'r', 0, 0, "If unmounting fails, try to remount read-only"},
  {"nosync", 'S', 0, 0, "Don't sync a translator before killing it"},
  {"test-opts", 'O', "OPTIONS", 0,
   "Only mount fstab entries matching the given set of options"},
  {"verbose", 'v', 0, 0, "Give more detailed information"},
  {},
};

static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
  struct fstab_argp_params *params = state->input;
  error_t err;
  switch (key)
    {
    case ARGP_KEY_INIT:
      state->child_inputs[0] = params; /* pass down to fstab_argp parser */
      break;

    case 'd':
      source_goaway = 1;
      break;

    case FAKE_KEY:
      fake = 1;
      break;

    case 'f':
      goaway_flags |= FSYS_GOAWAY_FORCE;
      break;

    case 'n':
      /* do nothing */
      break;

    case 'r':
      readonly = 1;
      break;

    case 'S':
      goaway_flags |= FSYS_GOAWAY_NOSYNC;
      break;

    case 'O':
      err = argz_create_sep (arg, ',', &test_opts, &test_opts_len);
      if (err)
	argp_failure (state, 100, ENOMEM, "%s", arg);
      break;

    case 'v':
      verbose += 1;
      break;

    case ARGP_KEY_ARG:
      err = argz_add (&targets, &targets_len, arg);
      if (err)
	argp_failure (state, 100, ENOMEM, "%s", arg);
      break;

    case ARGP_KEY_NO_ARGS:
      if (! params->do_all)
	{
	  argp_error (state,
		      "filesystem argument required if --all is not given");
	  return EINVAL;
	}
      break;

    case ARGP_KEY_END:
      if (params->do_all && targets)
	{
	  argp_error (state, "filesystem argument not allowed with --all");
	  return EINVAL;
	}
      break;

    default:
      return ARGP_ERR_UNKNOWN;
    }

  return 0;
}

static const char doc[] = "Stop active filesystem translators";
static const char args_doc[] = "DEVICE|DIRECTORY [DEVICE|DIRECTORY ...]";

static struct argp fstab_argp_mtab; /* Slightly modified version.  */

static const struct argp_child argp_kids[] =
{
  {&fstab_argp_mtab, 0,
   "Filesystem selection (if no explicit filesystem arguments given):", 2},
  {},
};
static struct argp argp =
{
 options: argp_opts,
 parser: parse_opt,
 args_doc: args_doc,
 doc: doc,
 children: argp_kids,
};

/* This is a trimmed and slightly modified version of
   fstab_argp.options which uses _PATH_MOUNTED instead of _PATH_MNTTAB
   in the doc strings.	*/
static const struct argp_option fstab_argp_mtab_opts[] =
{
  {"all",	 'a', 0,      0, "Do all filesystems in " _PATH_MOUNTED},
  {0,		 'A', 0,      OPTION_ALIAS },
  {"fstab",	 'F', "FILE", 0, "File to use instead of " _PATH_MOUNTED},
  {"fstype",	 't', "TYPE", 0, "Do only filesystems of given type(s)"},
  {"exclude-root",'R',0,      0,
     "Exclude root (/) filesystem from " _PATH_MOUNTED " list"},
  {"exclude",	 'X', "PATTERN", 0, "Exclude directories matching PATTERN"},
  {}
};

static error_t
fstab_argp_mtab_parse_opt (int key, char *arg, struct argp_state *state)
{
  return fstab_argp.parser (key, arg, state);
}

static struct argp fstab_argp_mtab =
{
  options: fstab_argp_mtab_opts,
  parser: fstab_argp_mtab_parse_opt,
};

/* Unmount one filesystem.  */
static error_t
do_umount (struct fs *fs)
{
  error_t err = 0;

  file_t node = file_name_lookup (fs->mntent.mnt_dir, O_NOTRANS, 0666);
  if (node == MACH_PORT_NULL)
    {
      error (0, errno, "%s", fs->mntent.mnt_dir);
      return errno;
    }

  if (verbose)
    printf ("settrans -ag%s%s %s\n",
	    goaway_flags & FSYS_GOAWAY_NOSYNC? "S": "",
	    goaway_flags & FSYS_GOAWAY_FORCE? "f": "",
	    fs->mntent.mnt_dir);

  if (! fake)
    {
      err = file_set_translator (node,
				 0, active_flags, goaway_flags,
				 NULL, 0,
				 MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND);
      if (! err)
	{
	  if (strcmp (fs->mntent.mnt_fsname, "") != 0 &&
	      strcmp (fs->mntent.mnt_fsname, "none") != 0)
	    {
	      if (verbose)
		printf ("settrans -ag%s%s %s\n",
			goaway_flags & FSYS_GOAWAY_NOSYNC? "S": "",
			goaway_flags & FSYS_GOAWAY_FORCE? "f": "",
			fs->mntent.mnt_fsname);

	      file_t source = file_name_lookup (fs->mntent.mnt_fsname,
						O_NOTRANS,
						0666);
	      if (source == MACH_PORT_NULL)
		{
		  error (0, errno, "%s", fs->mntent.mnt_fsname);
		  return errno;
		}

	      err = file_set_translator (source,
					 0, active_flags, goaway_flags,
					 NULL, 0,
					 MACH_PORT_NULL,
					 MACH_MSG_TYPE_COPY_SEND);
	      if (err)
		error (0, err, "%s", fs->mntent.mnt_fsname);

	      mach_port_deallocate (mach_task_self (), source);

	    }
	}
      else
	{
	  error (0, err, "%s", fs->mntent.mnt_dir);

	  /* Try remounting readonly instead if requested.  */
	  if (readonly)
	    {
	      if (verbose)
		printf ("fsysopts %s --readonly\n", fs->mntent.mnt_dir);

	      error_t e = fs_set_readonly (fs, TRUE);
	      if (e)
		error (0, e, "%s", fs->mntent.mnt_dir);
	    }
	}
    }

  /* Deallocate the reference so that unmounting nested translators
     works properly.  */
  mach_port_deallocate (mach_task_self (), node);
  return err;
}

int
main (int argc, char **argv)
{
  error_t err;

  err = argp_parse (&argp, argc, argv, 0, 0, &fstab_params);
  if (err)
    error (3, err, "parsing arguments");

  /* Read the mtab file by default.  */
  if (! fstab_params.fstab_path)
    fstab_params.fstab_path = _PATH_MOUNTED;

  struct fstab *fstab = fstab_argp_create (&fstab_params, NULL, 0);
  if (! fstab)
    error (3, ENOMEM, "fstab creation");

  if (targets)
    for (char *t = targets; t; t = argz_next (targets, targets_len, t))
      {
	/* Figure out if t is the device or the mountpoint.  */
	struct fs *fs = fstab_find_mount (fstab, t);
	if (! fs)
	  {
	    fs = fstab_find_device (fstab, t);
	    if (! fs)
	      {
		error (0, 0, "could not find entry for: %s", t);

		/* As last resort, just assume it is the mountpoint.  */
		struct mntent m =
		  {
		    mnt_fsname: "",
		    mnt_dir: t,
		    mnt_type: "",
		    mnt_opts: 0,
		    mnt_freq: 0,
		    mnt_passno: 0,
		  };

		err = fstab_add_mntent (fstab, &m, &fs);
		if (err)
		  error (2, err, "%s", t);
	      }
	  }

	if (fs)
	  err |= do_umount (fs);
      }
  else
    {
      /* Sort entries in reverse lexicographical order so that the
	 longest mount points are unmounted first.  This makes sure
	 that nested mounts are handled properly.  */
      size_t count = 0;
      for (struct fs *fs = fstab->entries; fs; fs = fs->next)
	count += 1;

      char **entries = malloc (count * sizeof (char *));
      if (! entries)
	error (3, ENOMEM, "allocating entries array");

      char **p = entries;
      for (struct fs *fs = fstab->entries; fs; fs = fs->next)
	*p++ = fs->mntent.mnt_dir;

      /* Reverse lexicographical order.	 */
      int compare_entries (const void *a, const void *b)
	{
	  return -strcmp ((char *) a, (char *) b);
	}

      qsort (entries, count, sizeof (char *), compare_entries);

      for (int i = 0; i < count; i++)
	{
	  struct fs *fs = fstab_find_mount (fstab, entries[i]);
	  if (! fs)
	    error (4, 0, "could not find entry for: %s", entries[i]);

	  if (! match_options (&fs->mntent))
	    continue;

	  err |= do_umount (fs);
	}
    }

  return err? EXIT_FAILURE: EXIT_SUCCESS;
}