1cbbe96195b6154b1957a73808eced432e994792
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 move most sources into libp...

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

8) 
Stefan Schuermans finish binaries, add readme

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

15) #include <thread>
16) 
17) static int go_on = 1;
18) 
19) void sighandler(int) {
20)   go_on = 0;
21) }
Stefan Schuermans begin of permissioner confi...

Stefan Schuermans authored 4 years ago

22) 
23) int main(int argc, char const **argv) {
24)   if (argc != 2) {
25)     std::cerr << "usage: " << argv[0] << " <config file>" << std::endl;
26)     return EXIT_FAILURE;
27)   }
28)   std::string configFileName(argv[1]);
29) 
Stefan Schuermans finish binaries, add readme

Stefan Schuermans authored 4 years ago

30)   // load configuration
31)   Config config;
Stefan Schuermans begin of permissioner confi...

Stefan Schuermans authored 4 years ago

32)   try {
33)     config.parseFile(configFileName);
34)   } catch (std::exception const &e) {
35)     std::cerr << "error: " << e.what() << std::endl;
36)     return EXIT_FAILURE;
37)   }
38) 
Stefan Schuermans finish binaries, add readme

Stefan Schuermans authored 4 years ago

39)   // catch signals to exit properly on Ctrl-C and so on
40)   signal(SIGINT, sighandler);
41)   signal(SIGPIPE, sighandler);
42)   signal(SIGQUIT, sighandler);
43)   signal(SIGTERM, sighandler);
44) 
45)   std::cout << "permissionerd (" << configFileName << ") starting"
46)             << std::endl;
47) 
Stefan Schuermans add nice and I/O idle config

Stefan Schuermans authored 4 years ago

48)   // set nicecess of process
49)   config.getNice().apply();
50) 
Stefan Schuermans finish binaries, add readme

Stefan Schuermans authored 4 years ago

51)   // continuously set ownership and permissions
52)   int ret = EXIT_SUCCESS;
53)   while (go_on) {
54) 
55)     // set owneship and permissions, measure time it takes
56)     std::cout << "permissionerd (" << configFileName
57)               << "): setting ownership and permissions" << std::endl;
58)     auto begin = std::chrono::steady_clock::now();
59)     try {
60)       config.setPermissions();
61)     } catch (std::exception const &e) {
62)       std::cerr << "error: " << e.what() << std::endl;
63)       ret = EXIT_FAILURE;
64)       break;
65)     }
66)     auto end = std::chrono::steady_clock::now();
67)     std::chrono::duration<float, std::ratio<1>> duration = end - begin;
68)     std::cout << "permissionerd (" << configFileName << "): took "
69)               << duration.count() << " s" << std::endl;
70) 
71)     // sleep 10 times as long as the work took plus one second
72)     auto sleep_time = 10 * duration
73)                     + std::chrono::duration<int, std::ratio<1>>(1);
74)     std::this_thread::sleep_for(sleep_time);
75) 
76)   } // while (go_on)
77) 
78)   std::cout << "permissionerd (" << configFileName << ") shutting down"
79)             << std::endl;
80) 
81)   return ret;