cppthread 1.1.16
C++ Thread Library
deswappify.cpp
1// Copyright (c) 2026 Made to Order Software Corp. All Rights Reserved
2//
3// https://snapwebsites.org/project/cppthread
4// contact@m2osw.com
5//
6// This program is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation; either version 2 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License along
17// with this program; if not, write to the Free Software Foundation, Inc.,
18// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20// cppthread
21//
22#include <cppthread/thread.h>
23
24
25// libexcept
26//
27#include <libexcept/file_inheritance.h>
28
29
30// snapdev
31//
32#include <snapdev/glob_to_list.h>
33
34
35// C++
36//
37#include <cstring>
38#include <iostream>
39#include <string>
40
41
42// last include
43//
44#include <snapdev/poison.h>
45
46
47
48void usage(char const * argv0)
49{
50 std::cout << "Usage: sudo " << argv0 << " [--opts]" << std::endl;
51 std::cout << "where --opts is one of:" << std::endl;
52 std::cout << " --all deswappify all the running processes (not recommended)" << std::endl;
53 std::cout << " --help | -h print out this help screen" << std::endl;
54 std::cout << " --pid <pid> the PID of the process to deswappify" << std::endl;
55}
56
57
58int main(int argc, char * argv[])
59{
60 libexcept::verify_inherited_files();
61
62 pid_t pid(0);
63 for(int i(1); i < argc; ++i)
64 {
65 if(strcmp(argv[i], "--help") == 0
66 || strcmp(argv[i], "-h") == 0)
67 {
68 usage(argv[0]);
69 return 3;
70 }
71 else if(strcmp(argv[i], "--pid") == 0)
72 {
73 ++i;
74 if(i >= argc)
75 {
76 std::cerr << argv[0] << ":error: pid must be followed by a process identifier." << std::endl;
77 return 1;
78 }
79 pid = atol(argv[i]);
80 if(pid <= 0)
81 {
82 std::cerr << argv[0] << ":error: invalid pid in \"" << argv[i] << "\"." << std::endl;
83 return 1;
84 }
85 }
86 else if(strcmp(argv[i], "--all") == 0)
87 {
88 pid = -1;
89 }
90 else
91 {
92 std::cerr << argv[0] << ":error: unexpected command line option \"" << argv[i] << "\"." << std::endl;
93 return 1;
94 }
95 }
96
97 if((pid > 0 || pid == -1)
98 && getuid() != 0)
99 {
100 std::cerr << argv[0] << ":warning: deswappifying generally requires you to be root. If it doesn't work, try again with sudo even if you own the process." << std::endl;
101 }
102
103 if(pid > 0)
104 {
105 errno = cppthread::deswappify(pid);
106 return errno == 0 ? 0 : 1;
107 }
108 else if(pid == -1)
109 {
110 snapdev::glob_to_list<std::list<std::string>> glob;
111 if(!glob.read_path<
112 snapdev::glob_to_list_flag_t::GLOB_FLAG_IGNORE_ERRORS,
113 snapdev::glob_to_list_flag_t::GLOB_FLAG_ONLY_DIRECTORIES>("/proc/*"))
114 {
115 std::cerr << argv[0] << ":error: could not read /proc for a list of processes." << std::endl;
116 return 1;
117 }
118 int e(0);
119 for(auto const & p : glob)
120 {
121 pid = atol(p.c_str());
122 if(pid == -1)
123 {
124 // not a valid number, skip
125 //
126 continue;
127 }
128 errno = cppthread::deswappify(pid);
129 if(errno != 0)
130 {
131 e = 1;
132 }
133 }
134 return e;
135 }
136 else
137 {
138 std::cerr << argv[0] << ":error: nothing to do. Try again with --pid or --all." << std::endl;
139 }
140
141 return 1;
142}
143
144// vim: ts=4 sw=4 et
int deswappify(pid_t pid)
Go through the swapped out sections of a process and deswappify them.
Thread Runner and Managers.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.