1: <?php
2: /**
3: * A Service management object meant for OpenBSD
4: *
5: * this object should allow; starting, stopping, settuping and removing
6: * OpenBSD services.
7: * Meant to virtualize the base command line tools so we can later port
8: * this object to other OSes like Linux, CentOS, etc..
9: *
10: */
11: class Service {
12: public $SUPPORTED_COMMANDS=array("start"=>"start a service","stop"=>"Stop a service","setup"=>"Setup a service for boot time","remove"=>"Remove a service from boot time");
13: public $Digest=array();
14: // public $Repos = array();
15: private $TFile;
16:
17: /**
18: * Service::__construct()
19: * Instantiate a Service object, just make sure to pass in a File object, it needs it.
20: * @param mixed $obj_File
21: */
22: function __construct(&$obj_File){
23: global $LOGLEVEL;
24:
25: if (isset($LOGLEVEL) AND $LOGLEVEL ) {
26: print "class Service instantiated\n";
27: }
28: $this->TFile=$obj_File;
29: }
30:
31: /**
32: * Service::Start()
33: * Start a service.
34: * @param mixed $Service_Name Service_Name (see /etc/rc.d/*)
35: * @param mixed $Ovr_ServiceName_PS_Check If using PS to check service status, you can override the string used to grep the PS output
36: * @param boolean $flag_Restart_ifRunning By default true, will kick a restart if already running
37: * @return boolean result of the operation
38: */
39: public function Start($Service_Name,$Ovr_ServiceName_PS_Check=null,$flag_Restart_ifRunning=true){
40:
41: if (! file_exists("/etc/rc.d/$Service_Name")) {
42: print "Service->Start() : ERROR : ($Service_Name) doesn't have an rc.d file under /etc/rc.d/\n";
43: return false;
44: }
45: // $result=trim(`/etc/rc.d/$Service_Name start`);
46: if ($flag_Restart_ifRunning AND $this->isRunning($Ovr_ServiceName_PS_Check)) {
47: $result=$this->TFile->Run_Process("/etc/rc.d/$Service_Name restart",true);
48: $start_verbiage="restarted";
49: } else {
50: $result=$this->TFile->Run_Process("/etc/rc.d/$Service_Name start",true);
51: $start_verbiage="started";
52: }
53:
54:
55: /**
56: * do a (simple?) ps -ax check to see if the process is indeed active.
57: * use that as a return code, cauz I think the rc stuff is still too sketchy in OpenBSd...
58: *
59: */
60:
61: if (! is_null($Ovr_ServiceName_PS_Check) ) {
62: $start_result= $this->isRunning($Ovr_ServiceName_PS_Check);
63: // $num_procs=intval(trim(`ps -ax | grep $Ovr_ServiceName_PS_Check | grep -v grep | wc -l`));
64: } else {
65: $start_result= $this->isRunning($Service_Name);
66: // $num_procs=intval(trim(`ps -ax | grep $Service_Name | grep -v grep | wc -l`));
67: }
68:
69: if ($start_result) {
70: print __CLASS__."->".__METHOD__." : Service $Service_Name has been $start_verbiage with success [$result]\n";
71: }
72: return $start_result;
73:
74: //return $num_procs;
75: }
76:
77: /**
78: * Service::isRunning()
79: * Check to see if a service is running
80: * @param mixed $Service_Name
81: * @return boolean $result
82: */
83: public function isRunning($Service_Name){
84: /**
85: * Ah ! /etc/rc.d/servicename check, returns the current status. ;)
86: *
87: */
88: $result=$this->TFile->Run_Process("/etc/rc.d/$Service_Name check",true);
89:
90: return $result;
91: }
92:
93: /**
94: * Service::Stop()
95: * Stop a running service
96: * @param mixed $Service_Name
97: * @return boolean $result
98: */
99: public function Stop($Service_Name){
100: if (! file_exists("/etc/rc.d/$Service_Name")) {
101: print __CLASS__."->".__METHOD__." : CRITICAL ERROR : ($Service_Name) doesn't have an rc.d file under /etc/rc.d/ so it cannot be stopped properly\n";
102: return false;
103: }
104: $result=trim(`/etc/rc.d/$Service_Name stop`);
105:
106: return $result;
107: }
108:
109:
110: /**
111: * Service::Setup()
112: * Configure a service to start automatically at boot-time, using the rc.d methodology on OpenBSD
113: * @param mixed $Service_Name
114: * @param mixed $daemon_User
115: * @param mixed $daemon_Flags
116: * @return boolean $result
117: */
118: public function Setup($Service_Name,$daemon_User=null,$daemon_Flags=null){
119: /**
120: * TODO: A global way of defining the Repository variable, which
121: * could be multiple, and thus, perhaps, a global function to retrieve ONE.
122: *
123: */
124: if (! file_exists("/etc/rc.d/$Service_Name")) {
125: print "Service->Setup() : ERROR : ($Service_Name) doesn't have an rc.d file under /etc/rc.d/\n";
126: return false;
127: }
128:
129: /**
130: * meddle in the boot confs... args.
131: *
132: */
133: // add daemon_user to /etc/rc.conf.local, if specified.
134: if (! is_null($daemon_User) ) {
135: $result=$this->TFile->AddLine("/etc/rc.conf.local","${Service_Name}_daemon=$daemon_User");
136: }
137:
138: // add the daemon_flags to /etc/rc.conf.local, if specified
139: if (! is_null($daemon_Flags) ) {
140: $result=$this->TFile->AddLine("/etc/rc.conf.local","${Service_Name}_flags=$daemon_Flags");
141: } else {
142: // assuming we want to activate this service...
143: $result=$this->TFile->AddLine("/etc/rc.conf.local","${Service_Name}_flags=\"\"");
144: }
145: // add the service to pkg_scripts in /etc/rc.conf.local
146: $result=$this->TFile->Append_To_Var("/etc/rc.conf.local","pkg_scripts",$Service_Name);
147:
148:
149: return $result;
150: }
151:
152: /**
153: * Service::Remove()
154: * Remove a particular service from the host's booting process, without uninstalling anything.
155: * @param mixed $Service_Name
156: *@return boolean $result
157: */
158: public function Remove($Service_Name){
159: /**
160: * remove from pkg_scripts, and the corresponding service_flags and service_daemon lines
161: * from /etc/rc.conf.local
162: *
163: */
164: $this->TFile->RemoveLine("/etc/rc.conf.local","${Service_Name}_flags=");
165:
166: $this->TFile->RemoveLine("/etc/rc.conf.local","${Service_Name}_daemon=");
167:
168: // essentially, if we could remove it from pkg_scripts, we should be happy.
169: $result=$this->TFile->Remove_From_Var("/etc/rc.conf.local","pkg_scripts",$Service_Name);
170: return $result;
171: }
172: }
173:
174:
175: ?>
176: