bdbf09afe8d2cd07513a908431a50e641cecc28d
Stefan Schuermans add copyright & license

Stefan Schuermans authored 4 years ago

1) /**
2)  * Permissioner: set file ownerships and permissions
3)  * Copyright 2020: Stefan Schuermans, Aachen, Germany <stefan@schuermans.info>
4)  * Copyleft: GNU GENERAL PUBLIC LICENSE version 3 (see LICENSE)
5)  */
6) 
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

7) #include <permissioner/Callback.h>
Stefan Schuermans move most sources into libp...

Stefan Schuermans authored 4 years ago

8) #include <permissioner/Config.h>
Stefan Schuermans begin of permissioner confi...

Stefan Schuermans authored 4 years ago

9) 
Stefan Schuermans finish binaries, add readme

Stefan Schuermans authored 4 years ago

10) #include <chrono>
11) #include <csignal>
Stefan Schuermans begin of permissioner confi...

Stefan Schuermans authored 4 years ago

12) #include <cstdlib>
13) #include <iostream>
14) #include <stdexcept>
15) #include <string>
Stefan Schuermans finish binaries, add readme

Stefan Schuermans authored 4 years ago

16) #include <thread>
17) 
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

18) class DaemonCallback : public Callback {
19) public:
20)   DaemonCallback() : go_on(true) {}
21)   bool callback() { return go_on; }
22)   bool go_on;
23) };
Stefan Schuermans finish binaries, add readme

Stefan Schuermans authored 4 years ago

24) 
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

25) DaemonCallback daemonCallback;
26) 
27) void sighandler(int) { daemonCallback.go_on = false; }
28) 
29) // iterative sleep, watching go_on, returns whether sleep completed
30) template <class Rep, class Period>
31) bool iterativeSleep(std::chrono::duration<Rep, Period> duration,
32)                     DaemonCallback &daemonCallback) {
33)   std::chrono::duration<int, std::milli> zero(0), step(100);
34)   while (duration > zero && daemonCallback.go_on) {
35)     if (duration >= step) {
36)       std::this_thread::sleep_for(step);
37)       duration -= step;
38)     } else {
39)       std::this_thread::sleep_for(duration);
40)       duration = zero;
41)     }
42)   }
43)   return duration <= zero;
Stefan Schuermans finish binaries, add readme

Stefan Schuermans authored 4 years ago

44) }
Stefan Schuermans begin of permissioner confi...

Stefan Schuermans authored 4 years ago

45) 
46) int main(int argc, char const **argv) {
47)   if (argc != 2) {
48)     std::cerr << "usage: " << argv[0] << " <config file>" << std::endl;
49)     return EXIT_FAILURE;
50)   }
51)   std::string configFileName(argv[1]);
52) 
Stefan Schuermans finish binaries, add readme

Stefan Schuermans authored 4 years ago

53)   // load configuration
54)   Config config;
Stefan Schuermans begin of permissioner confi...

Stefan Schuermans authored 4 years ago

55)   try {
56)     config.parseFile(configFileName);
57)   } catch (std::exception const &e) {
58)     std::cerr << "error: " << e.what() << std::endl;
59)     return EXIT_FAILURE;
60)   }
61) 
Stefan Schuermans finish binaries, add readme

Stefan Schuermans authored 4 years ago

62)   // catch signals to exit properly on Ctrl-C and so on
63)   signal(SIGINT, sighandler);
64)   signal(SIGPIPE, sighandler);
65)   signal(SIGQUIT, sighandler);
66)   signal(SIGTERM, sighandler);
67) 
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

68)   std::cout << "permissionerd (" << configFileName << ") starting" << std::endl;
Stefan Schuermans finish binaries, add readme

Stefan Schuermans authored 4 years ago

69) 
Stefan Schuermans add nice and I/O idle config

Stefan Schuermans authored 4 years ago

70)   // set nicecess of process
71)   config.getNice().apply();
72) 
Stefan Schuermans finish binaries, add readme

Stefan Schuermans authored 4 years ago

73)   // continuously set ownership and permissions
74)   int ret = EXIT_SUCCESS;
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

75)   while (daemonCallback.go_on) {
Stefan Schuermans finish binaries, add readme

Stefan Schuermans authored 4 years ago

76) 
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

77)     // set ownership and permissions, measure time it takes
Stefan Schuermans finish binaries, add readme

Stefan Schuermans authored 4 years ago

78)     std::cout << "permissionerd (" << configFileName
79)               << "): setting ownership and permissions" << std::endl;
80)     auto begin = std::chrono::steady_clock::now();
81)     try {
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

82)       if (! config.setPermissions(daemonCallback)) {
83)         break;
84)       }
Stefan Schuermans finish binaries, add readme

Stefan Schuermans authored 4 years ago

85)     } catch (std::exception const &e) {
86)       std::cerr << "error: " << e.what() << std::endl;
87)       ret = EXIT_FAILURE;
88)       break;
89)     }
90)     auto end = std::chrono::steady_clock::now();
91)     std::chrono::duration<float, std::ratio<1>> duration = end - begin;
92)     std::cout << "permissionerd (" << configFileName << "): took "
93)               << duration.count() << " s" << std::endl;
94) 
95)     // sleep 10 times as long as the work took plus one second
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

96)     auto sleep_time =
97)         10 * duration + std::chrono::duration<int, std::ratio<1>>(1);
98)     if (! iterativeSleep(sleep_time, daemonCallback)) {
99)       break;
100)     }
Stefan Schuermans finish binaries, add readme

Stefan Schuermans authored 4 years ago

101) 
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

102)   } // while (daemonCallback.go_on)
Stefan Schuermans finish binaries, add readme

Stefan Schuermans authored 4 years ago

103) 
104)   std::cout << "permissionerd (" << configFileName << ") shutting down"
105)             << std::endl;
106) 
107)   return ret;