connection.cc 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  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 Connection Definitions
  41. */
  42. #include "gear_config.h"
  43. #include "libgearman/common.h"
  44. #include "libgearman/assert.hpp"
  45. #include "libgearman/interface/push.hpp"
  46. #include "libgearman/log.hpp"
  47. #include "libgearman/protocol/option.h"
  48. #include <cerrno>
  49. #include <cstdio>
  50. #include <cstdlib>
  51. #include <cstring>
  52. #include <memory>
  53. #include <unistd.h>
  54. #if HAVE_NETINET_TCP_H
  55. # include <netinet/tcp.h> /* for TCP_NODELAY */
  56. #endif
  57. #ifdef HAVE_FCNTL_H
  58. # include <fcntl.h>
  59. #endif
  60. #ifndef SOCK_CLOEXEC
  61. # define SOCK_CLOEXEC 0
  62. #endif
  63. #ifndef SOCK_NONBLOCK
  64. # define SOCK_NONBLOCK 0
  65. #endif
  66. #ifndef FD_CLOEXEC
  67. # define FD_CLOEXEC 0
  68. #endif
  69. #ifndef MSG_NOSIGNAL
  70. # define MSG_NOSIGNAL 0
  71. #endif
  72. /**
  73. * @addtogroup gearman_connection_static Static Connection Declarations
  74. * @ingroup gearman_connection
  75. * @{
  76. */
  77. gearman_connection_st::gearman_connection_st(gearman_universal_st &universal_arg, const char* host_, const char* service_):
  78. state(GEARMAN_CON_UNIVERSAL_ADDRINFO),
  79. send_state(GEARMAN_CON_SEND_STATE_NONE),
  80. recv_state(GEARMAN_CON_RECV_UNIVERSAL_NONE),
  81. _events(0),
  82. _revents(0),
  83. fd(INVALID_SOCKET),
  84. _ssl(NULL),
  85. cached_errno(0),
  86. created_id(0),
  87. created_id_next(0),
  88. send_buffer_size(0),
  89. send_data_size(0),
  90. send_data_offset(0),
  91. recv_buffer_size(0),
  92. recv_data_size(0),
  93. recv_data_offset(0),
  94. universal(universal_arg),
  95. next(NULL),
  96. prev(NULL),
  97. context(NULL),
  98. _addrinfo(NULL),
  99. addrinfo_next(NULL),
  100. send_buffer_ptr(NULL),
  101. _recv_packet(NULL)
  102. {
  103. set_host(host_, service_);
  104. if (universal.con_list)
  105. {
  106. universal.con_list->prev= this;
  107. }
  108. next= universal.con_list;
  109. universal.con_list= this;
  110. universal.con_count++;
  111. send_buffer_ptr= send_buffer;
  112. recv_buffer_ptr= recv_buffer;
  113. }
  114. gearman_connection_st *gearman_connection_create(gearman_universal_st& universal,
  115. const char *host_, const char* service_)
  116. {
  117. gearman_connection_st *connection= new (std::nothrow) gearman_connection_st(universal, host_, service_);
  118. if (connection)
  119. {
  120. if (gearman_failed(connection->lookup()))
  121. {
  122. delete connection;
  123. return NULL;
  124. }
  125. }
  126. else
  127. {
  128. gearman_perror(universal, errno, "Failed at new() gearman_connection_st new");
  129. }
  130. return connection;
  131. }
  132. gearman_connection_st *gearman_connection_create(gearman_universal_st& universal,
  133. const char *host, const in_port_t& port_)
  134. {
  135. const char *service_ptr= NULL;
  136. char service[GEARMAN_NI_MAXSERV]= { 0 };
  137. if (port_ < 1)
  138. {
  139. service_ptr= GEARMAN_DEFAULT_TCP_PORT_STRING;
  140. }
  141. else
  142. {
  143. snprintf(service, sizeof(service), "%hu", uint16_t(port_));
  144. service[GEARMAN_NI_MAXSERV -1]= 0;
  145. service_ptr= service;
  146. }
  147. return gearman_connection_create(universal, host, service_ptr);
  148. }
  149. gearman_connection_st *gearman_connection_copy(gearman_universal_st& universal,
  150. const gearman_connection_st& from)
  151. {
  152. gearman_connection_st *connection= new (std::nothrow) gearman_connection_st(universal, from.host(), from.service());
  153. if (connection)
  154. {
  155. connection->options.ready= from.options.ready;
  156. // @todo Is this right?
  157. connection->options.packet_in_use= from.options.packet_in_use;
  158. strcpy(connection->_host, from._host);
  159. strcpy(connection->_service, from._service);
  160. }
  161. else
  162. {
  163. gearman_perror(universal, errno, "Failed at new() gearman_connection_st new");
  164. }
  165. return connection;
  166. }
  167. gearman_connection_st::~gearman_connection_st()
  168. {
  169. close_socket();
  170. reset_addrinfo();
  171. { // Remove from universal list
  172. if (universal.con_list == this)
  173. {
  174. universal.con_list= next;
  175. }
  176. if (prev)
  177. {
  178. prev->next= next;
  179. }
  180. if (next)
  181. {
  182. next->prev= prev;
  183. }
  184. universal.con_count--;
  185. }
  186. free_private_packet();
  187. }
  188. void gearman_connection_st::free_private_packet()
  189. {
  190. if (options.packet_in_use)
  191. {
  192. gearman_packet_free(&_packet);
  193. options.packet_in_use= false;
  194. }
  195. }
  196. /** @} */
  197. /*
  198. * Public Definitions
  199. */
  200. void gearman_connection_st::set_host(const char *host_, const in_port_t port_)
  201. {
  202. if (port_ < 1)
  203. {
  204. set_host(host_, GEARMAN_DEFAULT_TCP_PORT_STRING);
  205. }
  206. else
  207. {
  208. snprintf(_service, sizeof(_service), "%hu", uint16_t(port_));
  209. _service[GEARMAN_NI_MAXSERV -1]= 0;
  210. set_host(host_, _service);
  211. }
  212. }
  213. void gearman_connection_st::set_host(const char *host_, const char* service_)
  214. {
  215. reset_addrinfo();
  216. if (host_ and host_[0])
  217. { }
  218. else
  219. {
  220. host_= GEARMAN_DEFAULT_TCP_HOST;
  221. }
  222. strncpy(_host, host_, GEARMAN_NI_MAXHOST);
  223. _host[GEARMAN_NI_MAXHOST -1]= 0;
  224. if (service_ and service_[0])
  225. { }
  226. else
  227. {
  228. service_= GEARMAN_DEFAULT_TCP_PORT_STRING;
  229. }
  230. strncpy(_service, service_, GEARMAN_NI_MAXSERV);
  231. _service[GEARMAN_NI_MAXSERV -1]= 0;
  232. }
  233. /*
  234. Do not call error within this function.
  235. */
  236. void gearman_connection_st::close_socket()
  237. {
  238. if (_ssl)
  239. {
  240. #if defined(HAVE_SSL) && HAVE_SSL
  241. SSL_shutdown(_ssl);
  242. SSL_free(_ssl);
  243. _ssl= NULL;
  244. #endif
  245. }
  246. if (fd != INVALID_SOCKET)
  247. {
  248. /* in case of death shutdown to avoid blocking at close_socket() */
  249. if (shutdown(fd, SHUT_RDWR) == SOCKET_ERROR && get_socket_errno() != ENOTCONN)
  250. { }
  251. else
  252. {
  253. if (closesocket(fd) == SOCKET_ERROR)
  254. { }
  255. }
  256. state= GEARMAN_CON_UNIVERSAL_CONNECT;
  257. fd= INVALID_SOCKET;
  258. _events= 0;
  259. _revents= 0;
  260. send_state= GEARMAN_CON_SEND_STATE_NONE;
  261. send_buffer_ptr= send_buffer;
  262. send_buffer_size= 0;
  263. send_data_size= 0;
  264. send_data_offset= 0;
  265. recv_state= GEARMAN_CON_RECV_UNIVERSAL_NONE;
  266. free_recv_packet();
  267. recv_buffer_ptr= recv_buffer;
  268. recv_buffer_size= 0;
  269. options.server_options_sent= false;
  270. // created_id_next is incremented for every outbound packet (except status).
  271. // created_id is incremented for every response packet received, and also when
  272. // no packets are received due to an error. There are lots of such error paths
  273. // and it seems simpler to just reset these both to zero when a connection is
  274. // 'closed'.
  275. created_id= 0;
  276. created_id_next= 0;
  277. }
  278. }
  279. void gearman_connection_st::free_recv_packet()
  280. {
  281. if (_recv_packet)
  282. {
  283. gearman_packet_free(recv_packet());
  284. _recv_packet= NULL;
  285. }
  286. }
  287. void gearman_connection_st::reset_addrinfo()
  288. {
  289. if (_addrinfo)
  290. {
  291. freeaddrinfo(_addrinfo);
  292. _addrinfo= NULL;
  293. }
  294. addrinfo_next= NULL;
  295. }
  296. gearman_return_t gearman_connection_st::send_identifier(void)
  297. {
  298. if (universal._identifier)
  299. {
  300. options.identifier_sent= false;
  301. const void* id= (void*)universal._identifier->value();
  302. size_t id_size= universal._identifier->size();
  303. gearman_packet_st packet;
  304. gearman_return_t ret= gearman_packet_create_args(universal, packet, GEARMAN_MAGIC_REQUEST, GEARMAN_COMMAND_SET_CLIENT_ID, (const void**)&id, &id_size, 1);
  305. if (gearman_success(ret))
  306. {
  307. PUSH_BLOCKING(universal);
  308. options.identifier_sent= true;
  309. gearman_return_t local_ret= send_packet(packet, true);
  310. if (gearman_failed(local_ret))
  311. {
  312. options.identifier_sent= false;
  313. ret= local_ret;
  314. }
  315. else
  316. {
  317. options.identifier_sent= true;
  318. }
  319. }
  320. gearman_packet_free(&packet);
  321. return ret;
  322. }
  323. return GEARMAN_SUCCESS;
  324. }
  325. /*
  326. * The send_packet() method does not only send the passed-in packet_arg. If there are any server options
  327. * established, and they haven't yet been sent over, then these options are sent over first.
  328. * Only if that succeeds is the packet_arg sent.
  329. * The reason for this is server options are only set once by the client/worker. In the older code, this
  330. * resulted in them being sent over exactly once. If the connection was dropped and rebuilt, then the options
  331. * were not sent over again, rendering them moot. This way, we're guaranteed that the options are always sent
  332. * at least once to a connected server.
  333. */
  334. gearman_return_t gearman_connection_st::send_packet(const gearman_packet_st& packet_arg, const bool flush_buffer)
  335. {
  336. if (options.identifier_sent == false)
  337. {
  338. gearman_return_t ret= send_identifier();
  339. if (gearman_failed(ret))
  340. {
  341. return ret;
  342. }
  343. options.identifier_sent= true;
  344. }
  345. if (options.server_options_sent == false)
  346. {
  347. for (gearman_server_options_st* head= universal.server_options_list;
  348. head;
  349. head= head->next)
  350. {
  351. gearman_packet_st message;
  352. gearman_string_t option= { (const char*)head->value(), head->size() };
  353. gearman_return_t ret= libgearman::protocol::option(universal, message, option);
  354. if (gearman_failed(ret))
  355. {
  356. assert(universal.error_code());
  357. assert(universal.error());
  358. gearman_packet_free(&message);
  359. return universal.error_code();
  360. }
  361. PUSH_BLOCKING(universal);
  362. OptionCheck check(universal, option);
  363. ret= _send_packet(message, true);
  364. if (gearman_failed(ret))
  365. {
  366. assert(universal.error_code());
  367. assert(universal.error());
  368. gearman_packet_free(&message);
  369. return universal.error_code();
  370. }
  371. options.packet_in_use= true;
  372. gearman_packet_st *packet_ptr= receiving(_packet, ret, true);
  373. if (packet_ptr == NULL)
  374. {
  375. gearman_packet_free(&message);
  376. options.packet_in_use= false;
  377. return gearman_error(universal, ret, "Failed in receiving()");
  378. }
  379. if (gearman_failed(ret) ||
  380. gearman_failed(ret= check.success(this)))
  381. {
  382. gearman_packet_free(&message);
  383. free_private_packet();
  384. reset_recv_packet();
  385. return ret;
  386. }
  387. free_private_packet();
  388. reset_recv_packet();
  389. gearman_packet_free(&message);
  390. }
  391. options.server_options_sent= true;
  392. }
  393. return _send_packet(packet_arg, flush_buffer);
  394. }
  395. /*
  396. * This is the real implementation that actually sends a packet. Read the comments for send_packet() for why
  397. * that is. Note that this is a private method. External callers should only call send_packet().
  398. */
  399. #if __GNUC__ >= 7
  400. #pragma GCC diagnostic warning "-Wimplicit-fallthrough"
  401. #endif
  402. gearman_return_t gearman_connection_st::_send_packet(const gearman_packet_st& packet_arg, const bool flush_buffer)
  403. {
  404. switch (send_state)
  405. {
  406. case GEARMAN_CON_SEND_STATE_NONE:
  407. assert_msg(packet_arg.universal, "send_packet() was to execute against a packet with no universal");
  408. universal_reset_error(*(packet_arg.universal));
  409. if (packet_arg.options.complete == false)
  410. {
  411. return gearman_error(universal, GEARMAN_INVALID_PACKET, "packet not complete");
  412. }
  413. /* Pack first part of packet, which is everything but the payload. */
  414. while (1)
  415. {
  416. { // Scoping to shut compiler up about switch/case jump
  417. gearman_return_t rc;
  418. size_t send_size= gearman_packet_pack(packet_arg,
  419. send_buffer +send_buffer_size,
  420. GEARMAN_SEND_BUFFER_SIZE -send_buffer_size, rc);
  421. if (gearman_success(rc))
  422. {
  423. send_buffer_size+= send_size;
  424. break;
  425. }
  426. else if (rc != GEARMAN_FLUSH_DATA)
  427. {
  428. return rc;
  429. }
  430. }
  431. /* We were asked to flush when the buffer is already flushed! */
  432. if (send_buffer_size == 0)
  433. {
  434. return gearman_universal_set_error(universal, GEARMAN_SEND_BUFFER_TOO_SMALL, GEARMAN_AT,
  435. "send buffer too small (%u)", GEARMAN_SEND_BUFFER_SIZE);
  436. }
  437. /* Flush buffer now if first part of packet won't fit in. */
  438. send_state= GEARMAN_CON_SEND_UNIVERSAL_PRE_FLUSH;
  439. case GEARMAN_CON_SEND_UNIVERSAL_PRE_FLUSH:
  440. {
  441. gearman_return_t ret= flush();
  442. if (gearman_failed(ret))
  443. {
  444. return ret;
  445. }
  446. }
  447. }
  448. /* Return here if we have no data to send. */
  449. if (packet_arg.data_size == 0)
  450. {
  451. break;
  452. }
  453. /* If there is any room in the buffer, copy in data. */
  454. if (packet_arg.data and (GEARMAN_SEND_BUFFER_SIZE - send_buffer_size) > 0)
  455. {
  456. send_data_offset= GEARMAN_SEND_BUFFER_SIZE - send_buffer_size;
  457. if (send_data_offset > packet_arg.data_size)
  458. {
  459. send_data_offset= packet_arg.data_size;
  460. }
  461. memcpy(send_buffer + send_buffer_size, packet_arg.data, send_data_offset);
  462. send_buffer_size+= send_data_offset;
  463. /* Return if all data fit in the send buffer. */
  464. if (send_data_offset == packet_arg.data_size)
  465. {
  466. send_data_offset= 0;
  467. break;
  468. }
  469. }
  470. /* Flush buffer now so we can start writing directly from data buffer. */
  471. send_state= GEARMAN_CON_SEND_UNIVERSAL_FORCE_FLUSH;
  472. /* fall-thru */
  473. case GEARMAN_CON_SEND_UNIVERSAL_FORCE_FLUSH:
  474. {
  475. gearman_return_t ret= flush();
  476. if (gearman_failed(ret))
  477. {
  478. return ret;
  479. }
  480. /* fall-thru */
  481. }
  482. send_data_size= packet_arg.data_size;
  483. /* If this is NULL, then gearman_connection_send_data function will be used. */
  484. if (packet_arg.data == NULL)
  485. {
  486. send_state= GEARMAN_CON_SEND_UNIVERSAL_FLUSH_DATA;
  487. return GEARMAN_SUCCESS;
  488. }
  489. /* Copy into the buffer if it fits, otherwise flush from packet buffer. */
  490. send_buffer_size= packet_arg.data_size - send_data_offset;
  491. if (send_buffer_size < GEARMAN_SEND_BUFFER_SIZE)
  492. {
  493. memcpy(send_buffer,
  494. (const char*)(packet_arg.data) +send_data_offset,
  495. send_buffer_size);
  496. send_data_size= 0;
  497. send_data_offset= 0;
  498. break;
  499. }
  500. send_buffer_ptr= (const char*)(size_t(packet_arg.data) +send_data_offset);
  501. send_state= GEARMAN_CON_SEND_UNIVERSAL_FLUSH_DATA;
  502. /* fall-thru */
  503. case GEARMAN_CON_SEND_UNIVERSAL_FLUSH:
  504. case GEARMAN_CON_SEND_UNIVERSAL_FLUSH_DATA:
  505. return flush();
  506. }
  507. if (flush_buffer)
  508. {
  509. send_state= GEARMAN_CON_SEND_UNIVERSAL_FLUSH;
  510. return flush();
  511. }
  512. send_state= GEARMAN_CON_SEND_STATE_NONE;
  513. return GEARMAN_SUCCESS;
  514. }
  515. size_t gearman_connection_st::send_and_flush(const void *data, size_t data_size, gearman_return_t *ret_ptr)
  516. {
  517. if (send_state != GEARMAN_CON_SEND_UNIVERSAL_FLUSH_DATA)
  518. {
  519. return gearman_error(universal, GEARMAN_NOT_FLUSHING, "not flushing");
  520. }
  521. if (data_size > (send_data_size - send_data_offset))
  522. {
  523. return gearman_error(universal, GEARMAN_DATA_TOO_LARGE, "data too large");
  524. }
  525. send_buffer_ptr= (const char*)data;
  526. send_buffer_size= data_size;
  527. *ret_ptr= flush();
  528. return data_size -send_buffer_size;
  529. }
  530. gearman_return_t gearman_connection_st::lookup()
  531. {
  532. reset_addrinfo();
  533. struct addrinfo ai;
  534. memset(&ai, 0, sizeof(struct addrinfo));
  535. ai.ai_socktype= SOCK_STREAM;
  536. ai.ai_protocol= IPPROTO_TCP;
  537. assert_msg(_addrinfo == NULL, "Programmer error, reset_addrinfo() is either broke, or was not called.");
  538. int ret;
  539. if ((ret= getaddrinfo(_host, _service, &ai, &(_addrinfo))))
  540. {
  541. int local_errno= errno;
  542. reset_addrinfo();
  543. switch (ret)
  544. {
  545. case EAI_AGAIN:
  546. return gearman_universal_set_error(universal, GEARMAN_TIMEOUT, GEARMAN_AT, "Nameserver timed out while looking up %s:%s", host(), service());
  547. case EAI_MEMORY:
  548. return gearman_universal_set_error(universal, GEARMAN_MEMORY_ALLOCATION_FAILURE, GEARMAN_AT,
  549. "A memory allocation failed while calling getaddrinfo() for %s:%s", host(), service());
  550. case EAI_SYSTEM:
  551. if (local_errno == ENOENT)
  552. {
  553. return gearman_universal_set_error(universal, GEARMAN_GETADDRINFO, GEARMAN_AT, "DNS lookup failed for %s:%s", host(), service());
  554. }
  555. return gearman_universal_set_perror(universal, GEARMAN_ERRNO, local_errno, GEARMAN_AT, "System error happened during a call to getaddrinfo() for %s:%s", host(), service());
  556. default:
  557. break;
  558. }
  559. return gearman_universal_set_error(universal, GEARMAN_GETADDRINFO, GEARMAN_AT, "%s:%s getaddrinfo:%s", host(), service(), gai_strerror(ret));
  560. }
  561. addrinfo_next= _addrinfo;
  562. assert(addrinfo_next);
  563. state= GEARMAN_CON_UNIVERSAL_CONNECT;
  564. return GEARMAN_SUCCESS;
  565. }
  566. gearman_return_t gearman_connection_st::enable_ssl()
  567. {
  568. #if defined(HAVE_SSL) && HAVE_SSL
  569. if (universal.ssl())
  570. {
  571. _ssl= SSL_new(universal.ctx_ssl());
  572. if (_ssl == NULL)
  573. {
  574. close_socket();
  575. return gearman_error(universal, GEARMAN_COULD_NOT_CONNECT, "wolfSSL_new() failed to return a valid object");
  576. }
  577. if (SSL_set_fd(_ssl, fd) != SSL_SUCCESS)
  578. {
  579. close_socket();
  580. char errorString[SSL_ERROR_SIZE]= { 0 };
  581. ERR_error_string_n(SSL_get_error(_ssl, 0), errorString, sizeof(errorString));
  582. return gearman_error(universal, GEARMAN_COULD_NOT_CONNECT, errorString);
  583. }
  584. SSL_set_connect_state(_ssl);
  585. }
  586. #endif
  587. return GEARMAN_SUCCESS;
  588. }
  589. #if __GNUC__ >= 7
  590. #pragma GCC diagnostic warning "-Wimplicit-fallthrough"
  591. #endif
  592. gearman_return_t gearman_connection_st::flush()
  593. {
  594. while (1)
  595. {
  596. switch (state)
  597. {
  598. case GEARMAN_CON_UNIVERSAL_ADDRINFO:
  599. {
  600. gearman_return_t ret= lookup();
  601. if (gearman_failed(ret))
  602. {
  603. return ret;
  604. }
  605. }
  606. /* fall-thru */
  607. case GEARMAN_CON_UNIVERSAL_CONNECT:
  608. if (fd != INVALID_SOCKET)
  609. {
  610. close_socket();
  611. }
  612. if (addrinfo_next == NULL)
  613. {
  614. state= GEARMAN_CON_UNIVERSAL_ADDRINFO;
  615. return gearman_universal_set_error(universal, GEARMAN_COULD_NOT_CONNECT, GEARMAN_AT, "Connection to %s:%s failed", _host, _service);
  616. }
  617. // rewrite tye if HAVE_SOCK_CLOEXEC
  618. fd= socket(addrinfo_next->ai_family, addrinfo_next->ai_socktype|SOCK_CLOEXEC|SOCK_NONBLOCK, addrinfo_next->ai_protocol);
  619. if (fd == INVALID_SOCKET)
  620. {
  621. state= GEARMAN_CON_UNIVERSAL_ADDRINFO;
  622. return gearman_perror(universal, errno, "socket");
  623. }
  624. {
  625. gearman_return_t gret= set_socket_options();
  626. if (gearman_failed(gret))
  627. {
  628. close_socket();
  629. return gret;
  630. }
  631. }
  632. while (1)
  633. {
  634. if (connect(fd, addrinfo_next->ai_addr, addrinfo_next->ai_addrlen) == 0)
  635. {
  636. state= GEARMAN_CON_UNIVERSAL_CONNECTED;
  637. #if 0
  638. addrinfo_next= NULL;
  639. #endif
  640. break;
  641. }
  642. switch (errno)
  643. {
  644. // Treat as an async connect
  645. case EINTR:
  646. case EINPROGRESS:
  647. state= GEARMAN_CON_UNIVERSAL_CONNECTING;
  648. break;
  649. case ECONNREFUSED:
  650. case ENETUNREACH:
  651. case ETIMEDOUT:
  652. addrinfo_next= addrinfo_next->ai_next;
  653. /* fall-thru */
  654. // We will treat this as an error but retry the address
  655. case EAGAIN:
  656. state= GEARMAN_CON_UNIVERSAL_CONNECT;
  657. close_socket();
  658. break;
  659. default:
  660. gearman_perror(universal, errno, "connect");
  661. close_socket();
  662. return gearman_universal_set_error(universal, GEARMAN_COULD_NOT_CONNECT, GEARMAN_AT, "%s:%s", _host, _service);
  663. }
  664. break;
  665. }
  666. if (state != GEARMAN_CON_UNIVERSAL_CONNECTING)
  667. {
  668. break;
  669. }
  670. /* fall-thru */
  671. case GEARMAN_CON_UNIVERSAL_CONNECTING:
  672. while (1)
  673. {
  674. if (_revents & (POLLERR | POLLHUP | POLLNVAL))
  675. {
  676. state= GEARMAN_CON_UNIVERSAL_CONNECT;
  677. addrinfo_next= addrinfo_next->ai_next;
  678. break;
  679. }
  680. else if (_revents & POLLOUT)
  681. {
  682. state= GEARMAN_CON_UNIVERSAL_CONNECTED;
  683. gearman_return_t ssl_ret;
  684. if ((ssl_ret= enable_ssl()) != GEARMAN_SUCCESS)
  685. {
  686. return ssl_ret;
  687. }
  688. break;
  689. }
  690. set_events(POLLOUT);
  691. if (universal.is_non_blocking())
  692. {
  693. state= GEARMAN_CON_UNIVERSAL_CONNECTING;
  694. return gearman_gerror(universal, GEARMAN_IO_WAIT);
  695. }
  696. gearman_return_t gret= gearman_wait(universal);
  697. if (gearman_failed(gret))
  698. {
  699. return gret;
  700. }
  701. }
  702. if (state != GEARMAN_CON_UNIVERSAL_CONNECTED)
  703. {
  704. break;
  705. }
  706. /* fall-thru */
  707. case GEARMAN_CON_UNIVERSAL_CONNECTED:
  708. while (send_buffer_size != 0)
  709. {
  710. ssize_t write_size;
  711. #if defined(HAVE_SSL) && HAVE_SSL
  712. if (_ssl)
  713. {
  714. #if defined(HAVE_WOLFSSL) && HAVE_WOLFSSL
  715. write_size= wolfSSL_send(_ssl, send_buffer_ptr, int(send_buffer_size), MSG_NOSIGNAL);
  716. #elif defined(HAVE_OPENSSL) && HAVE_OPENSSL
  717. write_size= SSL_write(_ssl, send_buffer_ptr, int(send_buffer_size));
  718. #endif
  719. int ssl_error;
  720. switch ((ssl_error= SSL_get_error(_ssl, int(write_size))))
  721. {
  722. case SSL_ERROR_NONE:
  723. break;
  724. case SSL_ERROR_ZERO_RETURN:
  725. errno= ECONNRESET;
  726. write_size= SOCKET_ERROR;
  727. break;
  728. case SSL_ERROR_WANT_CONNECT:
  729. case SSL_ERROR_WANT_ACCEPT:
  730. case SSL_ERROR_WANT_WRITE:
  731. case SSL_ERROR_WANT_READ:
  732. case SSL_ERROR_WANT_X509_LOOKUP:
  733. errno= EAGAIN;
  734. write_size= SOCKET_ERROR;
  735. break;
  736. case SSL_ERROR_SYSCALL:
  737. if (errno) // If errno is really set, then let our normal error logic handle.
  738. {
  739. write_size= SOCKET_ERROR;
  740. break;
  741. }
  742. /* fall-thru */
  743. case SSL_ERROR_SSL:
  744. default:
  745. {
  746. char errorString[SSL_ERROR_SIZE]= { 0 };
  747. ERR_error_string_n(ssl_error, errorString, sizeof(errorString));
  748. close_socket();
  749. return gearman_universal_set_error(universal, GEARMAN_LOST_CONNECTION, GEARMAN_AT, "SSL failure(%s)", errorString);
  750. }
  751. }
  752. }
  753. else
  754. #endif // define(HAVE_SSL)
  755. {
  756. write_size= ::send(fd, send_buffer_ptr, send_buffer_size, MSG_NOSIGNAL);
  757. }
  758. if (write_size == 0) // Zero value on send()
  759. { }
  760. else if (write_size == SOCKET_ERROR)
  761. {
  762. switch (errno)
  763. {
  764. case ENOTCONN:
  765. #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
  766. case EWOULDBLOCK:
  767. #endif
  768. case EAGAIN:
  769. {
  770. set_events(POLLOUT);
  771. if (gearman_universal_is_non_blocking(universal))
  772. {
  773. return gearman_gerror(universal, GEARMAN_IO_WAIT);
  774. }
  775. gearman_return_t gret= gearman_wait(universal);
  776. if (gearman_failed(gret))
  777. {
  778. return gret;
  779. }
  780. continue;
  781. }
  782. case EPIPE:
  783. {
  784. gearman_return_t ret= gearman_perror(universal, errno, "lost connection to server during send");
  785. close_socket();
  786. return ret;
  787. }
  788. case ECONNRESET:
  789. case EHOSTDOWN:
  790. {
  791. gearman_return_t ret= gearman_perror(universal, errno, "lost connection to server during send");
  792. close_socket();
  793. return ret;
  794. }
  795. default:
  796. break;
  797. }
  798. gearman_return_t ret= gearman_perror(universal, errno, "send");
  799. close_socket();
  800. return ret;
  801. }
  802. gearman_log_debug(universal, "connection sent %u bytes of data", uint32_t(write_size));
  803. send_buffer_size-= size_t(write_size);
  804. if (send_state == GEARMAN_CON_SEND_UNIVERSAL_FLUSH_DATA)
  805. {
  806. send_data_offset+= size_t(write_size);
  807. if (send_data_offset == send_data_size)
  808. {
  809. send_data_size= 0;
  810. send_data_offset= 0;
  811. break;
  812. }
  813. if (send_buffer_size == 0)
  814. {
  815. return GEARMAN_SUCCESS;
  816. }
  817. }
  818. else if (send_buffer_size == 0)
  819. {
  820. break;
  821. }
  822. send_buffer_ptr+= write_size;
  823. }
  824. send_state= GEARMAN_CON_SEND_STATE_NONE;
  825. send_buffer_ptr= send_buffer;
  826. return GEARMAN_SUCCESS;
  827. }
  828. }
  829. }
  830. #if __GNUC__ >= 7
  831. #pragma GCC diagnostic warning "-Wimplicit-fallthrough"
  832. #endif
  833. gearman_packet_st *gearman_connection_st::receiving(gearman_packet_st& packet_arg,
  834. gearman_return_t& ret,
  835. const bool recv_data)
  836. {
  837. switch (recv_state)
  838. {
  839. case GEARMAN_CON_RECV_UNIVERSAL_NONE:
  840. if (state != GEARMAN_CON_UNIVERSAL_CONNECTED)
  841. {
  842. ret= gearman_error(universal, GEARMAN_NOT_CONNECTED, "not connected");
  843. return NULL;
  844. }
  845. // This should not fail
  846. _recv_packet= gearman_packet_create(universal, packet_arg);
  847. assert(_recv_packet == &packet_arg);
  848. if (_recv_packet == NULL)
  849. {
  850. ret= gearman_error(universal, GEARMAN_MEMORY_ALLOCATION_FAILURE, "Programmer error, gearman_packet_create() failed which should not be possible");
  851. return NULL;
  852. }
  853. recv_state= GEARMAN_CON_RECV_UNIVERSAL_READ;
  854. /* fall-thru */
  855. case GEARMAN_CON_RECV_UNIVERSAL_READ:
  856. while (1)
  857. {
  858. // If we have data, see if it is a complete packet
  859. if (recv_buffer_size > 0)
  860. {
  861. assert(recv_packet());
  862. size_t recv_size= gearman_packet_unpack(*(recv_packet()),
  863. recv_buffer_ptr,
  864. recv_buffer_size, ret);
  865. recv_buffer_ptr+= recv_size;
  866. recv_buffer_size-= recv_size;
  867. if (gearman_success(ret))
  868. {
  869. break;
  870. }
  871. else if (ret != GEARMAN_IO_WAIT)
  872. {
  873. assert(universal.error_code());
  874. close_socket();
  875. return NULL;
  876. }
  877. }
  878. /* Shift buffer contents if needed. */
  879. if (recv_buffer_size > 0)
  880. {
  881. memmove(recv_buffer, recv_buffer_ptr, recv_buffer_size);
  882. }
  883. recv_buffer_ptr= recv_buffer;
  884. size_t recv_size= recv_socket(recv_buffer +recv_buffer_size, GEARMAN_RECV_BUFFER_SIZE -recv_buffer_size, ret);
  885. if (gearman_failed(ret))
  886. {
  887. if (ret != GEARMAN_IO_WAIT) {
  888. recv_state= GEARMAN_CON_RECV_UNIVERSAL_NONE;
  889. }
  890. return NULL;
  891. }
  892. recv_buffer_size+= recv_size;
  893. }
  894. if (packet_arg.data_size == 0)
  895. {
  896. recv_state= GEARMAN_CON_RECV_UNIVERSAL_NONE;
  897. break;
  898. }
  899. recv_data_size= packet_arg.data_size;
  900. if (recv_data == false )
  901. {
  902. recv_state= GEARMAN_CON_RECV_STATE_READ_DATA;
  903. break;
  904. }
  905. assert(packet_arg.universal);
  906. packet_arg.data= gearman_malloc((*packet_arg.universal), packet_arg.data_size);
  907. if (packet_arg.data == NULL)
  908. {
  909. ret= gearman_error(universal, GEARMAN_MEMORY_ALLOCATION_FAILURE, "gearman_malloc((*packet_arg.universal), packet_arg.data_size)");
  910. close_socket();
  911. return NULL;
  912. }
  913. packet_arg.options.free_data= true;
  914. recv_state= GEARMAN_CON_RECV_STATE_READ_DATA;
  915. /* fall-thru */
  916. case GEARMAN_CON_RECV_STATE_READ_DATA:
  917. while (recv_data_size)
  918. {
  919. (void)receive_data(static_cast<uint8_t *>(const_cast<void *>(packet_arg.data)) +
  920. recv_data_offset,
  921. packet_arg.data_size -recv_data_offset, ret);
  922. if (gearman_failed(ret))
  923. {
  924. return NULL;
  925. }
  926. }
  927. recv_state= GEARMAN_CON_RECV_UNIVERSAL_NONE;
  928. break;
  929. }
  930. gearman_packet_st *tmp_packet_arg= recv_packet();
  931. reset_recv_packet();
  932. return tmp_packet_arg;
  933. }
  934. size_t gearman_connection_st::receive_data(void *data, size_t data_size, gearman_return_t& ret)
  935. {
  936. size_t recv_size= 0;
  937. if (recv_data_size == 0)
  938. {
  939. ret= GEARMAN_SUCCESS;
  940. return 0;
  941. }
  942. if ((recv_data_size - recv_data_offset) < data_size)
  943. {
  944. data_size= recv_data_size - recv_data_offset;
  945. }
  946. if (recv_buffer_size > 0)
  947. {
  948. if (recv_buffer_size < data_size)
  949. {
  950. recv_size= recv_buffer_size;
  951. }
  952. else
  953. {
  954. recv_size= data_size;
  955. }
  956. memcpy(data, recv_buffer_ptr, recv_size);
  957. recv_buffer_ptr+= recv_size;
  958. recv_buffer_size-= recv_size;
  959. }
  960. if (data_size != recv_size)
  961. {
  962. // @note fix this to test for error before blindly doing this opperation
  963. recv_size+= recv_socket(static_cast<uint8_t *>(const_cast<void *>(data)) + recv_size, data_size - recv_size, ret);
  964. recv_data_offset+= recv_size;
  965. }
  966. else
  967. {
  968. recv_data_offset+= recv_size;
  969. ret= GEARMAN_SUCCESS;
  970. }
  971. if (recv_data_size == recv_data_offset)
  972. {
  973. recv_data_size= 0;
  974. recv_data_offset= 0;
  975. recv_state= GEARMAN_CON_RECV_UNIVERSAL_NONE;
  976. }
  977. return recv_size;
  978. }
  979. size_t gearman_connection_st::recv_socket(void *data, size_t data_size, gearman_return_t& ret)
  980. {
  981. ssize_t read_size;
  982. while (1)
  983. {
  984. #if defined(HAVE_SSL) && HAVE_SSL
  985. if (_ssl)
  986. {
  987. # if defined(HAVE_WOLFSSL) && HAVE_WOLFSSL
  988. read_size= wolfSSL_recv(_ssl, data, int(data_size), MSG_NOSIGNAL);
  989. # elif defined(HAVE_OPENSSL) && HAVE_OPENSSL
  990. read_size= SSL_read(_ssl, data, int(data_size));
  991. # endif
  992. int ssl_error;
  993. switch ((ssl_error= SSL_get_error(_ssl, int(read_size))))
  994. {
  995. case SSL_ERROR_NONE:
  996. break;
  997. case SSL_ERROR_ZERO_RETURN:
  998. read_size= 0;
  999. break;
  1000. case SSL_ERROR_WANT_CONNECT:
  1001. case SSL_ERROR_WANT_ACCEPT:
  1002. case SSL_ERROR_WANT_WRITE:
  1003. case SSL_ERROR_WANT_READ:
  1004. case SSL_ERROR_WANT_X509_LOOKUP:
  1005. errno= EAGAIN;
  1006. read_size= SOCKET_ERROR;
  1007. break;
  1008. case SSL_ERROR_SYSCALL:
  1009. if (errno) // If errno is really set, then let our normal error logic handle.
  1010. {
  1011. read_size= SOCKET_ERROR;
  1012. break;
  1013. }
  1014. /* fall-thru */
  1015. case SSL_ERROR_SSL:
  1016. default:
  1017. {
  1018. char errorString[SSL_ERROR_SIZE]= { 0 };
  1019. ERR_error_string_n(ssl_error, errorString, sizeof(errorString));
  1020. close_socket();
  1021. return gearman_universal_set_error(universal, GEARMAN_LOST_CONNECTION, GEARMAN_AT, "SSL failure(%s)", errorString);
  1022. }
  1023. }
  1024. }
  1025. else
  1026. #endif // defined(HAVE_SSL)
  1027. {
  1028. read_size= ::recv(fd, data, data_size, MSG_NOSIGNAL);
  1029. }
  1030. if (read_size == 0)
  1031. {
  1032. ret= gearman_error(universal, GEARMAN_LOST_CONNECTION, "lost connection to server (EOF)");
  1033. close_socket();
  1034. return 0;
  1035. }
  1036. else if (read_size == SOCKET_ERROR)
  1037. {
  1038. if (errno == EAGAIN or errno == ENOTCONN)
  1039. {
  1040. set_events(POLLIN);
  1041. if (universal.is_non_blocking())
  1042. {
  1043. ret= gearman_gerror(universal, GEARMAN_IO_WAIT);
  1044. return 0;
  1045. }
  1046. ret= gearman_wait(universal);
  1047. if (gearman_failed(ret))
  1048. {
  1049. if (ret == GEARMAN_SHUTDOWN)
  1050. {
  1051. close_socket();
  1052. }
  1053. return 0;
  1054. }
  1055. continue;
  1056. }
  1057. else if (errno == EINTR)
  1058. {
  1059. continue;
  1060. }
  1061. else if (errno == EPIPE or errno == ECONNRESET or errno == EHOSTDOWN)
  1062. {
  1063. ret= gearman_perror(universal, errno, "lost connection to server during read");
  1064. }
  1065. else
  1066. {
  1067. ret= gearman_perror(universal, errno, "recv");
  1068. }
  1069. close_socket();
  1070. return 0;
  1071. }
  1072. break;
  1073. }
  1074. ret= GEARMAN_SUCCESS;
  1075. return size_t(read_size);
  1076. }
  1077. void gearman_connection_st::set_events(short arg)
  1078. {
  1079. if ((_events | arg) == _events)
  1080. {
  1081. return;
  1082. }
  1083. _events|= arg;
  1084. }
  1085. void gearman_connection_st::set_revents(short arg)
  1086. {
  1087. if (arg)
  1088. {
  1089. options.ready= true;
  1090. }
  1091. _revents= arg;
  1092. _events&= short(~arg);
  1093. }
  1094. /*
  1095. * Static Definitions
  1096. */
  1097. gearman_return_t gearman_connection_st::set_socket_options()
  1098. {
  1099. if (SOCK_CLOEXEC == 0)
  1100. {
  1101. if (FD_CLOEXEC)
  1102. {
  1103. int flags;
  1104. do
  1105. {
  1106. flags= fcntl(fd, F_GETFD, 0);
  1107. } while (flags == -1 and (errno == EINTR or errno == EAGAIN));
  1108. if (flags != -1)
  1109. {
  1110. int rval;
  1111. do
  1112. {
  1113. rval= fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
  1114. } while (rval == -1 && (errno == EINTR or errno == EAGAIN));
  1115. if (rval == -1)
  1116. {
  1117. gearman_perror(universal, errno, "fcntl (fd, F_SETFD, flags | FD_CLOEXEC)");
  1118. }
  1119. }
  1120. else
  1121. {
  1122. gearman_perror(universal, errno, "fcntl(fd, F_GETFD, 0)");
  1123. }
  1124. }
  1125. }
  1126. {
  1127. int ret= 1;
  1128. ret= setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &ret,
  1129. socklen_t(sizeof(int)));
  1130. if (ret == -1 && errno != EOPNOTSUPP)
  1131. {
  1132. return gearman_perror(universal, errno, "setsockopt(TCP_NODELAY)");
  1133. }
  1134. }
  1135. {
  1136. struct linger linger;
  1137. linger.l_onoff= 1;
  1138. linger.l_linger= GEARMAN_DEFAULT_SOCKET_TIMEOUT;
  1139. int ret= setsockopt(fd, SOL_SOCKET, SO_LINGER, &linger,
  1140. socklen_t(sizeof(struct linger)));
  1141. if (ret == -1)
  1142. {
  1143. return gearman_perror(universal, errno, "setsockopt(SO_LINGER)");
  1144. }
  1145. }
  1146. #if 0
  1147. if (0)
  1148. {
  1149. struct timeval waittime;
  1150. waittime.tv_sec= GEARMAN_DEFAULT_SOCKET_TIMEOUT;
  1151. waittime.tv_usec= 0;
  1152. int ret= setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &waittime,
  1153. socklen_t(sizeof(struct timeval)));
  1154. if (ret == -1 && errno != ENOPROTOOPT)
  1155. {
  1156. gearman_perror(universal, errno, "setsockopt(SO_SNDTIMEO)");
  1157. return GEARMAN_ERRNO;
  1158. }
  1159. ret= setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &waittime,
  1160. socklen_t(sizeof(struct timeval)));
  1161. if (ret == -1 && errno != ENOPROTOOPT)
  1162. {
  1163. gearman_perror(universal, errno, "setsockopt(SO_RCVTIMEO)");
  1164. return GEARMAN_ERRNO;
  1165. }
  1166. }
  1167. #endif
  1168. {
  1169. int optval= 1;
  1170. int ret= setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval));
  1171. if (ret == -1 && errno != ENOPROTOOPT)
  1172. {
  1173. return gearman_perror(universal, errno, "setsockopt(SO_KEEPALIVE)");
  1174. }
  1175. }
  1176. {
  1177. int ret= GEARMAN_DEFAULT_SOCKET_SEND_SIZE;
  1178. ret= setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &ret, socklen_t(sizeof(int)));
  1179. if (ret == -1)
  1180. {
  1181. return gearman_perror(universal, errno, "setsockopt(SO_SNDBUF)");
  1182. }
  1183. }
  1184. #if defined(SO_NOSIGPIPE)
  1185. if (SO_NOSIGPIPE)
  1186. {
  1187. int ret= 1;
  1188. ret= setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, static_cast<void *>(&ret), sizeof(int));
  1189. // This is not considered a fatal error
  1190. if (ret == -1)
  1191. {
  1192. gearman_perror(universal, errno, "setsockopt(SO_NOSIGPIPE)");
  1193. }
  1194. }
  1195. #endif
  1196. {
  1197. int ret= GEARMAN_DEFAULT_SOCKET_RECV_SIZE;
  1198. ret= setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &ret, socklen_t(sizeof(int)));
  1199. if (ret == -1)
  1200. {
  1201. return gearman_perror(universal, errno, "setsockopt(SO_RCVBUF)");
  1202. }
  1203. }
  1204. // If SOCK_NONBLOCK doesn't work, just enable non_block via fcntl
  1205. if (SOCK_NONBLOCK == 0)
  1206. {
  1207. int flags;
  1208. do
  1209. {
  1210. flags= fcntl(fd, F_GETFL, 0);
  1211. } while (flags == -1 and (errno == EINTR or errno == EAGAIN));
  1212. if (flags == -1)
  1213. {
  1214. return gearman_perror(universal, errno, "fcntl(F_GETFL)");
  1215. }
  1216. else if ((flags & O_NONBLOCK) == 0)
  1217. {
  1218. int retval;
  1219. do
  1220. {
  1221. retval= fcntl(fd, F_SETFL, flags | O_NONBLOCK);
  1222. } while (retval == -1 and (errno == EINTR or errno == EAGAIN));
  1223. if (retval == -1)
  1224. {
  1225. return gearman_perror(universal, errno, "fcntl(F_SETFL)");
  1226. }
  1227. }
  1228. }
  1229. return GEARMAN_SUCCESS;
  1230. }