f86590fcbf59b9582691bc4d377d06e091dcb226
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) {}
Stefan Schuermans make timing configurable, s...

Stefan Schuermans authored 4 years ago

21)   bool callback();
22)   std::chrono::duration<float, std::ratio<1>> sleepTime;
23)   template <class Rep, class Period>
24)   bool iterativeSleep(std::chrono::duration<Rep, Period> duration) const;
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

25)   bool go_on;
26) };
Stefan Schuermans finish binaries, add readme

Stefan Schuermans authored 4 years ago

27) 
Stefan Schuermans make timing configurable, s...

Stefan Schuermans authored 4 years ago

28) bool DaemonCallback::callback() {
29)   iterativeSleep(sleepTime);
30)   return go_on;
31) }
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

32) 
33) // iterative sleep, watching go_on, returns whether sleep completed
34) template <class Rep, class Period>
Stefan Schuermans make timing configurable, s...

Stefan Schuermans authored 4 years ago

35) bool DaemonCallback::iterativeSleep(
36)     std::chrono::duration<Rep, Period> duration) const {
37)   const std::chrono::duration<int, std::milli> zero(0), step(100);
38)   while (duration > zero && go_on) {
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

39)     if (duration >= step) {
40)       std::this_thread::sleep_for(step);
41)       duration -= step;
42)     } else {
43)       std::this_thread::sleep_for(duration);
44)       duration = zero;
45)     }
46)   }
47)   return duration <= zero;
Stefan Schuermans finish binaries, add readme

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

49) 
Stefan Schuermans make timing configurable, s...

Stefan Schuermans authored 4 years ago

50) DaemonCallback daemonCallback;
51) 
52) void sighandler(int) { daemonCallback.go_on = false; }
53) 
Stefan Schuermans begin of permissioner confi...

Stefan Schuermans authored 4 years ago

54) int main(int argc, char const **argv) {
55)   if (argc != 2) {
56)     std::cerr << "usage: " << argv[0] << " <config file>" << std::endl;
57)     return EXIT_FAILURE;
58)   }
59)   std::string configFileName(argv[1]);
60) 
Stefan Schuermans finish binaries, add readme

Stefan Schuermans authored 4 years ago

61)   // load configuration
62)   Config config;
Stefan Schuermans begin of permissioner confi...

Stefan Schuermans authored 4 years ago

63)   try {
64)     config.parseFile(configFileName);
65)   } catch (std::exception const &e) {
66)     std::cerr << "error: " << e.what() << std::endl;
67)     return EXIT_FAILURE;
68)   }
69) 
Stefan Schuermans make timing configurable, s...

Stefan Schuermans authored 4 years ago

70)   // get timing from config
71)   std::chrono::duration<float, std::ratio<1>> sleepTime(
72)       config.getSleepTime().get());
73)   float waitFactor = config.getWaitFactor().get();
74)   std::chrono::duration<float, std::ratio<1>> waitTime(
75)       config.getWaitTime().get());
76) 
Stefan Schuermans finish binaries, add readme

Stefan Schuermans authored 4 years ago

77)   // catch signals to exit properly on Ctrl-C and so on
78)   signal(SIGINT, sighandler);
79)   signal(SIGPIPE, sighandler);
80)   signal(SIGQUIT, sighandler);
81)   signal(SIGTERM, sighandler);
82) 
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

85)   // set nicecess of process
86)   config.getNice().apply();
87) 
Stefan Schuermans make timing configurable, s...

Stefan Schuermans authored 4 years ago

88)   // set sleep time after each file in daemon callback
89)   daemonCallback.sleepTime = sleepTime;
90) 
Stefan Schuermans finish binaries, add readme

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

96)     std::cout << "permissionerd (" << configFileName
97)               << "): setting ownership and permissions" << std::endl;
98)     auto begin = std::chrono::steady_clock::now();
99)     try {
Stefan Schuermans make timing configurable, s...

Stefan Schuermans authored 4 years ago

100)       if (!config.setPermissions(daemonCallback)) {
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

101)         break;
102)       }
Stefan Schuermans finish binaries, add readme

Stefan Schuermans authored 4 years ago

103)     } catch (std::exception const &e) {
104)       std::cerr << "error: " << e.what() << std::endl;
105)       ret = EXIT_FAILURE;
106)       break;
107)     }
108)     auto end = std::chrono::steady_clock::now();
109)     std::chrono::duration<float, std::ratio<1>> duration = end - begin;
110)     std::cout << "permissionerd (" << configFileName << "): took "
111)               << duration.count() << " s" << std::endl;
112) 
Stefan Schuermans make timing configurable, s...

Stefan Schuermans authored 4 years ago

113)     // wait after tree traversal
114)     if (!daemonCallback.iterativeSleep(waitFactor * duration + waitTime)) {
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

115)       break;
116)     }
Stefan Schuermans finish binaries, add readme

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

119) 
120)   std::cout << "permissionerd (" << configFileName << ") shutting down"
121)             << std::endl;
122) 
123)   return ret;