summaryrefslogtreecommitdiff
path: root/sutils/swapon.c
blob: 3a23353f35536be3fc9d7bbe7b48657e4949b5aa (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
/* Add/remove paging devices

   Copyright (C) 1997 Free Software Foundation, Inc.
   Written by Miles Bader <miles@gnu.ai.mit.edu>
   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, write to the Free Software Foundation, Inc.,
   59 Temple Place - Suite 330, Boston, MA 02111, USA. */

#include <hurd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <argp.h>
#include <error.h>
#include <hurd/store.h>
#include <version.h>
#include <mach/default_pager.h>

#ifdef SWAPOFF
const char *argp_program_version = STANDARD_HURD_VERSION (swapoff);
#else
const char *argp_program_version = STANDARD_HURD_VERSION (swapon);
#endif

static struct argp_option options[] =
{
  {"standard",	  'a', 0, 0, "Use all devices marked as `sw' in /etc/fstab"},
  {0, 0}
};
static char *args_doc = "DEVICE...";

#ifdef SWAPOFF
static char *doc = "Stop paging on DEVICE...";
#else
static char *doc = "Start paging onto DEVICE...";
#endif

static void
swaponoff (char *file, int add)
{
  error_t err;
  struct store *store;
  static mach_port_t def_pager = MACH_PORT_NULL;
  static mach_port_t dev_master = MACH_PORT_NULL;

  err = store_open (file, 0, 0, &store);
  if (err)
    {
      error (0, err, "%s", file);
      return;
    }

  if (store->class != &store_device_class)
    {
      error (0, 0, "%s: Can't get device", file);
      return;
    }
  if (! (store->flags & STORE_ENFORCED))
    {
      error (0, 0, "%s: Can only page to the entire device", file);
      return;
    }

  if (def_pager == MACH_PORT_NULL)
    {
      mach_port_t host;

      err = get_privileged_ports (&host, &dev_master);
      if (err)
	error (12, err, "Cannot get host port");

      err = vm_set_default_memory_manager (host, &def_pager);
      mach_port_deallocate (mach_task_self (), host);
      if (err)
	error (13, err, "Cannot get default pager port");
    }

  {
    char pname[sizeof "/dev/" + strlen (store->name) + 1];
    snprintf (pname, sizeof pname, "/dev/%s", store->name);
    err = default_pager_paging_file (def_pager, dev_master, pname, add);
    if (err)
      error (0, err, "%s", file);
  }

  store_free (store);
}

void 
main(int argc, char *argv[])
{
  /* Parse our options...  */
  error_t parse_opt (int key, char *arg, struct argp_state *state)
    {
      switch (key)
	{
	case 'a':
	  argp_failure (state, 5, 0, "--standard: Not supported yet"); 

	case ARGP_KEY_ARG:
#ifdef SWAPOFF
	  swaponoff (arg, 0);
#else
	  swaponoff (arg, 1);
#endif
	  break;

	default:
	  return ARGP_ERR_UNKNOWN;
	}
      return 0;
    }
  struct argp argp = {options, parse_opt, args_doc, doc};

  argp_parse (&argp, argc, argv, 0, 0, 0);

  exit(0);
}