355a11314d16d31e243965227820735062b2a680
Stefan Schuermans add copyright & license

Stefan Schuermans authored 3 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 3 years ago

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

Stefan Schuermans authored 3 years ago

8) 
Stefan Schuermans finish binaries, add readme

Stefan Schuermans authored 3 years ago

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

Stefan Schuermans authored 3 years ago

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

Stefan Schuermans authored 3 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 3 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 3 years ago

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

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