packet.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
  6. * Copyright (C) 2008 Brian Aker, Eric Day
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are
  11. * met:
  12. *
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * * Redistributions in binary form must reproduce the above
  17. * copyright notice, this list of conditions and the following disclaimer
  18. * in the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * * The names of its contributors may not be used to endorse or
  22. * promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. */
  38. /**
  39. * @file
  40. * @brief Server connection definitions
  41. */
  42. #include "gear_config.h"
  43. #include "libgearman-server/common.h"
  44. #include <libgearman/command.h>
  45. #include <libgearman-server/fifo.h>
  46. #include <cassert>
  47. #include <cerrno>
  48. #include <cstring>
  49. #include <memory>
  50. #ifndef __INTEL_COMPILER
  51. #pragma GCC diagnostic ignored "-Wold-style-cast"
  52. #endif
  53. /*
  54. * Public definitions
  55. */
  56. gearman_server_packet_st *
  57. gearman_server_packet_create(gearman_server_thread_st *thread,
  58. bool from_thread)
  59. {
  60. gearman_server_packet_st *server_packet= NULL;
  61. if (from_thread && Server->flags.threaded)
  62. {
  63. if (thread->free_packet_count > 0)
  64. {
  65. server_packet= thread->free_packet_list;
  66. thread->free_packet_list= server_packet->next;
  67. thread->free_packet_count--;
  68. }
  69. }
  70. else
  71. {
  72. if (Server->free_packet_count > 0)
  73. {
  74. server_packet= Server->free_packet_list;
  75. Server->free_packet_list= server_packet->next;
  76. Server->free_packet_count--;
  77. }
  78. }
  79. if (server_packet == NULL)
  80. {
  81. server_packet= new (std::nothrow) gearman_server_packet_st;
  82. if (server_packet == NULL)
  83. {
  84. gearmand_perror(errno, "new() gearman_server_packet_st");
  85. return NULL;
  86. }
  87. }
  88. server_packet->next= NULL;
  89. return server_packet;
  90. }
  91. void destroy_gearman_server_packet_st(gearman_server_packet_st *packet)
  92. {
  93. delete packet;
  94. }
  95. void gearman_server_packet_free(gearman_server_packet_st *packet,
  96. gearman_server_thread_st *thread,
  97. bool from_thread)
  98. {
  99. if (from_thread and Server->flags.threaded)
  100. {
  101. if (thread->free_packet_count < GEARMAN_MAX_FREE_SERVER_PACKET)
  102. {
  103. packet->next= thread->free_packet_list;
  104. thread->free_packet_list= packet;
  105. thread->free_packet_count++;
  106. }
  107. else
  108. {
  109. gearmand_debug("delete() gearman_server_packet_st");
  110. delete packet;
  111. }
  112. }
  113. else
  114. {
  115. if (Server->free_packet_count < GEARMAN_MAX_FREE_SERVER_PACKET)
  116. {
  117. packet->next= Server->free_packet_list;
  118. Server->free_packet_list= packet;
  119. Server->free_packet_count++;
  120. }
  121. else
  122. {
  123. gearmand_debug("delete() gearman_server_packet_st");
  124. delete packet;
  125. }
  126. }
  127. }
  128. gearmand_error_t gearman_server_io_packet_add(gearman_server_con_st *con,
  129. bool take_data,
  130. enum gearman_magic_t magic,
  131. gearman_command_t command,
  132. const void *arg, ...)
  133. {
  134. gearman_server_packet_st *server_packet;
  135. va_list ap;
  136. server_packet= gearman_server_packet_create(con->thread, false);
  137. if (server_packet == NULL)
  138. {
  139. return GEARMAN_MEMORY_ALLOCATION_FAILURE;
  140. }
  141. gearmand_packet_init(&(server_packet->packet), magic, command);
  142. va_start(ap, arg);
  143. while (arg)
  144. {
  145. size_t arg_size= va_arg(ap, size_t);
  146. gearmand_error_t ret= gearmand_packet_create(&(server_packet->packet), arg, arg_size);
  147. if (gearmand_failed(ret))
  148. {
  149. va_end(ap);
  150. gearmand_packet_free(&(server_packet->packet));
  151. gearman_server_packet_free(server_packet, con->thread, false);
  152. return ret;
  153. }
  154. arg= va_arg(ap, void *);
  155. }
  156. va_end(ap);
  157. gearmand_error_t ret= gearmand_packet_pack_header(&(server_packet->packet));
  158. if (gearmand_failed(ret))
  159. {
  160. gearmand_packet_free(&(server_packet->packet));
  161. gearman_server_packet_free(server_packet, con->thread, false);
  162. return ret;
  163. }
  164. if (take_data)
  165. {
  166. server_packet->packet.options.free_data= true;
  167. }
  168. int error;
  169. if ((error= pthread_mutex_lock(&con->thread->lock)) == 0)
  170. {
  171. gearmand_server_con_fifo_add(con, server_packet);
  172. if ((error= pthread_mutex_unlock(&con->thread->lock)))
  173. {
  174. gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock() failed");
  175. }
  176. }
  177. else
  178. {
  179. gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_lock() failed");
  180. }
  181. gearman_server_con_io_add(con);
  182. return GEARMAN_SUCCESS;
  183. }
  184. void gearman_server_io_packet_remove(gearman_server_con_st *con)
  185. {
  186. gearman_server_packet_st *server_packet= con->io_packet_list;
  187. gearmand_packet_free(&(server_packet->packet));
  188. int error;
  189. if ((error= pthread_mutex_lock(&con->thread->lock)) == 0)
  190. {
  191. gearmand_server_con_fifo_free(con, server_packet);
  192. if ((error= pthread_mutex_unlock(&con->thread->lock)))
  193. {
  194. gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock() failed");
  195. }
  196. }
  197. else
  198. {
  199. gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_lock() failed");
  200. }
  201. gearman_server_packet_free(server_packet, con->thread, true);
  202. }
  203. void gearman_server_proc_packet_add(gearman_server_con_st *con,
  204. gearman_server_packet_st *packet)
  205. {
  206. int error;
  207. if ((error= pthread_mutex_lock(&con->thread->lock)) == 0)
  208. {
  209. gearmand_server_con_fifo_proc_add(con, packet);
  210. if ((error= pthread_mutex_unlock(&con->thread->lock)))
  211. {
  212. gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock() failed");
  213. }
  214. }
  215. else
  216. {
  217. gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_lock() failed");
  218. }
  219. gearman_server_con_proc_add(con);
  220. }
  221. gearman_server_packet_st *
  222. gearman_server_proc_packet_remove(gearman_server_con_st *con)
  223. {
  224. gearman_server_packet_st *server_packet= con->proc_packet_list;
  225. if (server_packet == NULL)
  226. {
  227. return NULL;
  228. }
  229. int error;
  230. if ((error= pthread_mutex_lock(&con->thread->lock)) == 0)
  231. {
  232. gearmand_server_con_fifo_proc_free(con, server_packet);
  233. if ((error= pthread_mutex_unlock(&con->thread->lock)) != 0)
  234. {
  235. gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock() failed");
  236. }
  237. }
  238. else
  239. {
  240. gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_lock() failed");
  241. }
  242. return server_packet;
  243. }
  244. const char *gearmand_strcommand(gearmand_packet_st *packet)
  245. {
  246. assert(packet);
  247. return gearman_command_info(packet->command)->name;
  248. }
  249. inline static gearmand_error_t packet_create_arg(gearmand_packet_st *packet,
  250. const void *arg, size_t arg_size)
  251. {
  252. if (packet->argc == gearman_command_info(packet->command)->argc and
  253. (not (gearman_command_info(packet->command)->data) or
  254. packet->data))
  255. {
  256. gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM, "too many arguments for command(%s)", gearman_command_info(packet->command)->name);
  257. return GEARMAN_TOO_MANY_ARGS;
  258. }
  259. if (packet->argc == gearman_command_info(packet->command)->argc)
  260. {
  261. packet->data= static_cast<const char *>(arg);
  262. packet->data_size= arg_size;
  263. return GEARMAN_SUCCESS;
  264. }
  265. if (packet->args_size == 0 and packet->magic != GEARMAN_MAGIC_TEXT)
  266. {
  267. packet->args_size= GEARMAN_PACKET_HEADER_SIZE;
  268. }
  269. if ((packet->args_size + arg_size) < GEARMAN_ARGS_BUFFER_SIZE)
  270. {
  271. packet->args= packet->args_buffer;
  272. }
  273. else
  274. {
  275. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM, "resizing packet buffer");
  276. if (packet->args == packet->args_buffer)
  277. {
  278. packet->args= (char *)malloc(packet->args_size + arg_size);
  279. memcpy(packet->args, packet->args_buffer, packet->args_size);
  280. }
  281. else
  282. {
  283. char *new_args= (char *)realloc(packet->args, packet->args_size + arg_size);
  284. if (new_args == NULL)
  285. {
  286. return gearmand_perror(errno, "realloc");
  287. }
  288. packet->args= new_args;
  289. }
  290. }
  291. memcpy(packet->args + packet->args_size, arg, arg_size);
  292. packet->args_size+= arg_size;
  293. packet->arg_size[packet->argc]= arg_size;
  294. packet->argc++;
  295. size_t offset;
  296. if (packet->magic == GEARMAN_MAGIC_TEXT)
  297. {
  298. offset= 0;
  299. }
  300. else
  301. {
  302. offset= GEARMAN_PACKET_HEADER_SIZE;
  303. }
  304. for (uint8_t x= 0; x < packet->argc; x++)
  305. {
  306. packet->arg[x]= packet->args + offset;
  307. offset+= packet->arg_size[x];
  308. }
  309. return GEARMAN_SUCCESS;
  310. }
  311. /** @} */
  312. /*
  313. * Public Definitions
  314. */
  315. void gearmand_packet_init(gearmand_packet_st *packet, enum gearman_magic_t magic, gearman_command_t command)
  316. {
  317. assert(packet);
  318. packet->options.complete= false;
  319. packet->options.free_data= false;
  320. packet->magic= magic;
  321. packet->command= command;
  322. packet->argc= 0;
  323. packet->args_size= 0;
  324. packet->data_size= 0;
  325. packet->args= NULL;
  326. packet->data= NULL;
  327. }
  328. gearmand_error_t gearmand_packet_create(gearmand_packet_st *packet,
  329. const void *arg, size_t arg_size)
  330. {
  331. return packet_create_arg(packet, arg, arg_size);
  332. }
  333. void gearmand_packet_free(gearmand_packet_st *packet)
  334. {
  335. if (packet->args != packet->args_buffer && packet->args != NULL)
  336. {
  337. gearmand_debug("free packet's args");
  338. free(packet->args);
  339. packet->args= NULL;
  340. }
  341. if (packet->options.free_data && packet->data != NULL)
  342. {
  343. gearmand_debug("free() packet's data");
  344. free((void *)packet->data); //@todo fix the need for the casting.
  345. packet->data= NULL;
  346. }
  347. }
  348. gearmand_error_t gearmand_packet_pack_header(gearmand_packet_st *packet)
  349. {
  350. if (packet->magic == GEARMAN_MAGIC_TEXT)
  351. {
  352. packet->options.complete= true;
  353. return GEARMAN_SUCCESS;
  354. }
  355. if (packet->args_size == 0)
  356. {
  357. packet->args= packet->args_buffer;
  358. packet->args_size= GEARMAN_PACKET_HEADER_SIZE;
  359. }
  360. switch (packet->magic)
  361. {
  362. case GEARMAN_MAGIC_TEXT:
  363. break;
  364. case GEARMAN_MAGIC_REQUEST:
  365. memcpy(packet->args, "\0REQ", 4);
  366. break;
  367. case GEARMAN_MAGIC_RESPONSE:
  368. memcpy(packet->args, "\0RES", 4);
  369. break;
  370. default:
  371. gearmand_error("invalid magic value");
  372. return GEARMAN_INVALID_MAGIC;
  373. }
  374. if (packet->command == GEARMAN_COMMAND_TEXT ||
  375. packet->command >= GEARMAN_COMMAND_MAX)
  376. {
  377. gearmand_error("invalid command value");
  378. return GEARMAN_INVALID_COMMAND;
  379. }
  380. uint32_t tmp= packet->command;
  381. tmp= htonl(tmp);
  382. memcpy(packet->args + 4, &tmp, 4);
  383. uint64_t length_64= packet->args_size + packet->data_size - GEARMAN_PACKET_HEADER_SIZE;
  384. // Check for overflow on 32bit(portable?).
  385. if (length_64 >= UINT32_MAX || length_64 < packet->data_size)
  386. {
  387. gearmand_error("data size too too long");
  388. return GEARMAN_ARGUMENT_TOO_LARGE;
  389. }
  390. tmp= (uint32_t)length_64;
  391. tmp= htonl(tmp);
  392. memcpy(packet->args + 8, &tmp, 4);
  393. packet->options.complete= true;
  394. return GEARMAN_SUCCESS;
  395. }