universal.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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 Gearman State Definitions
  41. */
  42. #include "gear_config.h"
  43. #include "configmake.h"
  44. #include <libgearman/common.h>
  45. #include "libgearman/assert.hpp"
  46. #include "libgearman/interface/push.hpp"
  47. #include "libgearman/server_options.hpp"
  48. #include "libgearman/log.hpp"
  49. #include "libgearman/vector.h"
  50. #include "libgearman/uuid.hpp"
  51. #include "libgearman/pipe.h"
  52. #include "libgearman/protocol/echo.h"
  53. #include "libgearman/protocol/option.h"
  54. #include "libgearman/ssl.h"
  55. #include <cerrno>
  56. #include <cstdarg>
  57. #include <cstdio>
  58. #include <cstdlib>
  59. #include <cstring>
  60. #include <cctype>
  61. #include <unistd.h>
  62. #include <memory>
  63. #ifdef HAVE_SYS_EPOLL_H
  64. # include <sys/epoll.h>
  65. #endif
  66. void gearman_nap(int arg)
  67. {
  68. if (arg < 1)
  69. { }
  70. else
  71. {
  72. #ifdef WIN32
  73. sleep(arg/1000000);
  74. #else
  75. struct timespec global_sleep_value= { 0, static_cast<long>(arg * 1000)};
  76. nanosleep(&global_sleep_value, NULL);
  77. #endif
  78. }
  79. }
  80. void gearman_nap(gearman_universal_st &self)
  81. {
  82. gearman_nap(self.timeout);
  83. }
  84. void gearman_universal_clone(gearman_universal_st &destination, const gearman_universal_st &source)
  85. {
  86. destination.wakeup(source.has_wakeup());
  87. (void)gearman_universal_set_option(destination, GEARMAN_UNIVERSAL_NON_BLOCKING, source.options.non_blocking);
  88. destination.ssl(source.ssl());
  89. destination.timeout= source.timeout;
  90. destination._namespace= gearman_string_clone(source._namespace);
  91. destination._identifier= gearman_string_clone(source._identifier);
  92. destination.verbose= source.verbose;
  93. destination.log_fn= source.log_fn;
  94. destination.log_context= source.log_context;
  95. for (gearman_connection_st *con= source.con_list; con; con= con->next_connection())
  96. {
  97. if (gearman_connection_copy(destination, *con) == NULL)
  98. {
  99. return;
  100. }
  101. }
  102. assert(destination.con_count == source.con_count);
  103. }
  104. void gearman_universal_free(gearman_universal_st &universal)
  105. {
  106. gearman_free_all_cons(universal);
  107. gearman_free_all_packets(universal);
  108. if (universal.pfds)
  109. {
  110. // created realloc()
  111. free(universal.pfds);
  112. universal.pfds= NULL;
  113. }
  114. // clean-up server options
  115. while (universal.server_options_list)
  116. {
  117. delete universal.server_options_list;
  118. }
  119. }
  120. gearman_return_t gearman_universal_set_option(gearman_universal_st &self, universal_options_t option, bool value)
  121. {
  122. switch (option)
  123. {
  124. case GEARMAN_UNIVERSAL_NON_BLOCKING:
  125. self.options.non_blocking= value;
  126. break;
  127. case GEARMAN_UNIVERSAL_DONT_TRACK_PACKETS:
  128. break;
  129. case GEARMAN_UNIVERSAL_IDENTIFY:
  130. if (value)
  131. {
  132. if (self._identifier == NULL)
  133. {
  134. self._identifier= gearman_string_create_guid();
  135. }
  136. assert(self._identifier);
  137. }
  138. else
  139. {
  140. gearman_string_free(self._identifier);
  141. self._identifier= NULL;
  142. }
  143. break;
  144. case GEARMAN_UNIVERSAL_MAX:
  145. default:
  146. return gearman_gerror(self, GEARMAN_INVALID_COMMAND);
  147. }
  148. return GEARMAN_SUCCESS;
  149. }
  150. int gearman_universal_timeout(gearman_universal_st &self)
  151. {
  152. return self.timeout;
  153. }
  154. void gearman_universal_set_timeout(gearman_universal_st &self, int timeout)
  155. {
  156. self.timeout= timeout;
  157. }
  158. void gearman_universal_set_ssl(gearman_universal_st &self, bool ssl,
  159. const char *ca_file, const char *certificate, const char *key_file)
  160. {
  161. self.ssl(ssl);
  162. self.ssl_ca_file(ca_file);
  163. self.ssl_certificate(certificate);
  164. self.ssl_key(key_file);
  165. }
  166. void gearman_set_log_fn(gearman_universal_st &self, gearman_log_fn *function,
  167. void *context, gearman_verbose_t verbose)
  168. {
  169. self.log_fn= function;
  170. self.log_context= context;
  171. self.verbose= verbose;
  172. gearman_log_debug(self, "Enabled logging");
  173. }
  174. void gearman_set_workload_malloc_fn(gearman_universal_st& universal,
  175. gearman_malloc_fn *function,
  176. void *context)
  177. {
  178. universal.allocator.malloc= function;
  179. universal.allocator.context= context;
  180. }
  181. void gearman_set_workload_free_fn(gearman_universal_st& universal,
  182. gearman_free_fn *function,
  183. void *context)
  184. {
  185. universal.allocator.free= function;
  186. universal.allocator.context= context;
  187. }
  188. void gearman_free_all_cons(gearman_universal_st& universal)
  189. {
  190. while (universal.con_list)
  191. {
  192. delete universal.con_list;
  193. }
  194. }
  195. bool gearman_universal_st::wakeup(bool has_wakeup_)
  196. {
  197. if (has_wakeup_)
  198. {
  199. if (wakeup_fd[0] == INVALID_SOCKET)
  200. {
  201. return setup_shutdown_pipe(wakeup_fd);
  202. }
  203. return true;
  204. }
  205. close_wakeup();
  206. return true;
  207. }
  208. void gearman_universal_st::reset()
  209. {
  210. for (gearman_connection_st *con= con_list; con; con= con->next_connection())
  211. {
  212. con->close_socket();
  213. }
  214. }
  215. /*
  216. * Flush all shouldn't return any error, because there's no way to indicate
  217. * which connection experienced an issue. Error detection is better done in gearman_wait()
  218. * after flushing all the connections here.
  219. */
  220. void gearman_universal_st::flush()
  221. {
  222. for (gearman_connection_st *con= con_list; con; con= con->next_connection())
  223. {
  224. if (con->is_events(POLLOUT))
  225. {
  226. continue;
  227. }
  228. con->flush();
  229. }
  230. }
  231. gearman_return_t gearman_wait(gearman_universal_st& universal)
  232. {
  233. struct pollfd *pfds;
  234. bool have_shutdown_pipe= universal.has_wakeup();
  235. size_t con_count= universal.con_count +int(have_shutdown_pipe);
  236. if (universal.pfds_size < con_count)
  237. {
  238. pfds= static_cast<pollfd*>(realloc(universal.pfds, con_count * sizeof(struct pollfd)));
  239. if (pfds == NULL)
  240. {
  241. return gearman_universal_set_error(universal, GEARMAN_MEMORY_ALLOCATION_FAILURE, GEARMAN_AT,
  242. "realloc failed to allocate %u pollfd", uint32_t(con_count));
  243. }
  244. universal.pfds= pfds;
  245. universal.pfds_size= int(con_count);
  246. }
  247. else
  248. {
  249. pfds= universal.pfds;
  250. }
  251. nfds_t x= 0;
  252. for (gearman_connection_st *con= universal.con_list; con; con= con->next_connection())
  253. {
  254. if (con->events())
  255. {
  256. con->set_pollfd(pfds[x]);
  257. x++;
  258. }
  259. }
  260. if (x == 0)
  261. {
  262. return gearman_error(universal, GEARMAN_NO_ACTIVE_FDS, "no active file descriptors");
  263. }
  264. // Wakeup handling, we only make use of this if we have active connections
  265. size_t pipe_array_iterator= 0;
  266. if (have_shutdown_pipe)
  267. {
  268. pipe_array_iterator= x;
  269. pfds[x].fd= universal.wakeup_fd[0];
  270. pfds[x].events= POLLIN;
  271. pfds[x].revents= 0;
  272. x++;
  273. }
  274. int ret= 0;
  275. while (universal.timeout)
  276. {
  277. ret= poll(pfds, x, universal.timeout);
  278. if (ret == -1)
  279. {
  280. switch(errno)
  281. {
  282. case EINTR:
  283. continue;
  284. case EINVAL:
  285. return gearman_perror(universal, errno, "RLIMIT_NOFILE exceeded, or if OSX the timeout value was invalid");
  286. default:
  287. return gearman_perror(universal, errno, "poll");
  288. }
  289. }
  290. break;
  291. }
  292. if (ret == 0)
  293. {
  294. return gearman_universal_set_error(universal, GEARMAN_TIMEOUT, GEARMAN_AT,
  295. "timeout reached, %u servers were poll(), no servers were available, pipe:%s",
  296. uint32_t(x - have_shutdown_pipe), have_shutdown_pipe ? "true" : "false");
  297. }
  298. x= 0;
  299. for (gearman_connection_st *con= universal.con_list; con; con= con->next_connection())
  300. {
  301. if (con->events())
  302. {
  303. if (pfds[x].revents & (POLLERR | POLLHUP | POLLNVAL))
  304. {
  305. int err;
  306. socklen_t len= sizeof (err);
  307. if (getsockopt(pfds[x].fd, SOL_SOCKET, SO_ERROR, &err, &len) == 0)
  308. {
  309. con->error(err);
  310. }
  311. }
  312. con->set_revents(pfds[x].revents);
  313. x++;
  314. }
  315. }
  316. if (have_shutdown_pipe and pfds[pipe_array_iterator].revents)
  317. {
  318. char buffer[1];
  319. ssize_t read_length= read(universal.wakeup_fd[0], buffer, sizeof(buffer));
  320. if (read_length > 0)
  321. {
  322. gearman_return_t local_ret= gearman_kill(gearman_universal_id(universal), GEARMAN_INTERRUPT);
  323. if (gearman_failed(local_ret))
  324. {
  325. return gearman_gerror(universal, GEARMAN_SHUTDOWN);
  326. }
  327. return gearman_gerror(universal, GEARMAN_SHUTDOWN_GRACEFUL);
  328. }
  329. if (read_length == 0)
  330. {
  331. return gearman_gerror(universal, GEARMAN_SHUTDOWN);
  332. }
  333. if (read_length == -1)
  334. {
  335. gearman_perror(universal, errno, "read() from shutdown pipe");
  336. }
  337. #if 0
  338. perror("shudown read");
  339. #endif
  340. // @todo figure out what happens in an error
  341. }
  342. return GEARMAN_SUCCESS;
  343. }
  344. gearman_connection_st *gearman_ready(gearman_universal_st& universal)
  345. {
  346. /*
  347. We can't keep universal between calls since connections may be removed during
  348. processing. If this list ever gets big, we may want something faster.
  349. */
  350. for (gearman_connection_st *con= universal.con_list; con; con= con->next_connection())
  351. {
  352. if (con->options.ready)
  353. {
  354. con->options.ready= false;
  355. return con;
  356. }
  357. }
  358. return NULL;
  359. }
  360. void gearman_universal_st::close_wakeup()
  361. {
  362. if (wakeup_fd[0] != INVALID_SOCKET)
  363. {
  364. close(wakeup_fd[0]);
  365. }
  366. if (wakeup_fd[1] != INVALID_SOCKET)
  367. {
  368. close(wakeup_fd[1]);
  369. }
  370. }
  371. gearman_universal_st::~gearman_universal_st()
  372. {
  373. close_wakeup();
  374. gearman_string_free(_identifier);
  375. gearman_string_free(_namespace);
  376. #if defined(HAVE_SSL) && HAVE_SSL
  377. if (_ctx_ssl)
  378. {
  379. SSL_CTX_free(_ctx_ssl);
  380. }
  381. #else
  382. assert(_ctx_ssl == NULL);
  383. #endif
  384. }
  385. gearman_return_t gearman_universal_st::option(const universal_options_t& option_, bool value)
  386. {
  387. switch (option_)
  388. {
  389. case GEARMAN_UNIVERSAL_NON_BLOCKING:
  390. non_blocking(value);
  391. break;
  392. case GEARMAN_UNIVERSAL_DONT_TRACK_PACKETS:
  393. break;
  394. case GEARMAN_UNIVERSAL_IDENTIFY:
  395. _identifier= gearman_string_create_guid();
  396. assert(_identifier);
  397. break;
  398. case GEARMAN_UNIVERSAL_MAX:
  399. default:
  400. return gearman_gerror(*this, GEARMAN_INVALID_COMMAND);
  401. }
  402. return GEARMAN_SUCCESS;
  403. }
  404. bool gearman_universal_st::init_ssl()
  405. {
  406. if (ssl())
  407. {
  408. #if defined(HAVE_SSL) && HAVE_SSL
  409. // Check these files exist or not to avoid coredump.
  410. FILE *file = NULL;
  411. if ((file = fopen(ssl_ca_file(), "r")) == NULL)
  412. {
  413. gearman_universal_set_error(*this, GEARMAN_INVALID_ARGUMENT, GEARMAN_AT, "Failed to open CA certificate %s (%d: %s)", ssl_ca_file(), errno, strerror(errno));
  414. return false;
  415. }
  416. fclose(file);
  417. if ((file = fopen(ssl_certificate(), "r")) == NULL)
  418. {
  419. gearman_universal_set_error(*this, GEARMAN_INVALID_ARGUMENT, GEARMAN_AT, "Failed to open certificate %s (%d: %s)", ssl_certificate(), errno, strerror(errno));
  420. return false;
  421. }
  422. fclose(file);
  423. if ((file = fopen(ssl_key(), "r")) == NULL)
  424. {
  425. gearman_universal_set_error(*this, GEARMAN_INVALID_ARGUMENT, GEARMAN_AT, "Failed to open certificate key %s (%d: %s)", ssl_key(), errno, strerror(errno));
  426. return false;
  427. }
  428. fclose(file);
  429. SSL_load_error_strings();
  430. SSL_library_init();
  431. #if (OPENSSL_VERSION_NUMBER < 0x10100000L)
  432. if ((_ctx_ssl= SSL_CTX_new(TLSv1_2_client_method())) == NULL)
  433. #else
  434. if ((_ctx_ssl= SSL_CTX_new(TLS_client_method())) == NULL)
  435. #endif
  436. {
  437. gearman_universal_set_error(*this, GEARMAN_INVALID_ARGUMENT, GEARMAN_AT, "TLS_client_method() failed");
  438. return false;
  439. }
  440. if (SSL_CTX_load_verify_locations(_ctx_ssl, ssl_ca_file(), 0) != SSL_SUCCESS)
  441. {
  442. gearman_universal_set_error(*this, GEARMAN_INVALID_ARGUMENT, GEARMAN_AT, "Failed to load CA certificate %s", ssl_ca_file());
  443. return false;
  444. }
  445. if (SSL_CTX_use_certificate_file(_ctx_ssl, ssl_certificate(), SSL_FILETYPE_PEM) != SSL_SUCCESS)
  446. {
  447. gearman_universal_set_error(*this, GEARMAN_INVALID_ARGUMENT, GEARMAN_AT, "Failed to load certificate %s", ssl_certificate());
  448. return false;
  449. }
  450. if (SSL_CTX_use_PrivateKey_file(_ctx_ssl, ssl_key(), SSL_FILETYPE_PEM) != SSL_SUCCESS)
  451. {
  452. gearman_universal_set_error(*this, GEARMAN_INVALID_ARGUMENT, GEARMAN_AT, "Failed to load certificate key %s", ssl_key());
  453. return false;
  454. }
  455. if (SSL_CTX_check_private_key(_ctx_ssl) != SSL_SUCCESS)
  456. {
  457. gearman_universal_set_error(*this, GEARMAN_INVALID_ARGUMENT, GEARMAN_AT, "Failed to check private key");
  458. return false;
  459. }
  460. #endif // defined(HAVE_SSL) && HAVE_SSL
  461. }
  462. return true;
  463. }
  464. void gearman_universal_st::identifier(const char *identifier_, const size_t identifier_size_)
  465. {
  466. gearman_string_free(_identifier);
  467. if (identifier_ and identifier_size_)
  468. {
  469. _identifier= gearman_string_create(NULL, identifier_, identifier_size_);
  470. }
  471. else
  472. {
  473. _identifier= NULL;
  474. }
  475. }
  476. gearman_return_t gearman_set_identifier(gearman_universal_st& universal,
  477. const char *id, size_t id_size)
  478. {
  479. if (id == NULL)
  480. {
  481. return gearman_error(universal, GEARMAN_INVALID_ARGUMENT, "id was NULL");
  482. }
  483. if (id_size == 0)
  484. {
  485. return gearman_error(universal, GEARMAN_INVALID_ARGUMENT, "id_size was 0");
  486. }
  487. if (id_size > GEARMAN_MAX_IDENTIFIER)
  488. {
  489. return gearman_error(universal, GEARMAN_ARGUMENT_TOO_LARGE, "id_size was greater then GEARMAN_MAX_ECHO_SIZE");
  490. }
  491. for (size_t x= 0; x < id_size; x++)
  492. {
  493. if (isgraph(id[x]) == false)
  494. {
  495. return gearman_error(universal, GEARMAN_INVALID_ARGUMENT, "Invalid character found in identifier");
  496. }
  497. }
  498. universal.identifier(id, id_size);
  499. for (gearman_connection_st *con= universal.con_list; con; con= con->next_connection())
  500. {
  501. con->send_identifier();
  502. }
  503. return GEARMAN_SUCCESS;
  504. }
  505. static gearman_return_t connection_loop(gearman_universal_st& universal,
  506. const gearman_packet_st& message,
  507. Check& check)
  508. {
  509. gearman_return_t ret= GEARMAN_SUCCESS;
  510. for (gearman_connection_st *con= universal.con_list; con; con= con->next_connection())
  511. {
  512. ret= con->send_packet(message, true);
  513. if (gearman_failed(ret))
  514. {
  515. #if 0
  516. assert_msg(con->universal.error.rc != GEARMAN_SUCCESS, "Programmer error, error returned but not recorded");
  517. #endif
  518. break;
  519. }
  520. con->options.packet_in_use= true;
  521. gearman_packet_st *packet_ptr= con->receiving(con->_packet, ret, true);
  522. if (packet_ptr == NULL)
  523. {
  524. if (ret != GEARMAN_NOT_CONNECTED and ret != GEARMAN_LOST_CONNECTION)
  525. {
  526. assert(&con->_packet == universal.packet_list);
  527. }
  528. con->options.packet_in_use= false;
  529. break;
  530. }
  531. assert(packet_ptr == &con->_packet);
  532. if (gearman_failed(ret))
  533. {
  534. #if 0
  535. assert_msg(con->universal.error.rc != GEARMAN_SUCCESS, "Programmer error, error returned but not recorded");
  536. #endif
  537. con->free_private_packet();
  538. con->reset_recv_packet();
  539. break;
  540. }
  541. assert(packet_ptr);
  542. if (gearman_failed(ret= check.success(con)))
  543. {
  544. #if 0
  545. assert_msg(con->universal.error.rc != GEARMAN_SUCCESS, "Programmer error, error returned but not recorded");
  546. #endif
  547. con->free_private_packet();
  548. con->reset_recv_packet();
  549. break;
  550. }
  551. con->reset_recv_packet();
  552. con->free_private_packet();
  553. }
  554. return ret;
  555. }
  556. gearman_return_t gearman_server_option(gearman_universal_st& universal, gearman_string_t& option)
  557. {
  558. if (universal.has_connections() == false)
  559. {
  560. return gearman_universal_set_error(universal, GEARMAN_NO_SERVERS, GEARMAN_AT, "no servers provided");
  561. }
  562. gearman_packet_st message;
  563. gearman_return_t ret= libgearman::protocol::option(universal, message, option);
  564. if (gearman_success(ret))
  565. {
  566. PUSH_BLOCKING(universal);
  567. OptionCheck check(universal, option);
  568. ret= connection_loop(universal, message, check);
  569. }
  570. else
  571. {
  572. return universal.error_code();
  573. }
  574. gearman_packet_free(&message);
  575. return ret;
  576. }
  577. gearman_return_t gearman_echo(gearman_universal_st& universal,
  578. const void *workload_str,
  579. size_t workload_size)
  580. {
  581. if (universal.has_connections() == false)
  582. {
  583. return gearman_universal_set_error(universal, GEARMAN_NO_SERVERS, GEARMAN_AT, "no servers provided");
  584. }
  585. gearman_string_t workload= { static_cast<const char*>(workload_str), workload_size };
  586. gearman_packet_st message;
  587. gearman_return_t ret= libgearman::protocol::echo(universal, message, workload);
  588. if (gearman_success(ret))
  589. {
  590. PUSH_BLOCKING(universal);
  591. EchoCheck check(universal, workload_str, workload_size);
  592. ret= connection_loop(universal, message, check);
  593. }
  594. else
  595. {
  596. return universal.error_code();
  597. }
  598. gearman_packet_free(&message);
  599. return ret;
  600. }
  601. gearman_return_t cancel_job(gearman_universal_st& universal,
  602. gearman_job_handle_t job_handle)
  603. {
  604. if (universal.has_connections() == false)
  605. {
  606. return gearman_universal_set_error(universal, GEARMAN_NO_SERVERS, GEARMAN_AT, "no servers provided");
  607. }
  608. const void *args[1];
  609. size_t args_size[1];
  610. args[0]= job_handle;
  611. args_size[0]= strlen(job_handle);
  612. gearman_packet_st cancel_packet;
  613. gearman_return_t ret= gearman_packet_create_args(universal,
  614. cancel_packet,
  615. GEARMAN_MAGIC_REQUEST,
  616. GEARMAN_COMMAND_WORK_FAIL,
  617. args, args_size, 1);
  618. if (gearman_success(ret))
  619. {
  620. PUSH_BLOCKING(universal);
  621. CancelCheck check(universal);
  622. ret= connection_loop(universal, cancel_packet, check);
  623. }
  624. else
  625. {
  626. gearman_packet_free(&cancel_packet);
  627. return universal.error_code();
  628. }
  629. gearman_packet_free(&cancel_packet);
  630. return ret;
  631. }
  632. void gearman_free_all_packets(gearman_universal_st &universal)
  633. {
  634. while (universal.packet_list)
  635. {
  636. gearman_packet_free(universal.packet_list);
  637. }
  638. }
  639. gearman_id_t gearman_universal_id(gearman_universal_st &universal)
  640. {
  641. gearman_id_t handle= { universal.wakeup_fd[0], universal.wakeup_fd[1] };
  642. return handle;
  643. }
  644. /*
  645. * Local Definitions
  646. */
  647. void gearman_universal_set_namespace(gearman_universal_st& universal, const char *namespace_key, size_t namespace_key_size)
  648. {
  649. gearman_string_free(universal._namespace);
  650. if (namespace_key)
  651. {
  652. universal._namespace= gearman_string_create(NULL, namespace_key, namespace_key_size);
  653. }
  654. else
  655. {
  656. universal._namespace= NULL;
  657. }
  658. }
  659. const char *gearman_univeral_namespace(gearman_universal_st& universal)
  660. {
  661. return gearman_string_value(universal._namespace);
  662. }