packet.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011-2013 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 <cassert>
  46. #include <cerrno>
  47. #include <cstring>
  48. #include <memory>
  49. #pragma GCC diagnostic push
  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 and 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 < GEARMAND_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 < GEARMAND_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 GEARMAND_MEMORY_ALLOCATION_FAILURE;
  140. }
  141. server_packet->packet.reset(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_FIFO__ADD(con->io_packet, 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");
  175. }
  176. }
  177. else
  178. {
  179. gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_lock");
  180. }
  181. gearman_server_con_io_add(con);
  182. return GEARMAND_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_FIFO__DEL(con->io_packet, 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");
  195. }
  196. }
  197. else
  198. {
  199. gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_lock");
  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_FIFO__ADD(con->proc_packet, packet);
  210. if ((error= pthread_mutex_unlock(&con->thread->lock)))
  211. {
  212. gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock");
  213. }
  214. }
  215. else
  216. {
  217. gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_lock");
  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)
  226. {
  227. int error;
  228. if ((error= pthread_mutex_lock(&con->thread->lock)) == 0)
  229. {
  230. GEARMAND_FIFO__DEL(con->proc_packet, server_packet);
  231. if ((error= pthread_mutex_unlock(&con->thread->lock)) != 0)
  232. {
  233. gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock");
  234. }
  235. }
  236. else
  237. {
  238. gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_lock");
  239. }
  240. }
  241. return server_packet;
  242. }
  243. const char *gearmand_strcommand(gearmand_packet_st *packet)
  244. {
  245. assert(packet);
  246. return gearman_command_info(packet->command)->name;
  247. }
  248. inline static gearmand_error_t packet_create_arg(gearmand_packet_st *packet,
  249. const void *arg, size_t arg_size)
  250. {
  251. if (packet->argc == gearman_command_info(packet->command)->argc and
  252. (not (gearman_command_info(packet->command)->data) or
  253. packet->data))
  254. {
  255. gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM, "too many arguments for command(%s)", gearman_command_info(packet->command)->name);
  256. return GEARMAND_TOO_MANY_ARGS;
  257. }
  258. if (packet->argc == gearman_command_info(packet->command)->argc)
  259. {
  260. packet->data= static_cast<const char *>(arg);
  261. packet->data_size= arg_size;
  262. return GEARMAND_SUCCESS;
  263. }
  264. if (packet->args_size == 0 and packet->magic != GEARMAN_MAGIC_TEXT)
  265. {
  266. packet->args_size= GEARMAND_PACKET_HEADER_SIZE;
  267. }
  268. if ((packet->args_size + arg_size) < GEARMAND_ARGS_BUFFER_SIZE)
  269. {
  270. packet->args= packet->args_buffer;
  271. }
  272. else
  273. {
  274. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM, "resizing packet buffer");
  275. if (packet->args == packet->args_buffer)
  276. {
  277. packet->args= (char *)realloc(NULL, packet->args_size + arg_size);
  278. memcpy(packet->args, packet->args_buffer, packet->args_size);
  279. }
  280. else
  281. {
  282. char *new_args= (char *)realloc(packet->args, packet->args_size + arg_size);
  283. if (new_args == NULL)
  284. {
  285. return gearmand_perror(errno, "realloc");
  286. }
  287. packet->args= new_args;
  288. }
  289. }
  290. memcpy(packet->args + packet->args_size, arg, arg_size);
  291. packet->args_size+= arg_size;
  292. packet->arg_size[packet->argc]= arg_size;
  293. packet->argc++;
  294. size_t offset;
  295. if (packet->magic == GEARMAN_MAGIC_TEXT)
  296. {
  297. offset= 0;
  298. }
  299. else
  300. {
  301. offset= GEARMAND_PACKET_HEADER_SIZE;
  302. }
  303. for (uint8_t x= 0; x < packet->argc; ++x)
  304. {
  305. packet->arg[x]= packet->args + offset;
  306. offset+= packet->arg_size[x];
  307. }
  308. return GEARMAND_SUCCESS;
  309. }
  310. /** @} */
  311. /*
  312. * Public Definitions
  313. */
  314. void gearmand_packet_st::reset(enum gearman_magic_t magic_, gearman_command_t command_)
  315. {
  316. options.complete= false;
  317. options.free_data= false;
  318. magic= magic_;
  319. command= command_;
  320. argc= 0;
  321. args_size= 0;
  322. data_size= 0;
  323. args= NULL;
  324. data= NULL;
  325. }
  326. gearmand_error_t gearmand_packet_create(gearmand_packet_st *packet,
  327. const void *arg, size_t arg_size)
  328. {
  329. return packet_create_arg(packet, arg, arg_size);
  330. }
  331. void gearmand_packet_free(gearmand_packet_st *packet)
  332. {
  333. if (packet->args != packet->args_buffer && packet->args != NULL)
  334. {
  335. gearmand_debug("free packet's args");
  336. free(packet->args);
  337. packet->args= NULL;
  338. }
  339. if (packet->options.free_data && packet->data != NULL)
  340. {
  341. gearmand_debug("free() packet's data");
  342. free((void *)packet->data); //@todo fix the need for the casting.
  343. packet->data= NULL;
  344. }
  345. }
  346. gearmand_error_t gearmand_packet_pack_header(gearmand_packet_st *packet)
  347. {
  348. if (packet->magic == GEARMAN_MAGIC_TEXT)
  349. {
  350. packet->options.complete= true;
  351. return GEARMAND_SUCCESS;
  352. }
  353. if (packet->args_size == 0)
  354. {
  355. packet->args= packet->args_buffer;
  356. packet->args_size= GEARMAND_PACKET_HEADER_SIZE;
  357. }
  358. switch (packet->magic)
  359. {
  360. case GEARMAN_MAGIC_TEXT:
  361. break;
  362. case GEARMAN_MAGIC_REQUEST:
  363. memcpy(packet->args, "\0REQ", 4);
  364. break;
  365. case GEARMAN_MAGIC_RESPONSE:
  366. memcpy(packet->args, "\0RES", 4);
  367. break;
  368. default:
  369. gearmand_error("invalid magic value");
  370. return GEARMAND_INVALID_MAGIC;
  371. }
  372. if (packet->command == GEARMAN_COMMAND_TEXT ||
  373. packet->command >= GEARMAN_COMMAND_MAX)
  374. {
  375. gearmand_error("invalid command value");
  376. return GEARMAND_INVALID_COMMAND;
  377. }
  378. uint32_t tmp= packet->command;
  379. tmp= htonl(tmp);
  380. memcpy(packet->args + 4, &tmp, 4);
  381. uint64_t length_64= packet->args_size + packet->data_size - GEARMAND_PACKET_HEADER_SIZE;
  382. // Check for overflow on 32bit(portable?).
  383. if (length_64 >= UINT32_MAX || length_64 < packet->data_size)
  384. {
  385. gearmand_error("data size too too long");
  386. return GEARMAND_ARGUMENT_TOO_LARGE;
  387. }
  388. tmp= (uint32_t)length_64;
  389. tmp= htonl(tmp);
  390. memcpy(packet->args + 8, &tmp, 4);
  391. packet->options.complete= true;
  392. return GEARMAND_SUCCESS;
  393. }
  394. #pragma GCC diagnostic pop