packet.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. return gearman_command_info(packet->command)->name;
  245. }
  246. inline static gearmand_error_t packet_create_arg(gearmand_packet_st *packet,
  247. const void *arg, size_t arg_size)
  248. {
  249. if (packet->argc == gearman_command_info(packet->command)->argc and
  250. (not (gearman_command_info(packet->command)->data) or
  251. packet->data))
  252. {
  253. gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM, "too many arguments for command(%s)", gearman_command_info(packet->command)->name);
  254. return GEARMAND_TOO_MANY_ARGS;
  255. }
  256. if (packet->argc == gearman_command_info(packet->command)->argc)
  257. {
  258. packet->data= static_cast<const char *>(arg);
  259. packet->data_size= arg_size;
  260. return GEARMAND_SUCCESS;
  261. }
  262. if (packet->args_size == 0 and packet->magic != GEARMAN_MAGIC_TEXT)
  263. {
  264. packet->args_size= GEARMAND_PACKET_HEADER_SIZE;
  265. }
  266. if ((packet->args_size + arg_size) < GEARMAND_ARGS_BUFFER_SIZE)
  267. {
  268. packet->args= packet->args_buffer;
  269. }
  270. else
  271. {
  272. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM, "resizing packet buffer");
  273. if (packet->args == packet->args_buffer)
  274. {
  275. packet->args= (char *)realloc(NULL, packet->args_size + arg_size);
  276. memcpy(packet->args, packet->args_buffer, packet->args_size);
  277. }
  278. else
  279. {
  280. char *new_args= (char *)realloc(packet->args, packet->args_size + arg_size);
  281. if (new_args == NULL)
  282. {
  283. return gearmand_perror(errno, "realloc");
  284. }
  285. packet->args= new_args;
  286. }
  287. }
  288. memcpy(packet->args + packet->args_size, arg, arg_size);
  289. packet->args_size+= arg_size;
  290. packet->arg_size[packet->argc]= arg_size;
  291. packet->argc++;
  292. size_t offset;
  293. if (packet->magic == GEARMAN_MAGIC_TEXT)
  294. {
  295. offset= 0;
  296. }
  297. else
  298. {
  299. offset= GEARMAND_PACKET_HEADER_SIZE;
  300. }
  301. for (uint8_t x= 0; x < packet->argc; ++x)
  302. {
  303. packet->arg[x]= packet->args + offset;
  304. offset+= packet->arg_size[x];
  305. }
  306. return GEARMAND_SUCCESS;
  307. }
  308. /** @} */
  309. /*
  310. * Public Definitions
  311. */
  312. void gearmand_packet_st::reset(enum gearman_magic_t magic_, gearman_command_t command_)
  313. {
  314. options.complete= false;
  315. options.free_data= false;
  316. magic= magic_;
  317. command= command_;
  318. argc= 0;
  319. args_size= 0;
  320. data_size= 0;
  321. args= NULL;
  322. data= NULL;
  323. }
  324. gearmand_error_t gearmand_packet_create(gearmand_packet_st *packet,
  325. const void *arg, size_t arg_size)
  326. {
  327. return packet_create_arg(packet, arg, arg_size);
  328. }
  329. void gearmand_packet_free(gearmand_packet_st *packet)
  330. {
  331. if (packet->args != packet->args_buffer && packet->args != NULL)
  332. {
  333. free(packet->args);
  334. packet->args= NULL;
  335. }
  336. if (packet->options.free_data && packet->data != NULL)
  337. {
  338. free((void *)packet->data); //@todo fix the need for the casting.
  339. packet->data= NULL;
  340. }
  341. }
  342. gearmand_error_t gearmand_packet_pack_header(gearmand_packet_st *packet)
  343. {
  344. if (packet->magic == GEARMAN_MAGIC_TEXT)
  345. {
  346. packet->options.complete= true;
  347. return GEARMAND_SUCCESS;
  348. }
  349. if (packet->args_size == 0)
  350. {
  351. packet->args= packet->args_buffer;
  352. packet->args_size= GEARMAND_PACKET_HEADER_SIZE;
  353. }
  354. switch (packet->magic)
  355. {
  356. case GEARMAN_MAGIC_TEXT:
  357. break;
  358. case GEARMAN_MAGIC_REQUEST:
  359. memcpy(packet->args, "\0REQ", 4);
  360. break;
  361. case GEARMAN_MAGIC_RESPONSE:
  362. memcpy(packet->args, "\0RES", 4);
  363. break;
  364. default:
  365. gearmand_error("invalid magic value");
  366. return GEARMAND_INVALID_MAGIC;
  367. }
  368. if (packet->command == GEARMAN_COMMAND_TEXT ||
  369. packet->command >= GEARMAN_COMMAND_MAX)
  370. {
  371. gearmand_error("invalid command value");
  372. return GEARMAND_INVALID_COMMAND;
  373. }
  374. uint32_t tmp= packet->command;
  375. tmp= htonl(tmp);
  376. memcpy(packet->args + 4, &tmp, 4);
  377. uint64_t length_64= packet->args_size + packet->data_size - GEARMAND_PACKET_HEADER_SIZE;
  378. // Check for overflow on 32bit(portable?).
  379. if (length_64 >= UINT32_MAX || length_64 < packet->data_size)
  380. {
  381. gearmand_error("data size too too long");
  382. return GEARMAND_ARGUMENT_TOO_LARGE;
  383. }
  384. tmp= (uint32_t)length_64;
  385. tmp= htonl(tmp);
  386. memcpy(packet->args + 8, &tmp, 4);
  387. packet->options.complete= true;
  388. return GEARMAND_SUCCESS;
  389. }
  390. #pragma GCC diagnostic pop