packet.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. delete packet;
  110. }
  111. }
  112. else
  113. {
  114. if (Server->free_packet_count < GEARMAND_MAX_FREE_SERVER_PACKET)
  115. {
  116. packet->next= Server->free_packet_list;
  117. Server->free_packet_list= packet;
  118. Server->free_packet_count++;
  119. }
  120. else
  121. {
  122. delete packet;
  123. }
  124. }
  125. }
  126. gearmand_error_t gearman_server_io_packet_add(gearman_server_con_st *con,
  127. bool take_data,
  128. enum gearman_magic_t magic,
  129. gearman_command_t command,
  130. const void *arg, ...)
  131. {
  132. gearman_server_packet_st *server_packet;
  133. va_list ap;
  134. server_packet= gearman_server_packet_create(con->thread, false);
  135. if (server_packet == NULL)
  136. {
  137. return GEARMAND_MEMORY_ALLOCATION_FAILURE;
  138. }
  139. server_packet->packet.reset(magic, command);
  140. va_start(ap, arg);
  141. while (arg)
  142. {
  143. size_t arg_size= va_arg(ap, size_t);
  144. gearmand_error_t ret= gearmand_packet_create(&(server_packet->packet), arg, arg_size);
  145. if (gearmand_failed(ret))
  146. {
  147. va_end(ap);
  148. gearmand_packet_free(&(server_packet->packet));
  149. gearman_server_packet_free(server_packet, con->thread, false);
  150. return ret;
  151. }
  152. arg= va_arg(ap, void *);
  153. }
  154. va_end(ap);
  155. gearmand_error_t ret= gearmand_packet_pack_header(&(server_packet->packet));
  156. if (gearmand_failed(ret))
  157. {
  158. gearmand_packet_free(&(server_packet->packet));
  159. gearman_server_packet_free(server_packet, con->thread, false);
  160. return ret;
  161. }
  162. if (take_data)
  163. {
  164. server_packet->packet.options.free_data= true;
  165. }
  166. int error;
  167. if ((error= pthread_mutex_lock(&con->thread->lock)) == 0)
  168. {
  169. GEARMAND_FIFO__ADD(con->io_packet, server_packet);
  170. if ((error= pthread_mutex_unlock(&con->thread->lock)))
  171. {
  172. gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock");
  173. }
  174. }
  175. else
  176. {
  177. gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_lock");
  178. }
  179. gearman_server_con_io_add(con);
  180. return GEARMAND_SUCCESS;
  181. }
  182. void gearman_server_io_packet_remove(gearman_server_con_st *con)
  183. {
  184. gearman_server_packet_st *server_packet= con->io_packet_list;
  185. gearmand_packet_free(&(server_packet->packet));
  186. int error;
  187. if ((error= pthread_mutex_lock(&con->thread->lock)) == 0)
  188. {
  189. GEARMAND_FIFO__DEL(con->io_packet, server_packet);
  190. if ((error= pthread_mutex_unlock(&con->thread->lock)))
  191. {
  192. gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock");
  193. }
  194. }
  195. else
  196. {
  197. gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_lock");
  198. }
  199. gearman_server_packet_free(server_packet, con->thread, true);
  200. }
  201. void gearman_server_proc_packet_add(gearman_server_con_st *con,
  202. gearman_server_packet_st *packet)
  203. {
  204. int error;
  205. if ((error= pthread_mutex_lock(&con->thread->lock)) == 0)
  206. {
  207. GEARMAND_FIFO__ADD(con->proc_packet, packet);
  208. if ((error= pthread_mutex_unlock(&con->thread->lock)))
  209. {
  210. gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock");
  211. }
  212. }
  213. else
  214. {
  215. gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_lock");
  216. }
  217. gearman_server_con_proc_add(con);
  218. }
  219. gearman_server_packet_st *
  220. gearman_server_proc_packet_remove(gearman_server_con_st *con)
  221. {
  222. gearman_server_packet_st *server_packet= con->proc_packet_list;
  223. if (server_packet)
  224. {
  225. int error;
  226. if ((error= pthread_mutex_lock(&con->thread->lock)) == 0)
  227. {
  228. GEARMAND_FIFO__DEL(con->proc_packet, server_packet);
  229. if ((error= pthread_mutex_unlock(&con->thread->lock)) != 0)
  230. {
  231. gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock");
  232. }
  233. }
  234. else
  235. {
  236. gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_lock");
  237. }
  238. }
  239. return server_packet;
  240. }
  241. const char *gearmand_strcommand(gearmand_packet_st *packet)
  242. {
  243. assert(packet);
  244. if (packet)
  245. {
  246. const gearman_command_info_st* info= gearman_command_info(packet->command);
  247. if (info)
  248. {
  249. return info->name;
  250. }
  251. }
  252. return "__INVALID_PACKET_COMMAND";
  253. }
  254. inline static gearmand_error_t packet_create_arg(gearmand_packet_st *packet,
  255. const void *arg, size_t arg_size)
  256. {
  257. if (packet->argc == gearman_command_info(packet->command)->argc and
  258. (not (gearman_command_info(packet->command)->data) or
  259. packet->data))
  260. {
  261. gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM, "too many arguments for command(%s)", gearman_command_info(packet->command)->name);
  262. return GEARMAND_TOO_MANY_ARGS;
  263. }
  264. if (packet->argc == gearman_command_info(packet->command)->argc)
  265. {
  266. packet->data= static_cast<const char *>(arg);
  267. packet->data_size= arg_size;
  268. return GEARMAND_SUCCESS;
  269. }
  270. if (packet->args_size == 0 and packet->magic != GEARMAN_MAGIC_TEXT)
  271. {
  272. packet->args_size= GEARMAND_PACKET_HEADER_SIZE;
  273. }
  274. if ((packet->args_size + arg_size) < GEARMAND_ARGS_BUFFER_SIZE)
  275. {
  276. packet->args= packet->args_buffer;
  277. }
  278. else
  279. {
  280. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM, "resizing packet buffer");
  281. if (packet->args == packet->args_buffer)
  282. {
  283. packet->args= (char *)realloc(NULL, packet->args_size + arg_size);
  284. memcpy(packet->args, packet->args_buffer, packet->args_size);
  285. }
  286. else
  287. {
  288. char *new_args= (char *)realloc(packet->args, packet->args_size + arg_size);
  289. if (new_args == NULL)
  290. {
  291. return gearmand_perror(errno, "realloc");
  292. }
  293. packet->args= new_args;
  294. }
  295. }
  296. memcpy(packet->args + packet->args_size, arg, arg_size);
  297. packet->args_size+= arg_size;
  298. packet->arg_size[packet->argc]= arg_size;
  299. packet->argc++;
  300. size_t offset;
  301. if (packet->magic == GEARMAN_MAGIC_TEXT)
  302. {
  303. offset= 0;
  304. }
  305. else
  306. {
  307. offset= GEARMAND_PACKET_HEADER_SIZE;
  308. }
  309. for (uint8_t x= 0; x < packet->argc; ++x)
  310. {
  311. packet->arg[x]= packet->args + offset;
  312. offset+= packet->arg_size[x];
  313. }
  314. return GEARMAND_SUCCESS;
  315. }
  316. /** @} */
  317. /*
  318. * Public Definitions
  319. */
  320. void gearmand_packet_st::reset(enum gearman_magic_t magic_, gearman_command_t command_)
  321. {
  322. options.complete= false;
  323. options.free_data= false;
  324. magic= magic_;
  325. command= command_;
  326. argc= 0;
  327. args_size= 0;
  328. data_size= 0;
  329. args= NULL;
  330. data= NULL;
  331. }
  332. gearmand_error_t gearmand_packet_create(gearmand_packet_st *packet,
  333. const void *arg, size_t arg_size)
  334. {
  335. return packet_create_arg(packet, arg, arg_size);
  336. }
  337. void gearmand_packet_free(gearmand_packet_st *packet)
  338. {
  339. if (packet->args != packet->args_buffer && packet->args != NULL)
  340. {
  341. free(packet->args);
  342. packet->args= NULL;
  343. }
  344. if (packet->options.free_data && packet->data != NULL)
  345. {
  346. free((void *)packet->data); //@todo fix the need for the casting.
  347. packet->data= NULL;
  348. }
  349. }
  350. gearmand_error_t gearmand_packet_pack_header(gearmand_packet_st *packet)
  351. {
  352. if (packet->magic == GEARMAN_MAGIC_TEXT)
  353. {
  354. packet->options.complete= true;
  355. return GEARMAND_SUCCESS;
  356. }
  357. if (packet->args_size == 0)
  358. {
  359. packet->args= packet->args_buffer;
  360. packet->args_size= GEARMAND_PACKET_HEADER_SIZE;
  361. }
  362. switch (packet->magic)
  363. {
  364. case GEARMAN_MAGIC_TEXT:
  365. break;
  366. case GEARMAN_MAGIC_REQUEST:
  367. memcpy(packet->args, "\0REQ", 4);
  368. break;
  369. case GEARMAN_MAGIC_RESPONSE:
  370. memcpy(packet->args, "\0RES", 4);
  371. break;
  372. default:
  373. gearmand_error("invalid magic value");
  374. return GEARMAND_INVALID_MAGIC;
  375. }
  376. if (packet->command == GEARMAN_COMMAND_TEXT ||
  377. packet->command >= GEARMAN_COMMAND_MAX)
  378. {
  379. gearmand_error("invalid command value");
  380. return GEARMAND_INVALID_COMMAND;
  381. }
  382. uint32_t tmp= packet->command;
  383. tmp= htonl(tmp);
  384. memcpy(packet->args + 4, &tmp, 4);
  385. uint64_t length_64= packet->args_size + packet->data_size - GEARMAND_PACKET_HEADER_SIZE;
  386. // Check for overflow on 32bit(portable?).
  387. if (length_64 >= UINT32_MAX || length_64 < packet->data_size)
  388. {
  389. gearmand_error("data size too too long");
  390. return GEARMAND_ARGUMENT_TOO_LARGE;
  391. }
  392. tmp= (uint32_t)length_64;
  393. tmp= htonl(tmp);
  394. memcpy(packet->args + 8, &tmp, 4);
  395. packet->options.complete= true;
  396. return GEARMAND_SUCCESS;
  397. }
  398. #pragma GCC diagnostic pop