OSDN Git Service

(LibGoblin)
[drdeamon64/drdeamon64.git] / deamon / drd64_server_common.c
1 /*DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64
2
3                          D r . D e a m o n  6 4
4                         for INTEL64(R), AMD64(R)
5         
6    Copyright(C) 2007-2009 Koine Yuusuke(koinec). All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11  1. Redistributions of source code must retain the above copyright notice,
12     this list of conditions and the following disclaimer.
13  2. Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY Koine Yuusuke(koinec) ``AS IS'' AND ANY
18 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 Koine Yuusuke(koinec) OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64*/
30
31 /* File Info -----------------------------------------------------------
32 File: drd64_.c
33 Function: 
34 Comment: 
35 ----------------------------------------------------------------------*/
36
37 #include"drd64_server.h"
38
39
40 int
41         Drd64_Server_Common_Signal(
42                 int     i_signal, 
43                 void (*func_sigaction) )
44 {
45         int             i_err;
46         struct  sigaction       t_act;
47
48         t_act.sa_handler        = func_sigaction;
49         sigemptyset( &t_act.sa_mask );
50         t_act.sa_flags  = SA_SIGINFO;
51         
52         i_err   = sigaction( i_signal, &t_act, NULL );
53         
54         return i_err;
55 }
56
57
58 int
59         Drd64_Server_Common_Deamonize(
60                 void)
61 {
62         int             i_err;
63         int             i_fdlimit;
64         pid_t   t_err;
65
66         /* Create Deamon Process */
67         t_err   = fork();
68
69         /* Error fork() SystemCall */
70         if( -1 == t_err )       {
71                 return 0x01;
72         }
73         /* Proc. Parent Process => Exit. */
74         else if( 0 != t_err )   {
75                 _exit( 0x00 );
76         }
77         /* VVVVV Proc. Temp Child Process VVVVV */
78         
79
80         /* Create Process Session & Process Group */
81         t_err   = setsid();
82         if( -1 == t_err )       {
83                 return 0x02;
84         }
85
86
87         /* Create Deamon Process */
88         t_err   = fork();
89         if( -1 == t_err )       {
90                 /* Error fork() SystemCall */
91                 return 0x03;
92         }
93         else if( 0 != t_err )   {
94                 /* Proc. Child Process */
95                 _exit( 0x00 );
96         }
97
98
99         /* VVVVV Proc. Deamon Process VVVVV */
100         
101         /* Change Current Directory for Deamon Process */
102         i_err   = chdir( "/" );
103         if( -1 == i_err )       {
104                 return 0x04;
105         }
106                 
107         /* Close Std. I/O */
108         i_fdlimit       = sysconf( _SC_OPEN_MAX );
109         while( --i_fdlimit >= 0 )
110                 { close( i_fdlimit ); }
111
112         
113         return 0x00;
114 }
115
116
117 /* EOF of drd64_.c ----------------------------------- */