From 587990158fe0a7ee9a8ee086d83d1d61d61cc56b Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Sun, 14 Sep 2008 22:23:01 +0200 Subject: Change lock to mutex and add tool to check AFC in multithreaded env. --- src/AFC.c | 17 ++++--- src/AFC.h | 2 + src/Makefile.am | 12 +++-- src/afccheck.c | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 159 insertions(+), 8 deletions(-) create mode 100644 src/afccheck.c diff --git a/src/AFC.c b/src/AFC.c index 4d6b269..144efe1 100644 --- a/src/AFC.c +++ b/src/AFC.c @@ -34,10 +34,11 @@ const int MAXIMUM_PACKET_SIZE = (2 << 15) - 32; static void afc_lock(iphone_afc_client_t client) { log_debug_msg("Locked\n"); - while (client->lock) { - usleep(500); // they say it's obsolete, but whatever - } - client->lock = 1; + /*while (client->lock) { + usleep(500); // they say it's obsolete, but whatever + } + client->lock = 1; */ + g_mutex_lock(client->mutex); } /** Unlocks an AFC client, done for thread safety stuff. @@ -47,7 +48,8 @@ static void afc_lock(iphone_afc_client_t client) static void afc_unlock(iphone_afc_client_t client) { // just to be pretty log_debug_msg("Unlocked\n"); - client->lock = 0; + //client->lock = 0; + g_mutex_unlock(client->mutex); } /** Makes a connection to the AFC service on the phone. @@ -61,6 +63,10 @@ static void afc_unlock(iphone_afc_client_t client) iphone_error_t iphone_afc_new_client(iphone_device_t device, int src_port, int dst_port, iphone_afc_client_t * client) { int ret = IPHONE_E_SUCCESS; + + //makes sure thread environment is available + if (!g_thread_supported()) + g_thread_init(NULL); iphone_afc_client_t client_loc = (iphone_afc_client_t) malloc(sizeof(struct iphone_afc_client_int)); if (!device) @@ -92,6 +98,7 @@ iphone_error_t iphone_afc_new_client(iphone_device_t device, int src_port, int d client_loc->afc_packet->header2 = 0x4141504C; client_loc->file_handle = 0; client_loc->lock = 0; + client_loc->mutex = g_mutex_new(); *client = client_loc; return IPHONE_E_SUCCESS; diff --git a/src/AFC.h b/src/AFC.h index f05d831..463c13e 100644 --- a/src/AFC.h +++ b/src/AFC.h @@ -26,6 +26,7 @@ #include #include #include +#include typedef struct { uint32 header1, header2; @@ -46,6 +47,7 @@ struct iphone_afc_client_int { AFCPacket *afc_packet; int file_handle; int lock; + GMutex *mutex; }; struct iphone_afc_file_int { diff --git a/src/Makefile.am b/src/Makefile.am index a8f7cbf..24fc4aa 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,12 +1,12 @@ INCLUDES = -I$(top_srcdir)/include -AM_CFLAGS = $(libxml2_CFLAGS) $(libusb_CFLAGS) $(libglib2_CFLAGS) $(libgnutls_CFLAGS) $(libtasn1_CFLAGS) -g -AM_LDFLAGS = $(libxml2_LIBS) $(libusb_LIBS) $(libglib2_LIBS) $(libgnutls_LIBS) $(libtasn1_LIBS) +AM_CFLAGS = $(libxml2_CFLAGS) $(libusb_CFLAGS) $(libglib2_CFLAGS) $(libgnutls_CFLAGS) $(libtasn1_CFLAGS) $(libgthread2_CFLAGS) -g +AM_LDFLAGS = $(libxml2_LIBS) $(libusb_LIBS) $(libglib2_LIBS) $(libgnutls_LIBS) $(libtasn1_LIBS) $(libgthread2_LIBS) bin_PROGRAMS = libiphone-initconf if BUILD_DEV_TOOLS - bin_PROGRAMS = iphoneclient libiphone-initconf lckd-client + bin_PROGRAMS = iphoneclient libiphone-initconf lckd-client afccheck iphoneclient_SOURCES = main.c iphoneclient_LDADD = libiphone.la @@ -14,11 +14,17 @@ if BUILD_DEV_TOOLS lckd_client_CFLAGS = $(AM_CFLAGS) lckd_client_LDFLAGS = -lreadline $(AM_LDFLAGS) lckd_client_LDADD = libiphone.la + + afccheck_SOURCES = afccheck.c + afccheck_CFLAGS = $(AM_CFLAGS) + afccheck_LDFLAGS = $(AM_LDFLAGS) + afccheck_LDADD = libiphone.la endif libiphone_initconf_SOURCES = initconf.c userpref.c lockdown.c plist.c usbmux.c iphone.c utils.c libiphone_initconf_CFLAGS = $(libgthread2_CFLAGS) $(AM_CFLAGS) libiphone_initconf_LDFLAGS = $(libgthread2_LIBS) $(AM_LDFLAGS) + lib_LTLIBRARIES = libiphone.la libiphone_la_SOURCES = usbmux.c iphone.c plist.c lockdown.c AFC.c userpref.c utils.c diff --git a/src/afccheck.c b/src/afccheck.c new file mode 100644 index 0000000..e772d99 --- /dev/null +++ b/src/afccheck.c @@ -0,0 +1,136 @@ +/* + * afccheck.c + * creates threads and check communication through AFC is done rigth + * + * Copyright (c) 2008 Jonathan Beck All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include + +#include "usbmux.h" +#include "iphone.h" +#include + +#define BUFFER_SIZE 20000 +#define NB_THREADS 10 + +int debug = 0; + +typedef struct { + iphone_afc_client_t afc; + int id; +} param; + + +void check_afc(gpointer data) +{ + //prepare a buffer + int buffersize = BUFFER_SIZE * sizeof(int); + int *buf = (int *) malloc(buffersize); + int *buf2 = (int *) malloc(buffersize); + int bytes = 0; + //fill buffer + int i = 0; + for (i = 0; i < BUFFER_SIZE; i++) { + buf[i] = ((param *) data)->id * i; + } + + //now writes buffer on iphone + iphone_afc_file_t file = NULL; + char path[50]; + sprintf(path, "/Buf%i", ((param *) data)->id); + iphone_afc_open_file(((param *) data)->afc, path, IPHONE_AFC_FILE_WRITE, &file); + iphone_afc_write_file(((param *) data)->afc, file, (char *) buf, buffersize, &bytes); + iphone_afc_close_file(((param *) data)->afc, file); + file = NULL; + if (bytes != buffersize) + printf("Write operation failed\n"); + + //now read it + bytes = 0; + iphone_afc_open_file(((param *) data)->afc, path, IPHONE_AFC_FILE_READ, &file); + iphone_afc_read_file(((param *) data)->afc, file, (char *) buf2, buffersize, &bytes); + iphone_afc_close_file(((param *) data)->afc, file); + if (bytes != buffersize) + printf("Read operation failed\n"); + + //compare buffers + for (i = 0; i < BUFFER_SIZE; i++) { + if (buf[i] != buf2[i]) { + printf("Buffers are differents, stream corrupted\n"); + break; + } + } + + //cleanup + iphone_afc_delete_file(((param *) data)->afc, path); + g_thread_exit(0); +} + +int main(int argc, char *argv[]) +{ + iphone_lckd_client_t control = NULL; + iphone_device_t phone = NULL; + GError *err; + int port = 0; + iphone_afc_client_t afc = NULL; + + if (IPHONE_E_SUCCESS != iphone_get_device(&phone)) { + printf("No iPhone found, is it plugged in?\n"); + return 1; + } + + if (IPHONE_E_SUCCESS != iphone_lckd_new_client(phone, &control)) { + iphone_free_device(phone); + return 1; + } + + if (IPHONE_E_SUCCESS == iphone_lckd_start_service(control, "com.apple.afc", &port) && !port) { + iphone_lckd_free_client(control); + iphone_free_device(phone); + fprintf(stderr, "Something went wrong when starting AFC."); + return 1; + } + + iphone_afc_new_client(phone, 3432, port, &afc); + + //makes sure thread environment is available + if (!g_thread_supported()) + g_thread_init(NULL); + + GThread *threads[NB_THREADS]; + param data[NB_THREADS]; + + int i = 0; + for (i = 0; i < NB_THREADS; i++) { + data[i].afc = afc; + data[i].id = i + 1; + threads[i] = g_thread_create((GThreadFunc) check_afc, data + i, TRUE, &err); + } + + for (i = 0; i < NB_THREADS; i++) { + g_thread_join(threads[i]); + } + + + iphone_lckd_free_client(control); + iphone_free_device(phone); + + return 0; +} -- cgit v1.1-32-gdbae