chiark / gitweb /
systemctl: subscribe to changes of existing units/jobs
[elogind.git] / src / swap.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <limits.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <sys/epoll.h>
27 #include <sys/stat.h>
28 #include <sys/swap.h>
29
30 #include "unit.h"
31 #include "swap.h"
32 #include "load-fragment.h"
33 #include "load-dropin.h"
34 #include "unit-name.h"
35 #include "dbus-swap.h"
36
37 static const UnitActiveState state_translation_table[_SWAP_STATE_MAX] = {
38         [SWAP_DEAD] = UNIT_INACTIVE,
39         [SWAP_ACTIVE] = UNIT_ACTIVE,
40         [SWAP_MAINTAINANCE] = UNIT_INACTIVE
41 };
42
43 static void swap_init(Unit *u) {
44         Swap *s = SWAP(u);
45
46         assert(s);
47         assert(s->meta.load_state == UNIT_STUB);
48
49         s->parameters_etc_fstab.priority = s->parameters_proc_swaps.priority = s->parameters_fragment.priority = -1;
50 }
51
52 static void swap_done(Unit *u) {
53         Swap *s = SWAP(u);
54
55         assert(s);
56
57         free(s->what);
58         free(s->parameters_etc_fstab.what);
59         free(s->parameters_proc_swaps.what);
60         free(s->parameters_fragment.what);
61 }
62
63 int swap_add_one_mount_link(Swap *s, Mount *m) {
64          int r;
65
66         assert(s);
67         assert(m);
68
69         if (s->meta.load_state != UNIT_LOADED ||
70             m->meta.load_state != UNIT_LOADED)
71                 return 0;
72
73         if (is_device_path(s->what))
74                 return 0;
75
76         if (!path_startswith(s->what, m->where))
77                 return 0;
78
79         if ((r = unit_add_dependency(UNIT(m), UNIT_BEFORE, UNIT(s), true)) < 0)
80                 return r;
81
82         if ((r = unit_add_dependency(UNIT(s), UNIT_REQUIRES, UNIT(m), true)) < 0)
83                 return r;
84
85         return 0;
86 }
87
88 static int swap_add_mount_links(Swap *s) {
89         Meta *other;
90         int r;
91
92         assert(s);
93
94         LIST_FOREACH(units_per_type, other, s->meta.manager->units_per_type[UNIT_MOUNT])
95                 if ((r = swap_add_one_mount_link(s, (Mount*) other)) < 0)
96                         return r;
97
98         return 0;
99 }
100
101 static int swap_add_target_links(Swap *s) {
102         Unit *tu;
103         SwapParameters *p;
104         int r;
105
106         assert(s);
107
108         if (s->from_fragment)
109                 p = &s->parameters_fragment;
110         else if (s->from_etc_fstab)
111                 p = &s->parameters_etc_fstab;
112         else
113                 return 0;
114
115         if ((r = manager_load_unit(s->meta.manager, SPECIAL_SWAP_TARGET, NULL, &tu)) < 0)
116                 return r;
117
118         if (!p->noauto && p->handle && s->meta.manager->running_as != MANAGER_SESSION)
119                 if ((r = unit_add_dependency(tu, UNIT_WANTS, UNIT(s), true)) < 0)
120                         return r;
121
122         return unit_add_dependency(UNIT(s), UNIT_BEFORE, tu, true);
123 }
124
125 static int swap_verify(Swap *s) {
126         bool b;
127         char *e;
128
129         if (UNIT(s)->meta.load_state != UNIT_LOADED)
130                   return 0;
131
132         if (!(e = unit_name_from_path(s->what, ".swap")))
133                   return -ENOMEM;
134
135         b = unit_has_name(UNIT(s), e);
136         free(e);
137
138         if (!b) {
139                 log_error("%s: Value of \"What\" and unit name do not match, not loading.\n", UNIT(s)->meta.id);
140                 return -EINVAL;
141         }
142
143         return 0;
144 }
145
146 static int swap_load(Unit *u) {
147         int r;
148         Swap *s = SWAP(u);
149
150         assert(s);
151         assert(u->meta.load_state == UNIT_STUB);
152
153         /* Load a .swap file */
154         if ((r = unit_load_fragment_and_dropin_optional(u)) < 0)
155                 return r;
156
157         if (u->meta.load_state == UNIT_LOADED) {
158
159                 if (s->meta.fragment_path)
160                         s->from_fragment = true;
161
162                 if (!s->what) {
163                         if (s->parameters_fragment.what)
164                                 s->what = strdup(s->parameters_fragment.what);
165                         else if (s->parameters_etc_fstab.what)
166                                 s->what = strdup(s->parameters_etc_fstab.what);
167                         else if (s->parameters_proc_swaps.what)
168                                 s->what = strdup(s->parameters_proc_swaps.what);
169                         else
170                                 s->what = unit_name_to_path(u->meta.id);
171
172                         if (!s->what)
173                                 return -ENOMEM;
174                 }
175
176                 path_kill_slashes(s->what);
177
178                 if (!s->meta.description)
179                         if ((r = unit_set_description(u, s->what)) < 0)
180                                 return r;
181
182                 if ((r = unit_add_node_link(u, s->what,
183                                             (u->meta.manager->running_as == MANAGER_INIT ||
184                                              u->meta.manager->running_as == MANAGER_SYSTEM))) < 0)
185                         return r;
186
187                 if ((r = swap_add_mount_links(s)) < 0)
188                         return r;
189
190                 if ((r = swap_add_target_links(s)) < 0)
191                         return r;
192         }
193
194         return swap_verify(s);
195 }
196
197 static int swap_find(Manager *m, const char *what, Unit **_u) {
198         Unit *u;
199         char *e;
200
201         assert(m);
202         assert(what);
203         assert(_u);
204
205         /* /proc/swaps and /etc/fstab might refer to this device by
206          * different names (e.g. one by uuid, the other by the kernel
207          * name), we hence need to look for all aliases we are aware
208          * of for this device */
209
210         if (!(e = unit_name_from_path(what, ".device")))
211                 return -ENOMEM;
212
213         u = manager_get_unit(m, e);
214         free(e);
215
216         if (u) {
217                 Iterator i;
218                 const char *d;
219
220                 SET_FOREACH(d, u->meta.names, i) {
221                         Unit *k;
222
223                         if (!(e = unit_name_change_suffix(d, ".swap")))
224                                 return -ENOMEM;
225
226                         k = manager_get_unit(m, e);
227                         free(e);
228
229                         if (k) {
230                                 *_u = k;
231                                 return 0;
232                         }
233                 }
234         }
235
236         *_u = NULL;
237         return 0;
238 }
239
240 int swap_add_one(
241                 Manager *m,
242                 const char *what,
243                 int priority,
244                 bool noauto,
245                 bool handle,
246                 bool from_proc_swaps) {
247         Unit *u = NULL;
248         char *e = NULL, *w = NULL;
249         bool delete;
250         int r;
251         SwapParameters *p;
252
253         assert(m);
254         assert(what);
255
256         if (!(e = unit_name_from_path(what, ".swap")))
257                 return -ENOMEM;
258
259         if (!(u = manager_get_unit(m, e)))
260                 if ((r = swap_find(m, what, &u)) < 0)
261                         goto fail;
262
263         if (!u) {
264                 delete = true;
265
266                 if (!(u = unit_new(m))) {
267                         free(e);
268                         return -ENOMEM;
269                 }
270         } else
271                 delete = false;
272
273         if ((r = unit_add_name(u, e)) < 0)
274                 goto fail;
275
276         if (!(w = strdup(what))) {
277                 r = -ENOMEM;
278                 goto fail;
279         }
280
281         if (from_proc_swaps) {
282                 p = &SWAP(u)->parameters_proc_swaps;
283                 SWAP(u)->from_proc_swaps = true;
284         } else {
285                 p = &SWAP(u)->parameters_etc_fstab;
286                 SWAP(u)->from_etc_fstab = true;
287         }
288
289         free(p->what);
290         p->what = w;
291
292         p->priority = priority;
293         p->noauto = noauto;
294         p->handle = handle;
295
296         if (delete)
297                 unit_add_to_load_queue(u);
298
299         unit_add_to_dbus_queue(u);
300
301         free(e);
302
303         return 0;
304
305 fail:
306         free(w);
307         free(e);
308
309         if (delete && u)
310                 unit_free(u);
311
312         return r;
313 }
314
315 static void swap_set_state(Swap *s, SwapState state) {
316         SwapState old_state;
317         assert(s);
318
319         old_state = s->state;
320         s->state = state;
321
322         if (state != old_state)
323                 log_debug("%s changed %s -> %s",
324                           UNIT(s)->meta.id,
325                           swap_state_to_string(old_state),
326                           swap_state_to_string(state));
327
328         unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
329 }
330
331 static int swap_coldplug(Unit *u) {
332         Swap *s = SWAP(u);
333         SwapState new_state = SWAP_DEAD;
334
335         assert(s);
336         assert(s->state == SWAP_DEAD);
337
338         if (s->deserialized_state != s->state)
339                 new_state = s->deserialized_state;
340         else if (s->from_proc_swaps)
341                 new_state = SWAP_ACTIVE;
342
343         if (new_state != s->state)
344                 swap_set_state(s, new_state);
345
346         return 0;
347 }
348
349 static void swap_dump(Unit *u, FILE *f, const char *prefix) {
350         Swap *s = SWAP(u);
351         SwapParameters *p;
352
353         assert(s);
354         assert(f);
355
356         if (s->from_proc_swaps)
357                 p = &s->parameters_proc_swaps;
358         else if (s->from_fragment)
359                 p = &s->parameters_fragment;
360         else
361                 p = &s->parameters_etc_fstab;
362
363         fprintf(f,
364                 "%sSwap State: %s\n"
365                 "%sWhat: %s\n"
366                 "%sPriority: %i\n"
367                 "%sNoAuto: %s\n"
368                 "%sHandle: %s\n"
369                 "%sFrom /etc/fstab: %s\n"
370                 "%sFrom /proc/swaps: %s\n"
371                 "%sFrom fragment: %s\n",
372                 prefix, swap_state_to_string(s->state),
373                 prefix, s->what,
374                 prefix, p->priority,
375                 prefix, yes_no(p->noauto),
376                 prefix, yes_no(p->handle),
377                 prefix, yes_no(s->from_etc_fstab),
378                 prefix, yes_no(s->from_proc_swaps),
379                 prefix, yes_no(s->from_fragment));
380 }
381
382 static void swap_enter_dead(Swap *s, bool success) {
383         assert(s);
384
385         swap_set_state(s, success ? SWAP_MAINTAINANCE : SWAP_DEAD);
386 }
387
388 static int swap_start(Unit *u) {
389         Swap *s = SWAP(u);
390         int priority = -1;
391         int r;
392
393         assert(s);
394         assert(s->state == SWAP_DEAD || s->state == SWAP_MAINTAINANCE);
395
396         if (s->from_fragment)
397                 priority = s->parameters_fragment.priority;
398         else if (s->from_etc_fstab)
399                 priority = s->parameters_etc_fstab.priority;
400
401         r = swapon(s->what, (priority << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK);
402
403         if (r < 0 && errno != EBUSY) {
404                 r = -errno;
405                 swap_enter_dead(s, false);
406                 return r;
407         }
408
409         swap_set_state(s, SWAP_ACTIVE);
410         return 0;
411 }
412
413 static int swap_stop(Unit *u) {
414         Swap *s = SWAP(u);
415         int r;
416
417         assert(s);
418
419         assert(s->state == SWAP_ACTIVE);
420
421         r = swapoff(s->what);
422         swap_enter_dead(s, r >= 0 || errno == EINVAL);
423
424         return 0;
425 }
426
427 static int swap_serialize(Unit *u, FILE *f, FDSet *fds) {
428         Swap *s = SWAP(u);
429
430         assert(s);
431         assert(f);
432         assert(fds);
433
434         unit_serialize_item(u, f, "state", swap_state_to_string(s->state));
435
436         return 0;
437 }
438
439 static int swap_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
440         Swap *s = SWAP(u);
441
442         assert(s);
443         assert(fds);
444
445         if (streq(key, "state")) {
446                 SwapState state;
447
448                 if ((state = swap_state_from_string(value)) < 0)
449                         log_debug("Failed to parse state value %s", value);
450                 else
451                         s->deserialized_state = state;
452         } else
453                 log_debug("Unknown serialization key '%s'", key);
454
455         return 0;
456 }
457
458 static UnitActiveState swap_active_state(Unit *u) {
459         assert(u);
460
461         return state_translation_table[SWAP(u)->state];
462 }
463
464 static const char *swap_sub_state_to_string(Unit *u) {
465         assert(u);
466
467         return swap_state_to_string(SWAP(u)->state);
468 }
469
470 static bool swap_check_gc(Unit *u) {
471         Swap *s = SWAP(u);
472
473         assert(s);
474
475         return s->from_etc_fstab || s->from_proc_swaps;
476 }
477
478 static int swap_load_proc_swaps(Manager *m) {
479         rewind(m->proc_swaps);
480
481         (void) fscanf(m->proc_swaps, "%*s %*s %*s %*s %*s\n");
482
483         for (;;) {
484                 char *dev = NULL, *d;
485                 int prio = 0, k;
486
487                 if ((k = fscanf(m->proc_swaps,
488                                 "%ms " /* device/file */
489                                 "%*s " /* type of swap */
490                                 "%*s " /* swap size */
491                                 "%*s " /* used */
492                                 "%i\n", /* priority */
493                                 &dev, &prio)) != 2) {
494
495                         if (k == EOF)
496                                 break;
497
498                         free(dev);
499                         return -EBADMSG;
500                 }
501
502                 d = cunescape(dev);
503                 free(dev);
504
505                 if (!d)
506                         return -ENOMEM;
507
508                 k = swap_add_one(m, d, prio, false, false, true);
509                 free(d);
510
511                 if (k < 0)
512                         return k;
513         }
514
515         return 0;
516 }
517
518 static void swap_shutdown(Manager *m) {
519         assert(m);
520
521         if (m->proc_swaps) {
522                 fclose(m->proc_swaps);
523                 m->proc_swaps = NULL;
524         }
525 }
526
527 static const char* const swap_state_table[_SWAP_STATE_MAX] = {
528         [SWAP_DEAD] = "dead",
529         [SWAP_ACTIVE] = "active",
530         [SWAP_MAINTAINANCE] = "maintainance"
531 };
532
533 DEFINE_STRING_TABLE_LOOKUP(swap_state, SwapState);
534
535 static int swap_enumerate(Manager *m) {
536         int r;
537         assert(m);
538
539         if (!m->proc_swaps)
540                 if (!(m->proc_swaps = fopen("/proc/swaps", "re")))
541                         return -errno;
542
543         if ((r = swap_load_proc_swaps(m)) < 0)
544                 swap_shutdown(m);
545
546         return r;
547 }
548
549 const UnitVTable swap_vtable = {
550         .suffix = ".swap",
551
552         .no_instances = true,
553         .no_isolate = true,
554
555         .init = swap_init,
556         .load = swap_load,
557         .done = swap_done,
558
559         .coldplug = swap_coldplug,
560
561         .dump = swap_dump,
562
563         .start = swap_start,
564         .stop = swap_stop,
565
566         .serialize = swap_serialize,
567         .deserialize_item = swap_deserialize_item,
568
569         .active_state = swap_active_state,
570         .sub_state_to_string = swap_sub_state_to_string,
571
572         .check_gc = swap_check_gc,
573
574         .bus_message_handler = bus_swap_message_handler,
575
576         .enumerate = swap_enumerate,
577         .shutdown = swap_shutdown
578 };