From 63e5eed5542c8c85144ee0ac20174c0859fab866 Mon Sep 17 00:00:00 2001
From: Martin Szulecki
Date: Thu, 16 Apr 2009 20:52:29 +0200
Subject: Make sure start_service returns an error if it fails to start the
 service

[#34 state:resolved]

Signed-off-by: Matt Colyer <matt@colyer.name>
---
 include/libiphone/libiphone.h | 27 ++++++++++++++-------------
 src/lockdown.c                | 36 +++++++++++++++++++++---------------
 2 files changed, 35 insertions(+), 28 deletions(-)

diff --git a/include/libiphone/libiphone.h b/include/libiphone/libiphone.h
index 0d5257a..4bc2fea 100644
--- a/include/libiphone/libiphone.h
+++ b/include/libiphone/libiphone.h
@@ -32,23 +32,24 @@ extern "C" {
 #include <plist/plist.h>
 
 //general errors
-#define IPHONE_E_SUCCESS          0
-#define IPHONE_E_INVALID_ARG     -1
-#define IPHONE_E_UNKNOWN_ERROR   -2
-#define IPHONE_E_NO_DEVICE       -3
-#define IPHONE_E_TIMEOUT         -4
-#define IPHONE_E_NOT_ENOUGH_DATA -5
-#define IPHONE_E_BAD_HEADER      -6
+#define IPHONE_E_SUCCESS                0
+#define IPHONE_E_INVALID_ARG           -1
+#define IPHONE_E_UNKNOWN_ERROR         -2
+#define IPHONE_E_NO_DEVICE             -3
+#define IPHONE_E_TIMEOUT               -4
+#define IPHONE_E_NOT_ENOUGH_DATA       -5
+#define IPHONE_E_BAD_HEADER            -6
 
 //lockdownd specific error
-#define IPHONE_E_INVALID_CONF    -7
-#define IPHONE_E_PAIRING_FAILED  -8
-#define IPHONE_E_SSL_ERROR       -9
-#define IPHONE_E_PLIST_ERROR    -10
-#define IPHONE_E_DICT_ERROR     -11
+#define IPHONE_E_INVALID_CONF          -7
+#define IPHONE_E_PAIRING_FAILED        -8
+#define IPHONE_E_SSL_ERROR             -9
+#define IPHONE_E_PLIST_ERROR          -10
+#define IPHONE_E_DICT_ERROR           -11
+#define IPHONE_E_START_SERVICE_FAILED -12
 
 //afc specific error
-#define IPHONE_E_NO_SUCH_FILE   -12
+#define IPHONE_E_NO_SUCH_FILE         -13
 
 typedef int16_t iphone_error_t;
 
diff --git a/src/lockdown.c b/src/lockdown.c
index e720b29..e3636d1 100644
--- a/src/lockdown.c
+++ b/src/lockdown.c
@@ -1021,8 +1021,9 @@ ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_
  *
  * @param control The lockdownd client
  * @param service The name of the service to start
- *
- * @return The port number the service was started on or 0 on failure.
+ * @param port The port number the service was started on
+ 
+ * @return an error code
  */
 iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char *service, int *port)
 {
@@ -1064,6 +1065,8 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char
 	if (!dict)
 		return IPHONE_E_PLIST_ERROR;
 
+	ret = IPHONE_E_UNKNOWN_ERROR;
+
 	plist_t query_node = plist_find_node_by_string(dict, "StartService");
 	plist_t result_key_node = plist_get_next_sibling(query_node);
 	plist_t result_value_node = plist_get_next_sibling(result_key_node);
@@ -1076,9 +1079,7 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char
 	plist_type port_key_type = plist_get_node_type(port_key_node);
 	plist_type port_value_type = plist_get_node_type(port_value_node);
 
-	if (result_key_type == PLIST_KEY && result_value_type == PLIST_STRING && port_key_type == PLIST_KEY
-		&& port_value_type == PLIST_UINT) {
-
+	if (result_key_type == PLIST_KEY && result_value_type == PLIST_STRING) {
 		char *result_key = NULL;
 		char *result_value = NULL;
 		char *port_key = NULL;
@@ -1086,18 +1087,23 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char
 
 		plist_get_key_val(result_key_node, &result_key);
 		plist_get_string_val(result_value_node, &result_value);
-		plist_get_key_val(port_key_node, &port_key);
-		plist_get_uint_val(port_value_node, &port_value);
 
-		if (!strcmp(result_key, "Result") && !strcmp(result_value, "Success") && !strcmp(port_key, "Port")) {
-			port_loc = port_value;
-			ret = IPHONE_E_SUCCESS;
-		}
+		if (!strcmp(result_key, "Result") && !strcmp(result_value, "Failure")) {
+			ret = IPHONE_E_START_SERVICE_FAILED;
+		} else {
+			if (port_key_type == PLIST_KEY && port_value_type == PLIST_UINT) {
+				plist_get_key_val(port_key_node, &port_key);
+				plist_get_uint_val(port_value_node, &port_value);
 
-		if (port && ret == IPHONE_E_SUCCESS)
-			*port = port_loc;
-		else
-			ret = IPHONE_E_UNKNOWN_ERROR;
+				if (!strcmp(result_key, "Result") && !strcmp(result_value, "Success") && !strcmp(port_key, "Port")) {
+					port_loc = port_value;
+					ret = IPHONE_E_SUCCESS;
+				}
+
+				if (port && ret == IPHONE_E_SUCCESS)
+					*port = port_loc;
+			}
+		}
 	}
 
 	plist_free(dict);
-- 
cgit v1.1-32-gdbae