Skip to content

Commit

Permalink
dinitctl: slight improvement to err msg when no instance running
Browse files Browse the repository at this point in the history
It seems like it's common enough for users to be confused when run a
dinitctl command with no user instance, so slightly improve the message
(to suggest the reason for socket connection failure might be the
absence of a user instance). It might not be enough to resolve all
confusion but it is at least an improvement.
  • Loading branch information
davmac314 committed Dec 28, 2023
1 parent 251cbc6 commit e2ba0ca
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/dinitctl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -531,10 +531,13 @@ int dinitctl_main(int argc, char **argv)
}
}

bool user_specified_cs_path = false;

if (!use_passed_cfd) {
// Locate control socket
if (!control_socket_str.empty()) {
control_socket_path = control_socket_str.c_str();
user_specified_cs_path = true;
}
else {
control_socket_path = get_default_socket_path(control_socket_str, user_dinit);
Expand Down Expand Up @@ -626,6 +629,14 @@ int dinitctl_main(int argc, char **argv)
catch (dinit_protocol_error &e) {
cerr << "dinitctl: protocol error" << endl;
}
catch (control_sock_conn_err &ce) {
cerr << "dinitctl: " << ce.get_action() << ": " << ce.get_arg() << ": " << strerror(ce.get_err()) << "\n";
if (user_dinit && ce.get_err() == ENOENT && !user_specified_cs_path) {
// It is common enough that users don't realise they need to have a user instance
// running in order to control it, so elaborate a little:
cerr << "dinitctl: perhaps no user instance is running?\n";
}
}
catch (general_error &ge) {
std::cerr << "dinitctl";
if (ge.get_action() != nullptr) {
Expand Down
11 changes: 10 additions & 1 deletion src/includes/dinit-client.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ class general_error
std::string &get_arg() { return arg; }
};

// Specialise general_error for the case of connecting to the control socket
class control_sock_conn_err : public general_error
{
public:
control_sock_conn_err(int err, std::string sockpath) : general_error(err, "connecting to socket", std::move(sockpath))
{
}
};

// static_membuf: a buffer of a fixed size (N) with one additional value (of type T). Don't use this
// directly, construct via membuf.
template <int N> class static_membuf
Expand Down Expand Up @@ -333,7 +342,7 @@ inline int connect_to_daemon(const char *control_socket_path)
free(name);

if (connr == -1) {
throw general_error(errno, "connecting to socket", control_socket_path);
throw control_sock_conn_err(errno, control_socket_path);
}

return socknum;
Expand Down

0 comments on commit e2ba0ca

Please sign in to comment.