Starting move to lib
[mnenc] / mnencd.cpp
1 /*
2 Copyright (c) 2004, Devin Watson <dmwatson@comcast.net> 
3 and 2010 Micke Nordin <mickewiki@gmail.com>
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8     * Redistributions of source code must retain the above copyright
9       notice, this list of conditions and the following disclaimer.
10     * Redistributions in binary form must reproduce the above copyright
11       notice, this list of conditions and the following disclaimer in the
12       documentation and/or other materials provided with the distribution.
13     * Neither the name of mnenc nor the
14       names of its contributors may be used to endorse or promote products
15       derived from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL Devin Watson BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * */
28
29 #include <dirent.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33 #include <syslog.h>
34 #include <fcntl.h>
35 #include <cstdio>
36 #include <cstdlib>
37 #include <string>
38 #include <cerrno>
39 #include <fstream>
40 #include <iostream>
41 #include <mnenc.hpp>
42 #include "mnencd.hpp"
43
44 int main(int argc, char** argv) {
45         /* Our process ID and Session ID */
46         pid_t pid, sid;
47         
48         /* Fork off the parent process */
49         pid = fork();
50         if (pid < 0) {
51                 exit(EXIT_FAILURE);
52         }
53         /* If we got a good PID, then
54            we can exit the parent process. */
55         if (pid > 0) {
56                 exit(EXIT_SUCCESS);
57         }
58
59         /* Change the file mode mask */
60         umask(0);
61                 
62         /* Open any logs here */        
63         std::string name, dirname;
64         name = getenv("USER");
65         dirname = "/home/" + name + "/.mnenc/";
66         if(mkdir(dirname.c_str(), S_IRWXU | S_IRWXG | S_IRWXO ) < 0) {
67                 if(errno != EEXIST) {
68                         exit(EXIT_FAILURE);
69                 }
70         }
71         /*This is log stuff */
72         std::string logname = "/home/" + name + "/.mnenc/log";
73         std::ofstream log; //Log stream
74         log.open(logname.c_str(), ios::app); // open log
75         time_t rawtime; //Time stuff here
76         struct tm * timeinfo;
77         time ( &rawtime );
78         timeinfo = localtime ( &rawtime );
79         std::string current_time = asctime (timeinfo); //Holds current time
80         
81         /* Create a new SID for the child process */
82         sid = setsid();
83         if (sid < 0) {
84                 log << current_time << ": Unable to create a new SID for the child process\n";
85                 exit(EXIT_FAILURE);
86         }
87         
88         /* Change the current working directory */
89         if ((chdir("/tmp/")) < 0) {
90                 log << current_time << ": Could not change to /tmp\n";
91                 exit(EXIT_FAILURE);
92         }
93         
94         /* Close out the standard file descriptors */
95         close(STDIN_FILENO);
96         close(STDOUT_FILENO);
97         close(STDERR_FILENO);
98         
99         /* Daemon-specific initialization goes here */
100         mkfifo("./mnencdfifo", 0777); //This is the fifo for communications
101         signal(SIGTERM, term); // register a SIGTERM handler
102         log.close(); //Closing log 
103         
104         /* The Big Loop */
105         while (1) {
106                 std::string request = m_read();
107                 std::string reply = do_something(request);
108                 m_send(reply);
109                 sleep(1); /* wait 1 second*/
110         }
111         exit(EXIT_SUCCESS);
112 }