1 #!/usr/bin/env ucode 2 3 import * as libubus from "ubus"; 4 5 let script_path = ARGV[0]; 6 let proto_name = ARGV[1]; 7 let action = ARGV[2]; 8 let iface_name = ARGV[3]; 9 let config_json = ARGV[4]; 10 let device = ARGV[5]; 11 12 let config; 13 try { 14 let blob = json(config_json); 15 let inner = blob?._ucode_config; 16 config = inner ? json(inner) : blob; 17 } catch (e) { 18 warn(`Failed to parse config JSON: ${e}\n${e.stacktrace[0].context}\n`); 19 exit(1); 20 } 21 22 let ubus = libubus.connect(); 23 if (!ubus) { 24 warn(`Failed to connect to ubus\n`); 25 exit(1); 26 } 27 28 let notify_path = `network.interface.${iface_name}`; 29 30 function proto_notify(data) 31 { 32 return ubus.call(notify_path, "notify_proto", data); 33 } 34 35 let proto = { 36 iface: iface_name, 37 proto: proto_name, 38 config, 39 device, 40 41 notify: proto_notify, 42 43 update_link: function(up, data) { 44 let msg = { action: 0, "link-up": up, ...(data ?? {}) }; 45 return proto_notify(msg); 46 }, 47 48 run_command: function(argv, env) { 49 let msg = { action: 1, command: argv }; 50 if (env) 51 msg.env = env; 52 return proto_notify(msg); 53 }, 54 55 kill_command: function(signal) { 56 return proto_notify({ action: 2, signal: signal ?? 15 }); 57 }, 58 59 error: function(errors) { 60 return proto_notify({ action: 3, error: errors }); 61 }, 62 63 block_restart: function() { 64 return proto_notify({ action: 4 }); 65 }, 66 67 set_available: function(available) { 68 return proto_notify({ action: 5, available }); 69 }, 70 71 add_host_dependency: function(host, ifname) { 72 let msg = { action: 6 }; 73 if (host) 74 msg.host = host; 75 if (ifname) 76 msg.ifname = ifname; 77 return proto_notify(msg); 78 }, 79 80 setup_failed: function() { 81 return proto_notify({ action: 7 }); 82 }, 83 }; 84 85 let handlers = {}; 86 87 let netifd_stub = { 88 add_proto: function(handler) { 89 if (handler?.name) 90 handlers[handler.name] = handler; 91 }, 92 }; 93 94 try { 95 include(script_path, { netifd: netifd_stub }); 96 } catch (e) { 97 warn(`Failed to load proto handler script '${script_path}': ${e}\n${e.stacktrace[0].context}\n`); 98 exit(1); 99 } 100 101 let handler = handlers[proto_name]; 102 if (!handler) { 103 warn(`No handler found for protocol '${proto_name}'\n`); 104 exit(1); 105 } 106 107 if (!handler[action]) { 108 warn(`Handler '${proto_name}' has no '${action}' function\n`); 109 exit(1); 110 } 111 112 handler[action](proto);
This page was automatically generated by LXR 0.3.1. • OpenWrt