Revert "Remove TPCS and TWPServer features"
[platform/upstream/csf-framework.git] / framework / IpcForkDaemon.c
1 /*
2     Copyright (c) 2014, McAfee, Inc.
3
4     All rights reserved.
5
6     Redistribution and use in source and binary forms, with or without modification,
7     are permitted provided that the following conditions are met:
8
9     Redistributions of source code must retain the above copyright notice, this list
10     of conditions and the following disclaimer.
11
12     Redistributions in binary form must reproduce the above copyright notice, this
13     list of conditions and the following disclaimer in the documentation and/or other
14     materials provided with the distribution.
15
16     Neither the name of McAfee, Inc. nor the names of its contributors may be used
17     to endorse or promote products derived from this software without specific prior
18     written permission.
19
20     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23     IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24     INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25     BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27     LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
28     OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29     OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /**
33  * \file IpcForkDaemon.c
34  * \brief Ipc Fork Daemon source file.
35  *
36  * This file implements the Ipc Fork Daemon function used by Security framework.
37  */
38
39 #include "Debug.h"
40 #include "IpcForkDaemon.h"
41 #include <signal.h>
42
43 void
44 fork_daemon()
45 {
46     pid_t pid;
47
48     // Fork off the parent process.
49     pid = fork();
50
51     // An error occurred.
52     if (pid < 0)
53     {
54         DERR("%s\n", "unable to start the TWPSerDaemon");
55         exit(EXIT_FAILURE);
56     }
57
58     // Success: Let the parent terminate.
59     if (pid > 0)
60     {
61         DERR("%s\n", "Successfully forked the child process");
62         exit(EXIT_SUCCESS);
63     }
64
65     // On success: The child process becomes session leader.
66     if (setsid() < 0)
67     {
68         DERR("%s\n", "unable to start the TWPSerDaemon");
69         exit(EXIT_FAILURE);
70     }
71
72     signal(SIGCHLD, SIG_IGN);
73     signal(SIGHUP, SIG_IGN);
74
75     // Fork off for the second time to ensure session leading process ends.
76     pid = fork();
77
78     // An error occurred.
79     if (pid < 0)
80     {
81         DERR("%s\n", "unable to start the TWPSerDaemon");
82         exit(EXIT_FAILURE);
83     }
84
85     // Success: Let the session leading process end.
86     if (pid > 0)
87     {
88         DERR("%s\n", "Successfully forked the TWPSerDaemon process in the new session");
89         exit(EXIT_SUCCESS);
90     }
91
92     //close the file descriptors of terminal associated with the process
93     close(STDIN_FILENO);
94     close(STDOUT_FILENO);
95     close(STDERR_FILENO);
96 }