X-Git-Url: http://git.tuebingen.mpg.de/?a=blobdiff_plain;f=afs.c;h=d41a518d791f9908b0230beda99849f0a8f4c3e9;hb=19c44d502e169d4801449861e7ef0252e3b1dc5b;hp=05c2c21367e93f851cf4ac6d89deece65ac23d06;hpb=f2f556689c3061959257a59690b1cfc3adaf7625;p=paraslash.git diff --git a/afs.c b/afs.c index 05c2c213..d41a518d 100644 --- a/afs.c +++ b/afs.c @@ -157,11 +157,11 @@ struct callback_result { * \sa send_option_arg_callback_request(), send_standard_callback_request(). */ int send_callback_request(callback_function *f, struct osl_object *query, - struct osl_object *result) + callback_result_handler *result_handler, + void *private_result_data) { struct callback_query *cq; - struct callback_result *cr; - int ret, fd = -1, query_shmid, result_shmid; + int num_results = 0, ret, fd = -1, query_shmid, result_shmid; void *query_shm, *result_shm; char buf[sizeof(afs_socket_cookie) + sizeof(int)]; size_t query_shm_size = sizeof(*cq); @@ -195,37 +195,45 @@ int send_callback_request(callback_function *f, struct osl_object *query, ret = send_bin_buffer(fd, buf, sizeof(buf)); if (ret < 0) goto out; - ret = recv_bin_buffer(fd, buf, sizeof(buf)); - if (ret < 0) - goto out; - if (ret != sizeof(int)) { - ret = -E_AFS_SHORT_READ; - goto out; - } - ret = *(int *) buf; - if (ret <= 0) - goto out; - result_shmid = ret; - ret = shm_attach(result_shmid, ATTACH_RO, &result_shm); - if (ret >= 0) { - assert(result); - cr = result_shm; - result->size = cr->result_size; - result->data = para_malloc(result->size); - memcpy(result->data, result_shm + sizeof(*cr), result->size); - ret = shm_detach(result_shm); + for (;;) { + ret = recv_bin_buffer(fd, buf, sizeof(int)); + if (ret <= 0) + goto out; + if (ret != sizeof(int)) { + ret = -E_AFS_SHORT_READ; + goto out; + } + ret = *(int *) buf; + if (ret <= 0) + goto out; + result_shmid = ret; + ret = shm_attach(result_shmid, ATTACH_RO, &result_shm); + if (ret >= 0) { + struct callback_result *cr = result_shm; + struct osl_object result; + num_results++; + result.size = cr->result_size; + result.data = result_shm + sizeof(*cr); + if (result.size) { + assert(result_handler); + ret = result_handler(&result, private_result_data); + if (shm_detach(result_shm) < 0) + PARA_ERROR_LOG("can not detach result\n"); + } + } else + PARA_ERROR_LOG("attach result failed: %d\n", ret); + if (shm_destroy(result_shmid) < 0) + PARA_ERROR_LOG("destroy result failed\n"); if (ret < 0) - PARA_ERROR_LOG("can not detach result\n"); - } else - PARA_ERROR_LOG("attach result failed: %d\n", ret); - if (shm_destroy(result_shmid) < 0) - PARA_ERROR_LOG("destroy result failed\n"); - ret = 1; + break; + } out: if (shm_destroy(query_shmid) < 0) PARA_ERROR_LOG("%s\n", "shm destroy error"); if (fd >= 0) close(fd); + if (ret >= 0) + ret = num_results; // PARA_DEBUG_LOG("callback_ret: %d\n", ret); return ret; } @@ -248,7 +256,8 @@ out: */ int send_option_arg_callback_request(struct osl_object *options, int argc, char * const * const argv, callback_function *f, - struct osl_object *result) + callback_result_handler *result_handler, + void *private_result_data) { char *p; int i, ret; @@ -266,7 +275,8 @@ int send_option_arg_callback_request(struct osl_object *options, strcpy(p, argv[i]); /* OK */ p += strlen(argv[i]) + 1; } - ret = send_callback_request(f, &query, result); + ret = send_callback_request(f, &query, result_handler, + private_result_data); free(query.data); return ret; } @@ -286,9 +296,11 @@ int send_option_arg_callback_request(struct osl_object *options, * send_option_arg_callback_request(). */ int send_standard_callback_request(int argc, char * const * const argv, - callback_function *f, struct osl_object *result) + callback_function *f, callback_result_handler *result_handler, + void *private_result_data) { - return send_option_arg_callback_request(NULL, argc, argv, f, result); + return send_option_arg_callback_request(NULL, argc, argv, f, result_handler, + private_result_data); } static int action_if_pattern_matches(struct osl_row *row, void *data) @@ -407,7 +419,8 @@ static int fd2buf(int fd, unsigned max_size, struct osl_object *obj) * send_callback_request() otherwise. */ int stdin_command(int fd, struct osl_object *arg_obj, callback_function *f, - unsigned max_len, struct osl_object *result) + unsigned max_len, callback_result_handler *result_handler, + void *private_result_data) { struct osl_object query, stdin_obj; int ret; @@ -423,7 +436,7 @@ int stdin_command(int fd, struct osl_object *arg_obj, callback_function *f, memcpy(query.data, arg_obj->data, arg_obj->size); memcpy((char *)query.data + arg_obj->size, stdin_obj.data, stdin_obj.size); free(stdin_obj.data); - ret = send_callback_request(f, &query, result); + ret = send_callback_request(f, &query, result_handler, private_result_data); free(query.data); return ret; } @@ -588,26 +601,28 @@ static int com_select_callback(const struct osl_object *query, para_printf(&pb, "activated %s (%d admissible files)\n", current_mop? current_mop : "dummy mood", num_admissible); result->data = pb.buf; - result->size = pb.size; + result->size = pb.offset; return 1; } +int send_result(struct osl_object *result, void *private_result_data) +{ + int fd = *(int *)private_result_data; + if (!result->size) + return 1; + return send_bin_buffer(fd, result->data, result->size); +} + int com_select(int fd, int argc, char * const * const argv) { - int ret; - struct osl_object query, result; + struct osl_object query; if (argc != 2) return -E_AFS_SYNTAX; query.data = argv[1]; query.size = strlen(argv[1]) + 1; - ret = send_callback_request(com_select_callback, &query, - &result); - if (ret > 0 && result.data && result.size) { - ret = send_va_buffer(fd, "%s", (char *)result.data); - free(result.data); - } - return ret; + return send_callback_request(com_select_callback, &query, + &send_result, &fd); } static void init_admissible_files(char *arg) @@ -783,18 +798,47 @@ static void command_pre_select(struct sched *s, struct task *t) t->ret = 1; } +int pass_object_as_shm(int fd, struct osl_object *obj) +{ + int ret, shmid; + void *shm; + struct callback_result *cr; + + if (!obj->data || !obj->size) + return 0; + ret = shm_new(obj->size + sizeof(struct callback_result)); + if (ret < 0) + return ret; + shmid = ret; + ret = shm_attach(shmid, ATTACH_RW, &shm); + if (ret < 0) + goto err; + cr = shm; + cr->result_size = obj->size; + memcpy(shm + sizeof(*cr), obj->data, obj->size); + ret = shm_detach(shm); + if (ret < 0) + goto err; + ret = send_bin_buffer(fd, (char *)&shmid, sizeof(int)); + if (ret >= 0) + return ret; +err: + if (shm_destroy(shmid) < 0) + PARA_ERROR_LOG("destroy result failed\n"); + return ret; +} + /* * On errors, negative value is written to fd. * On success: If query produced a result, the result_shmid is written to fd. * Otherwise, zero is written. */ -static int call_callback(int fd, int query_shmid) +static void call_callback(int fd, int query_shmid) { - void *query_shm, *result_shm; + void *query_shm; struct callback_query *cq; - struct callback_result *cr; struct osl_object query, result = {.data = NULL}; - int result_shmid = -1, ret, ret2; + int ret; ret = shm_attach(query_shmid, ATTACH_RW, &query_shm); if (ret < 0) @@ -803,39 +847,13 @@ static int call_callback(int fd, int query_shmid) query.data = (char *)query_shm + sizeof(*cq); query.size = cq->query_size; ret = cq->handler(&query, &result); - ret2 = shm_detach(query_shm); - if (ret2 < 0 && ret >= 0) - ret = ret2; if (ret < 0) goto out; - ret = 0; - if (!result.data || !result.size) - goto out; - ret = shm_new(result.size + sizeof(struct callback_result)); - if (ret < 0) - goto out; - result_shmid = ret; - ret = shm_attach(result_shmid, ATTACH_RW, &result_shm); - if (ret < 0) - goto out; - cr = result_shm; - cr->result_size = result.size; - memcpy(result_shm + sizeof(*cr), result.data, result.size); - ret = shm_detach(result_shm); - if (ret < 0) - goto out; - ret = result_shmid; + ret = pass_object_as_shm(fd, &result); out: free(result.data); - ret2 = send_bin_buffer(fd, (char *)&ret, sizeof(int)); - if (ret < 0 || ret2 < 0) { - if (result_shmid >= 0) - if (shm_destroy(result_shmid) < 0) - PARA_ERROR_LOG("destroy result failed\n"); - if (ret >= 0) - ret = ret2; - } - return ret; + if (ret < 0) + PARA_ERROR_LOG("%s\n", para_strerror(-ret)); } static void execute_server_command(void) @@ -890,7 +908,6 @@ static void execute_afs_command(int fd, uint32_t expected_cookie) query_shmid); return; } - /* Ignore return value: Errors might be OK here. */ call_callback(fd, query_shmid); } @@ -1047,7 +1064,7 @@ int com_init(int fd, int argc, char * const * const argv) return -E_BAD_TABLE_NAME; } } - ret = send_callback_request(create_tables_callback, &query, NULL); + ret = send_callback_request(create_tables_callback, &query, NULL, NULL); if (ret < 0) return ret; return send_va_buffer(fd, "successfully created afs table(s)\n"); @@ -1071,7 +1088,6 @@ int com_check(int fd, int argc, char * const * const argv) { unsigned flags = 0; int i, ret; - struct osl_object result; for (i = 1; i < argc; i++) { const char *arg = argv[i]; @@ -1100,37 +1116,19 @@ int com_check(int fd, int argc, char * const * const argv) if (!flags) flags = ~0U; if (flags & CHECK_AFT) { - ret = send_callback_request(aft_check_callback, NULL, &result); + ret = send_callback_request(aft_check_callback, NULL, send_result, &fd); if (ret < 0) return ret; - if (ret > 0) { - ret = send_buffer(fd, (char *) result.data); - free(result.data); - if (ret < 0) - return ret; - } } if (flags & CHECK_PLAYLISTS) { - ret = send_callback_request(playlist_check_callback, NULL, &result); + ret = send_callback_request(playlist_check_callback, NULL, send_result, &fd); if (ret < 0) return ret; - if (ret > 0) { - ret = send_buffer(fd, (char *) result.data); - free(result.data); - if (ret < 0) - return ret; - } } if (flags & CHECK_MOODS) { - ret = send_callback_request(mood_check_callback, NULL, &result); + ret = send_callback_request(mood_check_callback, NULL, send_result, &fd); if (ret < 0) return ret; - if (ret > 0) { - ret = send_buffer(fd, (char *) result.data); - free(result.data); - if (ret < 0) - return ret; - } } return 1; }