Merge branch 'v3-2-test' of git://git.samba.org/samba into v3-2-test
[kai/samba.git] / source3 / services / svc_rcinit.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  Service Control API Implementation
4  *  Copyright (C) Gerald Carter                   2005.
5  *  
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *  
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *  
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "includes.h"
21
22 /*********************************************************************
23 *********************************************************************/
24
25 static WERROR rcinit_stop( const char *service, SERVICE_STATUS *status )
26 {
27         char *command = NULL;
28         int ret, fd;
29
30         if (asprintf(&command, "%s/%s/%s stop",
31                                 get_dyn_LIBDIR(), SVCCTL_SCRIPT_DIR, service) < 0) {
32                 return WERR_NOMEM;
33         }
34
35         /* we've already performed the access check when the service was opened */
36
37         become_root();
38         ret = smbrun( command , &fd );
39         unbecome_root();
40
41         DEBUGADD(5, ("rcinit_start: [%s] returned [%d]\n", command, ret));
42         close(fd);
43
44         SAFE_FREE(command);
45
46         ZERO_STRUCTP( status );
47         status->type = 0x0020;
48         status->state = (ret == 0 ) ? 0x0001 : 0x0004;
49         status->controls_accepted = 0x0005;
50
51         return ( ret == 0 ) ? WERR_OK : WERR_ACCESS_DENIED;
52 }
53
54 /*********************************************************************
55 *********************************************************************/
56
57 static WERROR rcinit_start( const char *service )
58 {
59         char *command = NULL;
60         int ret, fd;
61
62         if (asprintf(&command, "%s/%s/%s start",
63                                 get_dyn_LIBDIR(), SVCCTL_SCRIPT_DIR, service) < 0) {
64                 return WERR_NOMEM;
65         }
66
67         /* we've already performed the access check when the service was opened */
68
69         become_root();
70         ret = smbrun( command , &fd );
71         unbecome_root();
72
73         DEBUGADD(5, ("rcinit_start: [%s] returned [%d]\n", command, ret));
74         close(fd);
75
76         SAFE_FREE(command);
77
78         return ( ret == 0 ) ? WERR_OK : WERR_ACCESS_DENIED;
79 }
80
81 /*********************************************************************
82 *********************************************************************/
83
84 static WERROR rcinit_status( const char *service, SERVICE_STATUS *status )
85 {
86         char *command = NULL;
87         int ret, fd;
88
89         if (asprintf(&command, "%s/%s/%s status",
90                                 get_dyn_LIBDIR(), SVCCTL_SCRIPT_DIR, service) < 0) {
91                 return WERR_NOMEM;
92         }
93
94         /* we've already performed the access check when the service was opened */
95         /* assume as return code of 0 means that the service is ok.  Anything else
96            is STOPPED */
97
98         become_root();
99         ret = smbrun( command , &fd );
100         unbecome_root();
101
102         DEBUGADD(5, ("rcinit_start: [%s] returned [%d]\n", command, ret));
103         close(fd);
104
105         SAFE_FREE(command);
106
107         ZERO_STRUCTP( status );
108         status->type = 0x0020;
109         status->state = (ret == 0 ) ? 0x0004 : 0x0001;
110         status->controls_accepted = 0x0005;
111
112         return WERR_OK;
113 }
114
115 /*********************************************************************
116 *********************************************************************/
117
118 /* struct for svcctl control to manipulate rcinit service */
119
120 SERVICE_CONTROL_OPS rcinit_svc_ops = {
121         rcinit_stop,
122         rcinit_start,
123         rcinit_status
124 };