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

   Copyright (C) 1997,98,99 Free Software Foundation, Inc.
   Written by Miles Bader <miles@gnu.org>
   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>
#include <mntent.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 `swap' in " _PATH_MNTTAB},
  {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 int
swaponoff (const 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 err;
    }

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

  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);

  return err;
}

static int do_all;

int
main(int argc, char *argv[])
{
  /* Parse our options...  */
  error_t parse_opt (int key, char *arg, struct argp_state *state)
    {
      switch (key)
	{
	case 'a':
	  do_all = 1;
	  break;

	case ARGP_KEY_ARG:
#ifdef SWAPOFF
#define ONOFF 0
#else
#define ONOFF 1
#endif
	  swaponoff (arg, ONOFF);
	  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);

  if (do_all)
    {
      struct mntent *me;
      FILE *f;

      f = setmntent (_PATH_MNTTAB, "r");
      if (f == NULL)
	error (1, errno, "Cannot read %s", _PATH_MNTTAB);
      else
	{
	  int done = 0, err = 0;
	  while ((me = getmntent (f)) != NULL)
	    if (!strcmp (me->mnt_type, MNTTYPE_SWAP))
	      {
		done = 1;

		err |= swaponoff (me->mnt_fsname, ONOFF);
	      }
	  if (done == 0)
	    error (2, 0, "No swap partitions found in %s", _PATH_MNTTAB);
	  else if (err)
	    return 1;
	}
    }

  return 0;
}