Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 3 | import sys |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 4 | import subprocess |
Norman James | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 5 | #from gi.repository import GObject |
| 6 | import gobject |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 7 | import dbus |
| 8 | import dbus.service |
| 9 | import dbus.mainloop.glib |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 10 | import os |
| 11 | import PropertyManager |
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 12 | import time |
| 13 | import json |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 14 | import Openbmc |
| 15 | |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 16 | if (len(sys.argv) < 2): |
| 17 | print "Usage: system_manager.py [system name]" |
| 18 | exit(1) |
| 19 | |
| 20 | System = __import__(sys.argv[1]) |
| 21 | import Openbmc |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 22 | |
| 23 | DBUS_NAME = 'org.openbmc.managers.System' |
| 24 | OBJ_NAME = '/org/openbmc/managers/System' |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 25 | HEARTBEAT_CHECK_INTERVAL = 20000 |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 26 | |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 27 | |
| 28 | class SystemManager(dbus.service.Object): |
| 29 | def __init__(self,bus,name): |
| 30 | dbus.service.Object.__init__(self,bus,name) |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 31 | self.property_manager = PropertyManager.PropertyManager(bus,System.CACHE_PATH) |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 32 | |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 33 | ## Signal handlers |
| 34 | bus.add_signal_receiver(self.NewBusHandler, |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 35 | dbus_interface = 'org.freedesktop.DBus', |
| 36 | signal_name = "NameOwnerChanged") |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 37 | bus.add_signal_receiver(self.HeartbeatHandler, signal_name = "Heartbeat") |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 38 | bus.add_signal_receiver(self.SystemStateHandler,signal_name = "GotoSystemState") |
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 39 | bus.add_signal_receiver(self.event_log_signal_handler, |
| 40 | dbus_interface = "org.openbmc.EventLog", |
| 41 | signal_name = "EventLog", |
| 42 | path_keyword='path') |
| 43 | |
| 44 | |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 45 | |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 46 | self.current_state = "" |
| 47 | self.system_states = {} |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 48 | for bus_name in System.SYSTEM_CONFIG.keys(): |
| 49 | sys_state = System.SYSTEM_CONFIG[bus_name]['system_state'] |
| 50 | if (self.system_states.has_key(sys_state) == False): |
| 51 | self.system_states[sys_state] = [] |
| 52 | self.system_states[sys_state].append(bus_name) |
| 53 | self.SystemStateHandler("INIT") |
| 54 | print "SystemManager Init Done" |
| 55 | |
Norman James | dfdaca9 | 2015-09-27 22:11:15 -0500 | [diff] [blame] | 56 | @dbus.service.signal('org.openbmc.EventLog') |
| 57 | def EventLog(self, priority, message, rc): |
| 58 | pass |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 59 | |
| 60 | def SystemStateHandler(self,state_name): |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 61 | print "Checking previous state started..." |
| 62 | i = 0 |
Norman James | 7caa10e | 2015-09-27 22:39:52 -0500 | [diff] [blame] | 63 | started = self.check_state_started() |
Norman James | dfdaca9 | 2015-09-27 22:11:15 -0500 | [diff] [blame] | 64 | while(i<10 and started == False): |
| 65 | started = self.check_state_started() |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 66 | i=i+1 |
Norman James | dfdaca9 | 2015-09-27 22:11:15 -0500 | [diff] [blame] | 67 | time.sleep(1) |
| 68 | |
| 69 | if (i == 10): |
| 70 | self.EventLog(1,"Timeout waiting for state to finish: "+self.current_state,1) |
| 71 | return |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 72 | |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 73 | print "Running System State: "+state_name |
| 74 | if (self.system_states.has_key(state_name)): |
| 75 | for bus_name in self.system_states[state_name]: |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 76 | self.start_process(bus_name) |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 77 | |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 78 | if (state_name == "INIT"): |
| 79 | ## Add poll for heartbeat |
Norman James | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 80 | gobject.timeout_add(HEARTBEAT_CHECK_INTERVAL, self.heartbeat_check) |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 81 | |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 82 | if (System.ENTER_STATE_CALLBACK.has_key(state_name)): |
| 83 | cb = System.ENTER_STATE_CALLBACK[state_name] |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 84 | obj = bus.get_<F11>object(cb['bus_name'],cb['obj_name']) |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 85 | method = obj.get_dbus_method(cb['method_name'],cb['interface_name']) |
| 86 | method() |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 87 | |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 88 | self.current_state = state_name |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 89 | |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 90 | def start_process(self,bus_name): |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 91 | if (System.SYSTEM_CONFIG[bus_name]['start_process'] == True): |
| 92 | process_name = System.BIN_PATH+System.SYSTEM_CONFIG[bus_name]['process_name'] |
| 93 | cmdline = [ ] |
| 94 | cmdline.append(process_name) |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 95 | System.SYSTEM_CONFIG[bus_name]['popen'] = None |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 96 | for instance in System.SYSTEM_CONFIG[bus_name]['instances']: |
| 97 | cmdline.append(instance['name']) |
| 98 | try: |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 99 | print "Starting process: "+" ".join(cmdline)+": "+bus_name |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 100 | System.SYSTEM_CONFIG[bus_name]['popen'] = subprocess.Popen(cmdline) |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 101 | except Exception as e: |
| 102 | ## TODO: error |
| 103 | print "Error starting process: "+" ".join(cmdline) |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 104 | |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 105 | def heartbeat_check(self): |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 106 | #print "heartbeat check" |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 107 | for bus_name in System.SYSTEM_CONFIG.keys(): |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 108 | if (System.SYSTEM_CONFIG[bus_name]['start_process'] == True and |
Norman James | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 109 | System.SYSTEM_CONFIG[bus_name].has_key('popen') and |
| 110 | System.SYSTEM_CONFIG[bus_name]['monitor_process'] == True): |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 111 | ## even if process doesn't request heartbeat check, |
| 112 | ## make sure process is still alive |
| 113 | p = System.SYSTEM_CONFIG[bus_name]['popen'] |
| 114 | p.poll() |
| 115 | if (p.returncode != None): |
| 116 | print "Process for "+bus_name+" appears to be dead" |
| 117 | self.start_process(bus_name) |
| 118 | |
| 119 | ## process is alive, now check if heartbeat received |
| 120 | ## during previous interval |
| 121 | elif (System.SYSTEM_CONFIG[bus_name]['heartbeat'] == 'yes'): |
| 122 | if (System.SYSTEM_CONFIG[bus_name]['heartbeat_count'] == 0): |
| 123 | print "Heartbeat error: "+bus_name |
| 124 | p = System.SYSTEM_CONFIG[bus_name]['popen'] |
| 125 | ## TODO: error checking |
| 126 | p.poll() |
| 127 | if (p.returncode == None): |
| 128 | print "Process must be hung, so killing" |
| 129 | p.kill() |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 130 | |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 131 | self.start_process(bus_name) |
| 132 | else: |
| 133 | System.SYSTEM_CONFIG[bus_name]['heartbeat_count'] = 0 |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 134 | #print "Heartbeat ok: "+bus_name |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 135 | |
| 136 | return True |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 137 | |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 138 | def HeartbeatHandler(self,bus_name): |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 139 | #print "Heartbeat seen: "+bus_name |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 140 | System.SYSTEM_CONFIG[bus_name]['heartbeat_count']=1 |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 141 | |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 142 | def check_state_started(self): |
| 143 | r = True |
| 144 | if (self.current_state == ""): |
| 145 | return True |
| 146 | for bus_name in self.system_states[self.current_state]: |
| 147 | if (System.SYSTEM_CONFIG[bus_name].has_key('popen') == False and |
| 148 | System.SYSTEM_CONFIG[bus_name]['start_process'] == True): |
| 149 | print "Not found: "+bus_name |
| 150 | r = False |
| 151 | break; |
| 152 | return r |
| 153 | |
| 154 | |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 155 | def NewBusHandler(self, bus_name, a, b): |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 156 | if (len(b) > 0 and bus_name.find(Openbmc.BUS_PREFIX) == 0): |
| 157 | if (System.SYSTEM_CONFIG.has_key(bus_name)): |
| 158 | System.SYSTEM_CONFIG[bus_name]['heartbeat_count'] = 0 |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 159 | objects = {} |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 160 | Openbmc.get_objs(bus,bus_name,Openbmc.OBJ_PREFIX,objects) |
| 161 | |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 162 | for instance_name in objects.keys(): |
| 163 | obj_path = objects[instance_name]['PATH'] |
| 164 | for instance in System.SYSTEM_CONFIG[bus_name]['instances']: |
| 165 | if (instance.has_key('properties') and instance['name'] == instance_name): |
| 166 | props = instance['properties'] |
| 167 | print "Load Properties: "+obj_path |
| 168 | self.property_manager.loadProperties(bus_name,obj_path,props) |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 169 | ## If object has an init method, call it |
| 170 | for init_intf in objects[instance_name]['INIT']: |
| 171 | obj = bus.get_object(bus_name,obj_path) |
| 172 | intf = dbus.Interface(obj,init_intf) |
| 173 | print "Calling init method: " +obj_path+" : "+init_intf |
| 174 | intf.init() |
Norman James | dfdaca9 | 2015-09-27 22:11:15 -0500 | [diff] [blame] | 175 | |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 176 | |
| 177 | @dbus.service.method(DBUS_NAME, |
| 178 | in_signature='s', out_signature='sis') |
| 179 | def gpioInit(self,name): |
| 180 | gpio_path = '' |
| 181 | gpio_num = 0 |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 182 | if (System.GPIO_CONFIG.has_key(name) == False): |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 183 | # TODO: Error handling |
| 184 | print "ERROR: "+name+" not found in GPIO config table" |
| 185 | return ['',0,''] |
| 186 | else: |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 187 | gpio_num = System.GPIO_CONFIG[name]['gpio_num'] |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 188 | |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 189 | return [Openbmc.GPIO_DEV, gpio_num, System.GPIO_CONFIG[name]['direction']] |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 190 | |
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 191 | ## Signal handler |
| 192 | def event_log_signal_handler(self,priority,msg,rc,path = None): |
| 193 | message = {} |
| 194 | ts = time.time() |
| 195 | |
| 196 | message['priority'] = priority |
| 197 | message['object_path'] = path |
| 198 | message['message'] = msg |
| 199 | message['rc'] = rc |
| 200 | |
| 201 | json_dump = json.dumps(message) |
| 202 | print "EVENT_LOG: "+json_dump |
| 203 | #syslog.openlog('OpenBmc',logoption=syslog.LOG_PID) |
| 204 | #syslog.syslog(priority,json_dump) |
| 205 | #syslog.closelog() |
| 206 | |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 207 | |
| 208 | if __name__ == '__main__': |
| 209 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 210 | bus = dbus.SessionBus() |
| 211 | name = dbus.service.BusName(DBUS_NAME,bus) |
| 212 | obj = SystemManager(bus,OBJ_NAME) |
Norman James | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 213 | mainloop = gobject.MainLoop() |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 214 | |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 215 | print "Running SystemManager" |
| 216 | mainloop.run() |
| 217 | |