text.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2012 Data Differential, http://datadifferential.com/
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following disclaimer
  17. * in the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * The names of its contributors may not be used to endorse or
  21. * promote products derived from this software without specific prior
  22. * written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. */
  37. #include "gear_config.h"
  38. #include "libgearman-server/common.h"
  39. #include <cassert>
  40. #include <cerrno>
  41. #include <cstring>
  42. #define TEXT_SUCCESS "OK\r\n"
  43. #define TEXT_ERROR_ARGS "ERR INVALID_ARGUMENTS An+incomplete+set+of+arguments+was+sent+to+this+command+%.*s\r\n"
  44. #define TEXT_ERROR_CREATE_FUNCTION "ERR CREATE_FUNCTION %.*s\r\n"
  45. #define TEXT_ERROR_UNKNOWN_COMMAND "ERR UNKNOWN_COMMAND Unknown+server+command%.*s\r\n"
  46. #define TEXT_ERROR_INTERNAL_ERROR "ERR UNKNOWN_ERROR\r\n"
  47. gearmand_error_t server_run_text(gearman_server_con_st *server_con,
  48. gearmand_packet_st *packet)
  49. {
  50. size_t total;
  51. char *data= (char *)(char *)malloc(GEARMAN_TEXT_RESPONSE_SIZE);
  52. if (data == NULL)
  53. {
  54. gearmand_perror("malloc");
  55. return GEARMAN_MEMORY_ALLOCATION_FAILURE;
  56. }
  57. total= GEARMAN_TEXT_RESPONSE_SIZE;
  58. data[0]= 0;
  59. if (packet->argc)
  60. {
  61. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM, "text command %.*s", packet->arg_size[0], packet->arg[0]);
  62. }
  63. if (packet->argc == 0)
  64. {
  65. snprintf(data, GEARMAN_TEXT_RESPONSE_SIZE, TEXT_ERROR_UNKNOWN_COMMAND, 4, "NULL");
  66. }
  67. else if (strcasecmp("workers", (char *)(packet->arg[0])) == 0)
  68. {
  69. size_t size= 0;
  70. for (gearman_server_thread_st *thread= Server->thread_list;
  71. thread != NULL;
  72. thread= thread->next)
  73. {
  74. int error;
  75. if (! (error= pthread_mutex_lock(&thread->lock)))
  76. {
  77. for (gearman_server_con_st *con= thread->con_list; con != NULL; con= con->next)
  78. {
  79. if (con->_host == NULL)
  80. {
  81. continue;
  82. }
  83. if (size > total)
  84. {
  85. size= total;
  86. }
  87. /* Make sure we have at least GEARMAN_TEXT_RESPONSE_SIZE bytes. */
  88. if (size + GEARMAN_TEXT_RESPONSE_SIZE > total)
  89. {
  90. char *new_data= (char *)realloc(data, total + GEARMAN_TEXT_RESPONSE_SIZE);
  91. if (new_data == NULL)
  92. {
  93. if (pthread_mutex_unlock(&(thread->lock)) == -1)
  94. {
  95. gearmand_fatal("pthread_mutex_unlock()");
  96. assert(!"pthread_mutex_lock");
  97. }
  98. gearmand_perror("realloc");
  99. gearmand_debug("free");
  100. free(data);
  101. return GEARMAN_MEMORY_ALLOCATION_FAILURE;
  102. }
  103. data= new_data;
  104. total+= GEARMAN_TEXT_RESPONSE_SIZE;
  105. }
  106. int sn_checked_length= snprintf(data + size, total - size, "%d %s %s :",
  107. con->con.fd, con->_host, con->id);
  108. if ((size_t)sn_checked_length > total - size || sn_checked_length < 0)
  109. {
  110. if (pthread_mutex_unlock(&(thread->lock)) == -1)
  111. {
  112. gearmand_fatal("pthread_mutex_unlock()");
  113. assert(!"pthread_mutex_lock");
  114. }
  115. gearmand_debug("free");
  116. free(data);
  117. gearmand_perror("snprintf");
  118. return GEARMAN_MEMORY_ALLOCATION_FAILURE;
  119. }
  120. size+= (size_t)sn_checked_length;
  121. if (size > total)
  122. {
  123. continue;
  124. }
  125. for (gearman_server_worker_st *worker= con->worker_list; worker != NULL; worker= worker->con_next)
  126. {
  127. int checked_length= snprintf(data + size, total - size, " %.*s",
  128. (int)(worker->function->function_name_size),
  129. worker->function->function_name);
  130. if ((size_t)checked_length > total - size || checked_length < 0)
  131. {
  132. if (pthread_mutex_unlock(&(thread->lock)) == -1)
  133. {
  134. gearmand_fatal("pthread_mutex_unlock()");
  135. assert(!"pthread_mutex_lock");
  136. }
  137. gearmand_debug("free");
  138. free(data);
  139. gearmand_perror("snprintf");
  140. return GEARMAN_MEMORY_ALLOCATION_FAILURE;
  141. }
  142. size+= (size_t)checked_length;
  143. if (size > total)
  144. break;
  145. }
  146. if (size > total)
  147. {
  148. continue;
  149. }
  150. int checked_length= snprintf(data + size, total - size, "\n");
  151. if ((size_t)checked_length > total - size || checked_length < 0)
  152. {
  153. if (pthread_mutex_unlock(&(thread->lock)) == -1)
  154. {
  155. gearmand_fatal("pthread_mutex_unlock()");
  156. assert(!"pthread_mutex_lock");
  157. }
  158. gearmand_debug("free");
  159. free(data);
  160. gearmand_perror("snprintf");
  161. return GEARMAN_MEMORY_ALLOCATION_FAILURE;
  162. }
  163. size+= (size_t)checked_length;
  164. }
  165. if (pthread_mutex_unlock(&(thread->lock)) == -1)
  166. {
  167. gearmand_fatal("pthread_mutex_unlock()");
  168. assert(!"pthread_mutex_lock");
  169. }
  170. }
  171. else
  172. {
  173. errno= error;
  174. gearmand_error("pthread_mutex_lock");
  175. assert(! "pthread_mutex_lock");
  176. }
  177. }
  178. if (size < total)
  179. {
  180. int checked_length= snprintf(data + size, total - size, ".\n");
  181. if ((size_t)checked_length > total - size || checked_length < 0)
  182. {
  183. gearmand_perror("snprintf");
  184. return GEARMAN_MEMORY_ALLOCATION_FAILURE;
  185. }
  186. }
  187. }
  188. else if (strcasecmp("status", (char *)(packet->arg[0])) == 0)
  189. {
  190. size_t size= 0;
  191. gearman_server_function_st *function;
  192. for (function= Server->function_list; function != NULL;
  193. function= function->next)
  194. {
  195. if (size + GEARMAN_TEXT_RESPONSE_SIZE > total)
  196. {
  197. char *new_data= (char *)realloc(data, total + GEARMAN_TEXT_RESPONSE_SIZE);
  198. if (new_data == NULL)
  199. {
  200. gearmand_perror("realloc");
  201. gearmand_debug("free");
  202. free(data);
  203. return GEARMAN_MEMORY_ALLOCATION_FAILURE;
  204. }
  205. data= new_data;
  206. total+= GEARMAN_TEXT_RESPONSE_SIZE;
  207. }
  208. int checked_length= snprintf(data + size, total - size, "%.*s\t%u\t%u\t%u\n",
  209. (int)(function->function_name_size),
  210. function->function_name, function->job_total,
  211. function->job_running, function->worker_count);
  212. if ((size_t)checked_length > total - size || checked_length < 0)
  213. {
  214. gearmand_perror("snprintf");
  215. gearmand_debug("free");
  216. free(data);
  217. return GEARMAN_MEMORY_ALLOCATION_FAILURE;
  218. }
  219. size+= (size_t)checked_length;
  220. if (size > total)
  221. {
  222. size= total;
  223. }
  224. }
  225. if (size < total)
  226. {
  227. int checked_length= snprintf(data + size, total - size, ".\n");
  228. if ((size_t)checked_length > total - size || checked_length < 0)
  229. {
  230. gearmand_perror("snprintf");
  231. gearmand_debug("free");
  232. free(data);
  233. return GEARMAN_MEMORY_ALLOCATION_FAILURE;
  234. }
  235. }
  236. }
  237. else if (strcasecmp("create", (char *)(packet->arg[0])) == 0)
  238. {
  239. if (packet->argc == 3 && !strcasecmp("function", (char *)(packet->arg[1])))
  240. {
  241. gearman_server_function_st *function;
  242. function= gearman_server_function_get(Server, (char *)(packet->arg[2]), packet->arg_size[2] -2);
  243. if (function)
  244. {
  245. snprintf(data, GEARMAN_TEXT_RESPONSE_SIZE, TEXT_SUCCESS);
  246. }
  247. else
  248. {
  249. snprintf(data, GEARMAN_TEXT_RESPONSE_SIZE, TEXT_ERROR_CREATE_FUNCTION,
  250. (int)packet->arg_size[2], (char *)(packet->arg[2]));
  251. }
  252. }
  253. else
  254. {
  255. // create
  256. snprintf(data, GEARMAN_TEXT_RESPONSE_SIZE, TEXT_ERROR_ARGS, (int)packet->arg_size[0], (char *)(packet->arg[0]));
  257. }
  258. }
  259. else if (strcasecmp("drop", (char *)(packet->arg[0])) == 0)
  260. {
  261. if (packet->argc == 3 && !strcasecmp("function", (char *)(packet->arg[1])))
  262. {
  263. bool success= false;
  264. for (gearman_server_function_st *function= Server->function_list; function != NULL; function= function->next)
  265. {
  266. if (strcasecmp(function->function_name, (char *)(packet->arg[2])) == 0)
  267. {
  268. success= true;
  269. if (function->worker_count == 0 && function->job_running == 0)
  270. {
  271. gearman_server_function_free(Server, function);
  272. snprintf(data, GEARMAN_TEXT_RESPONSE_SIZE, TEXT_SUCCESS);
  273. }
  274. else
  275. {
  276. snprintf(data, GEARMAN_TEXT_RESPONSE_SIZE, "ERR there are still connected workers or executing clients\r\n");
  277. }
  278. break;
  279. }
  280. }
  281. if (success == false)
  282. {
  283. snprintf(data, GEARMAN_TEXT_RESPONSE_SIZE, "ERR function not found\r\n");
  284. gearmand_debug(data);
  285. }
  286. }
  287. else
  288. {
  289. // drop
  290. snprintf(data, GEARMAN_TEXT_RESPONSE_SIZE, TEXT_ERROR_ARGS, (int)packet->arg_size[0], (char *)(packet->arg[0]));
  291. }
  292. }
  293. else if (strcasecmp("maxqueue", (char *)(packet->arg[0])) == 0)
  294. {
  295. if (packet->argc == 1)
  296. {
  297. snprintf(data, GEARMAN_TEXT_RESPONSE_SIZE, TEXT_ERROR_ARGS, (int)packet->arg_size[0], (char *)(packet->arg[0]));
  298. }
  299. else
  300. {
  301. uint32_t max_queue_size[GEARMAN_JOB_PRIORITY_MAX];
  302. for (int priority= 0; priority < GEARMAN_JOB_PRIORITY_MAX; ++priority)
  303. {
  304. const int argc= priority +2;
  305. if (packet->argc > argc)
  306. {
  307. const int parameter= atoi((char *)(packet->arg[argc]));
  308. if (parameter < 0)
  309. {
  310. max_queue_size[priority]= 0;
  311. }
  312. else
  313. {
  314. max_queue_size[priority]= (uint32_t)parameter;
  315. }
  316. }
  317. else
  318. {
  319. max_queue_size[priority]= GEARMAN_DEFAULT_MAX_QUEUE_SIZE;
  320. }
  321. }
  322. /*
  323. To preserve the existing behavior of maxqueue, ensure that the
  324. one-parameter invocation is applied to all priorities.
  325. */
  326. if (packet->argc <= 3)
  327. {
  328. for (int priority= 1; priority < GEARMAN_JOB_PRIORITY_MAX; ++priority)
  329. {
  330. max_queue_size[priority]= max_queue_size[0];
  331. }
  332. }
  333. for (gearman_server_function_st *function= Server->function_list; function != NULL; function= function->next)
  334. {
  335. if (strlen((char *)(packet->arg[1])) == function->function_name_size &&
  336. (memcmp(packet->arg[1], function->function_name, function->function_name_size) == 0))
  337. {
  338. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM, "Applying queue limits to %s", function->function_name);
  339. memcpy(function->max_queue_size, max_queue_size, sizeof(uint32_t) * GEARMAN_JOB_PRIORITY_MAX);
  340. }
  341. }
  342. snprintf(data, GEARMAN_TEXT_RESPONSE_SIZE, TEXT_SUCCESS);
  343. }
  344. }
  345. else if (strcasecmp("getpid", (char *)(packet->arg[0])) == 0)
  346. {
  347. snprintf(data, GEARMAN_TEXT_RESPONSE_SIZE, "OK %d\n", (int)getpid());
  348. }
  349. else if (strcasecmp("shutdown", (char *)(packet->arg[0])) == 0)
  350. {
  351. if (packet->argc == 1)
  352. {
  353. Server->shutdown= true;
  354. snprintf(data, GEARMAN_TEXT_RESPONSE_SIZE, TEXT_SUCCESS);
  355. }
  356. else if (packet->argc == 2 &&
  357. strcasecmp("graceful", (char *)(packet->arg[1])) == 0)
  358. {
  359. Server->shutdown_graceful= true;
  360. snprintf(data, GEARMAN_TEXT_RESPONSE_SIZE, TEXT_SUCCESS);
  361. }
  362. else
  363. {
  364. // shutdown
  365. snprintf(data, GEARMAN_TEXT_RESPONSE_SIZE, TEXT_ERROR_ARGS, (int)packet->arg_size[0], (char *)(packet->arg[0]));
  366. }
  367. }
  368. else if (strcasecmp("verbose", (char *)(packet->arg[0])) == 0)
  369. {
  370. snprintf(data, GEARMAN_TEXT_RESPONSE_SIZE, "OK %s\n", gearmand_verbose_name(Gearmand()->verbose));
  371. }
  372. else if (strcasecmp("version", (char *)(packet->arg[0])) == 0)
  373. {
  374. snprintf(data, GEARMAN_TEXT_RESPONSE_SIZE, "OK %s\n", PACKAGE_VERSION);
  375. }
  376. else
  377. {
  378. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM, "Failed to find command %.*s(%" PRIu64 ")",
  379. packet->arg_size[0], packet->arg[0],
  380. packet->arg_size[0]);
  381. snprintf(data, GEARMAN_TEXT_RESPONSE_SIZE, TEXT_ERROR_UNKNOWN_COMMAND, (int)packet->arg_size[0], (char *)(packet->arg[0]));
  382. }
  383. gearman_server_packet_st *server_packet= gearman_server_packet_create(server_con->thread, false);
  384. if (server_packet == NULL)
  385. {
  386. gearmand_debug("free");
  387. free(data);
  388. return GEARMAN_MEMORY_ALLOCATION_FAILURE;
  389. }
  390. gearmand_packet_init(&(server_packet->packet), GEARMAN_MAGIC_TEXT, GEARMAN_COMMAND_TEXT);
  391. server_packet->packet.magic= GEARMAN_MAGIC_TEXT;
  392. server_packet->packet.command= GEARMAN_COMMAND_TEXT;
  393. server_packet->packet.options.complete= true;
  394. server_packet->packet.options.free_data= true;
  395. if (data[0] == 0)
  396. {
  397. snprintf(data, GEARMAN_TEXT_RESPONSE_SIZE, TEXT_ERROR_INTERNAL_ERROR);
  398. }
  399. server_packet->packet.data= data;
  400. server_packet->packet.data_size= strlen(data);
  401. int error;
  402. if ((error= pthread_mutex_lock(&server_con->thread->lock)) == 0)
  403. {
  404. GEARMAN_FIFO_ADD(server_con->io_packet, server_packet,);
  405. if (pthread_mutex_unlock(&(server_con->thread->lock)) == -1)
  406. {
  407. gearmand_fatal("pthread_mutex_unlock()");
  408. assert(!"pthread_mutex_lock");
  409. }
  410. }
  411. else
  412. {
  413. errno= error;
  414. gearmand_perror("pthread_mutex_lock");
  415. assert(!"pthread_mutex_lock");
  416. }
  417. gearman_server_con_io_add(server_con);
  418. return GEARMAN_SUCCESS;
  419. }