+
+// exception for cancelling a service operation
+class service_op_cancel { };
+
+static int enable_disable_service(int socknum, cpbuffer_t &rbuffer, const char *from, const char *to, bool enable)
+{
+ using namespace std;
+
+ service_state_t from_state = service_state_t::STARTED;
+ handle_t from_handle;
+
+ handle_t to_handle;
+
+ if (! load_service(socknum, rbuffer, from, &from_handle, &from_state)
+ || ! load_service(socknum, rbuffer, to, &to_handle, nullptr)) {
+ return 1;
+ }
+
+ // Get service load path
+ char buf[1] = { DINIT_CP_QUERY_LOAD_MECH };
+ write_all_x(socknum, buf, 1);
+
+ wait_for_reply(rbuffer, socknum);
+
+ if (rbuffer[0] != DINIT_RP_LOADER_MECH) {
+ cerr << "dinitctl: Control socket protocol error" << endl;
+ return 1;
+ }
+
+ // Packet type, load mechanism type, packet size:
+ fill_buffer_to(rbuffer, socknum, 2 + sizeof(uint32_t));
+
+ if (rbuffer[1] != SSET_TYPE_DIRLOAD) {
+ cerr << "dinitctl: unknown configuration, unable to load service descriptions" << endl;
+ return 1;
+ }
+
+ vector<string> paths;
+
+ uint32_t pktsize;
+ rbuffer.extract(&pktsize, 2, sizeof(uint32_t));
+
+ fill_buffer_to(rbuffer, socknum, 2 + sizeof(uint32_t) * 3); // path entries, cwd length
+
+ uint32_t path_entries; // number of service directories
+ rbuffer.extract(&path_entries, 2 + sizeof(uint32_t), sizeof(uint32_t));
+
+ uint32_t cwd_len;
+ rbuffer.extract(&cwd_len, 2 + sizeof(uint32_t) * 2, sizeof(uint32_t));
+ rbuffer.consume(2 + sizeof(uint32_t) * 3);
+ pktsize -= 2 + sizeof(uint32_t) * 3;
+
+ // Read current working directory of daemon:
+ std::string dinit_cwd = read_string(socknum, rbuffer, cwd_len);
+
+ // dinit daemon base directory against which service paths are resolved is in dinit_cwd
+
+ for (int i = 0; i < (int)path_entries; i++) {
+ uint32_t plen;
+ fill_buffer_to(rbuffer, socknum, sizeof(uint32_t));
+ rbuffer.extract(&plen, 0, sizeof(uint32_t));
+ rbuffer.consume(sizeof(uint32_t));
+ paths.push_back(read_string(socknum, rbuffer, plen));
+ }
+
+ // all service directories are now in the 'paths' vector
+ // Load/read service description for 'from' service:
+
+ ifstream service_file;
+ string service_file_path;
+
+ for (std::string path : paths) {
+ string test_path = combine_paths(dinit_cwd + '/' + path, from);
+
+ service_file.open(test_path.c_str(), ios::in);
+ if (service_file) {
+ service_file_path = test_path;
+ break;
+ }
+ }
+
+ if (! service_file) {
+ cerr << "dinitctl: could not locate service file for service '" << from << "'" << endl;
+ return 1;
+ }
+
+ // We now need to read the service file, identify the waits-for.d directory (bail out if more than one),
+ // make sure the service is not listed as a dependency individually.
+
+ string waits_for_d;
+
+ try {
+ process_service_file(from, service_file, [&](string &line, string &setting,
+ dinit_load::string_iterator i, dinit_load::string_iterator end) -> void {
+ if (setting == "waits-for" || setting == "depends-on" || setting == "depends-ms") {
+ string dname = dinit_load::read_setting_value(i, end);
+ if (dname == to) {
+ // There is already a dependency
+ cerr << "dinitctl: there is a fixed dependency to service '" << to
+ << "' in the service description of '" << from << "'." << endl;
+ throw service_op_cancel();
+ }
+ }
+ else if (setting == "waits-for.d") {
+ string dname = dinit_load::read_setting_value(i, end);
+ if (! waits_for_d.empty()) {
+ cerr << "dinitctl: service '" << from << "' has multiple waits-for.d directories "
+ << "specified in service description" << endl;
+ throw service_op_cancel();
+ }
+ waits_for_d = std::move(dname);
+ }
+ });
+ }
+ catch (const service_op_cancel &cexc) {
+ return 1;
+ }
+
+ // If the from service has no waits-for.d specified, we can't continue
+ if (waits_for_d.empty()) {
+ cerr << "dinitctl: service '" << from << "' has no waits-for.d directory specified" << endl;
+ return 1;
+ }
+
+ // The waits-for.d path is relative to the service file path, combine:
+ string waits_for_d_full = combine_paths(parent_path(service_file_path), waits_for_d.c_str());
+
+ // check if dependency already exists
+ string dep_link_path = combine_paths(waits_for_d_full, to);
+ struct stat stat_buf;
+ if (lstat(dep_link_path.c_str(), &stat_buf) == -1) {
+ if (errno != ENOENT) {
+ cerr << "dinitctl: checking for existing dependency link: " << dep_link_path << ": "
+ << strerror(errno) << endl;
+ return 1;
+ }
+ }
+ else {
+ // dependency already exists
+ if (enable) {
+ cerr << "dinitctl: service already enabled." << endl;
+ return 1;
+ }
+ }
+
+ // warn if 'from' service is not started
+ if (enable && from_state != service_state_t::STARTED) {
+ cerr << "dinitctl: warning: enabling dependency for non-started service" << endl;
+ }
+
+ // add/remove dependency
+ constexpr int enable_pktsize = 2 + sizeof(handle_t) * 2;
+ char cmdbuf[enable_pktsize] = { char(enable ? DINIT_CP_ENABLESERVICE : DINIT_CP_REM_DEP),
+ char(dependency_type::WAITS_FOR)};
+ memcpy(cmdbuf + 2, &from_handle, sizeof(from_handle));
+ memcpy(cmdbuf + 2 + sizeof(from_handle), &to_handle, sizeof(to_handle));
+ write_all_x(socknum, cmdbuf, enable_pktsize);
+
+ wait_for_reply(rbuffer, socknum);
+
+ // check reply
+ if (enable && rbuffer[0] == DINIT_RP_NAK) {
+ cerr << "dinitctl: Could not enable service: possible circular dependency" << endl;
+ return 1;
+ }
+ if (rbuffer[0] != DINIT_RP_ACK) {
+ cerr << "dinitctl: Control socket protocol error" << endl;
+ return 1;
+ }
+
+ // create link
+ if (enable) {
+ if (symlink((string("../") + to).c_str(), dep_link_path.c_str()) == -1) {
+ cerr << "dinitctl: Could not create symlink at " << dep_link_path << ": " << strerror(errno) << "\n"
+ "dinitctl: Note: service was activated, but will not be enabled on restart." << endl;
+ return 1;
+ }
+ }
+ else {
+ if (unlink(dep_link_path.c_str()) == -1) {
+ cerr << "dinitctl: Could not unlink dependency entry " << dep_link_path << ": " << strerror(errno) << "\n"
+ "dinitctl: Note: service was disabled, but will be re-enabled on restart." << endl;
+ return 1;
+ }
+ }
+
+ return 0;
+}