diff --git a/ChangeLog.txt b/ChangeLog.txt index 1630e251..f67fc472 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1,3 +1,7 @@ +Clients: +- All clients exit with an error exit code on CONNACK failure. Closes #1778. + + 1.6.11 - 2020-08-11 =================== diff --git a/client/pub_client.c b/client/pub_client.c index ef0bf16e..3b0078a8 100644 --- a/client/pub_client.c +++ b/client/pub_client.c @@ -46,6 +46,7 @@ static bool disconnect_sent = false; static int publish_count = 0; static bool ready_for_repeat = false; static volatile int status = STATUS_CONNECTING; +static int connack_result = 0; #ifdef WIN32 static uint64_t next_publish_tv; @@ -129,6 +130,8 @@ void my_connect_callback(struct mosquitto *mosq, void *obj, int result, int flag UNUSED(flags); UNUSED(properties); + connack_result = result; + if(!result){ switch(cfg.pub_mode){ case MSGMODE_CMD: @@ -555,7 +558,11 @@ int main(int argc, char *argv[]) if(rc){ err_printf(&cfg, "Error: %s\n", mosquitto_strerror(rc)); } - return rc; + if(connack_result){ + return connack_result; + }else{ + return rc; + } cleanup: mosquitto_lib_cleanup(); diff --git a/client/rr_client.c b/client/rr_client.c index 4e22fa3b..dec94686 100644 --- a/client/rr_client.c +++ b/client/rr_client.c @@ -52,6 +52,7 @@ extern struct mosq_config cfg; bool process_messages = true; int msg_count = 0; struct mosquitto *mosq = NULL; +static int connack_result = 0; #ifndef WIN32 void my_signal_handler(int signum) @@ -117,6 +118,7 @@ void my_message_callback(struct mosquitto *mosq, void *obj, const struct mosquit void my_connect_callback(struct mosquitto *mosq, void *obj, int result, int flags, const mosquitto_property *properties) { + connack_result = result; if(!result){ client_state = rr_s_connected; mosquitto_subscribe_v5(mosq, NULL, cfg.response_topic, cfg.qos, 0, cfg.subscribe_props); @@ -363,7 +365,11 @@ int main(int argc, char *argv[]) if(rc){ err_printf(&cfg, "Error: %s\n", mosquitto_strerror(rc)); } - return rc; + if(connack_result){ + return connack_result; + }else{ + return rc; + } cleanup: mosquitto_lib_cleanup(); diff --git a/client/sub_client.c b/client/sub_client.c index 7eeaae59..fb26638f 100644 --- a/client/sub_client.c +++ b/client/sub_client.c @@ -40,6 +40,7 @@ bool process_messages = true; int msg_count = 0; struct mosquitto *mosq = NULL; int last_mid = 0; +static int connack_result = 0; #ifndef WIN32 void my_signal_handler(int signum) @@ -117,6 +118,7 @@ void my_connect_callback(struct mosquitto *mosq, void *obj, int result, int flag UNUSED(flags); UNUSED(properties); + connack_result = result; if(!result){ mosquitto_subscribe_multiple(mosq, NULL, cfg.topic_count, cfg.topics, cfg.qos, cfg.sub_opts, cfg.subscribe_props); @@ -371,7 +373,11 @@ int main(int argc, char *argv[]) if(rc){ err_printf(&cfg, "Error: %s\n", mosquitto_strerror(rc)); } - return rc; + if(connack_result){ + return connack_result; + }else{ + return rc; + } cleanup: mosquitto_destroy(mosq); diff --git a/man/mosquitto_passwd.1.xml b/man/mosquitto_passwd.1.xml index 5f144bf9..3c0abd3f 100644 --- a/man/mosquitto_passwd.1.xml +++ b/man/mosquitto_passwd.1.xml @@ -109,6 +109,64 @@ + + Exit Status + + mosquitto_sub returns zero on success, or non-zero on error. If + the connection is refused by the broker at the MQTT level, then + the exit code is the CONNACK reason code. If another error + occurs, the exit code is a libmosquitto return value. + + + MQTT v3.1.1 CONNACK codes: + + Success + Connection refused: Bad protocol version + Connection refused: Identifier rejected + Connection refused: Server unavailable + Connection refused: Bad username/password + Connection refused: Not authorized + + + MQTT v5 CONNACK codes: + + Success + Unspecified error + Malformed packet + Protocol error + Implementation specific error + Unsupported protocol version + Client ID not valid + Bad username or password + Not authorized + Server unavailable + Server busy + Banned + Server shutting down + Bad authentication method + Keep alive timeout + Session taken over + Topic filter invalid + Topic name invalid + Receive maximum exceeded + Topic alias invalid + Packet too large + Message rate too high + Quota exceeded + Administrative action + Payload format invalid + Retain not supported + QoS not supported + Use another server + Server moved + Shared subscriptions not supported + Connection rate exceeded + Maximum connect time + Subscription IDs not supported + Wildcard subscriptions not supported + + + Examples Add a user to a new password file: diff --git a/man/mosquitto_pub.1.xml b/man/mosquitto_pub.1.xml index a05a938f..58991749 100644 --- a/man/mosquitto_pub.1.xml +++ b/man/mosquitto_pub.1.xml @@ -654,6 +654,64 @@ + + Exit Status + + mosquitto_sub returns zero on success, or non-zero on error. If + the connection is refused by the broker at the MQTT level, then + the exit code is the CONNACK reason code. If another error + occurs, the exit code is a libmosquitto return value. + + + MQTT v3.1.1 CONNACK codes: + + Success + Connection refused: Bad protocol version + Connection refused: Identifier rejected + Connection refused: Server unavailable + Connection refused: Bad username/password + Connection refused: Not authorized + + + MQTT v5 CONNACK codes: + + Success + Unspecified error + Malformed packet + Protocol error + Implementation specific error + Unsupported protocol version + Client ID not valid + Bad username or password + Not authorized + Server unavailable + Server busy + Banned + Server shutting down + Bad authentication method + Keep alive timeout + Session taken over + Topic filter invalid + Topic name invalid + Receive maximum exceeded + Topic alias invalid + Packet too large + Message rate too high + Quota exceeded + Administrative action + Payload format invalid + Retain not supported + QoS not supported + Use another server + Server moved + Shared subscriptions not supported + Connection rate exceeded + Maximum connect time + Subscription IDs not supported + Wildcard subscriptions not supported + + + Examples Publish temperature information to localhost with QoS 1: diff --git a/man/mosquitto_sub.1.xml b/man/mosquitto_sub.1.xml index 55867c7e..2e7e4bd6 100644 --- a/man/mosquitto_sub.1.xml +++ b/man/mosquitto_sub.1.xml @@ -893,6 +893,64 @@ mosquitto_sub -t 'bbc/#' -T bbc/bbc1 --remove-retained + + Exit Status + + mosquitto_sub returns zero on success, or non-zero on error. If + the connection is refused by the broker at the MQTT level, then + the exit code is the CONNACK reason code. If another error + occurs, the exit code is a libmosquitto return value. + + + MQTT v3.1.1 CONNACK codes: + + Success + Connection refused: Bad protocol version + Connection refused: Identifier rejected + Connection refused: Server unavailable + Connection refused: Bad username/password + Connection refused: Not authorized + + + MQTT v5 CONNACK codes: + + Success + Unspecified error + Malformed packet + Protocol error + Implementation specific error + Unsupported protocol version + Client ID not valid + Bad username or password + Not authorized + Server unavailable + Server busy + Banned + Server shutting down + Bad authentication method + Keep alive timeout + Session taken over + Topic filter invalid + Topic name invalid + Receive maximum exceeded + Topic alias invalid + Packet too large + Message rate too high + Quota exceeded + Administrative action + Payload format invalid + Retain not supported + QoS not supported + Use another server + Server moved + Shared subscriptions not supported + Connection rate exceeded + Maximum connect time + Subscription IDs not supported + Wildcard subscriptions not supported + + + Examples Note that these really are examples - the subscriptions will work