~clint-fewbar/ubuntu/precise/gearmand/drop-unneeded-patches

« back to all changes in this revision

Viewing changes to .pc/debian_patches_fix_spelling.patch/libgearman-server/packet.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2009-09-28 21:43:31 UTC
  • mto: (1.2.3 upstream) (6.1.1 sid)
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: james.westby@ubuntu.com-20090928214331-9bku0d3v1b1ypgp4
ImportĀ upstreamĀ versionĀ 0.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Gearman server and library
2
 
 * Copyright (C) 2008 Brian Aker, Eric Day
3
 
 * All rights reserved.
4
 
 *
5
 
 * Use and distribution licensed under the BSD license.  See
6
 
 * the COPYING file in the parent directory for full text.
7
 
 */
8
 
 
9
 
/**
10
 
 * @file
11
 
 * @brief Server connection definitions
12
 
 */
13
 
 
14
 
#include <libgearman-server/common.h>
15
 
 
16
 
#define GEARMAN_CORE
17
 
#include <libgearman/command.h>
18
 
 
19
 
#include <libgearman-server/fifo.h>
20
 
#include <assert.h>
21
 
#include <cstring>
22
 
 
23
 
#ifndef __INTEL_COMPILER
24
 
#pragma GCC diagnostic ignored "-Wold-style-cast"
25
 
#endif
26
 
 
27
 
/*
28
 
 * Public definitions
29
 
 */
30
 
 
31
 
gearman_server_packet_st *
32
 
gearman_server_packet_create(gearman_server_thread_st *thread,
33
 
                             bool from_thread)
34
 
{
35
 
  gearman_server_packet_st *server_packet= NULL;
36
 
 
37
 
  if (from_thread && Server->flags.threaded)
38
 
  {
39
 
    if (thread->free_packet_count > 0)
40
 
    {
41
 
      server_packet= thread->free_packet_list;
42
 
      thread->free_packet_list= server_packet->next;
43
 
      thread->free_packet_count--;
44
 
    }
45
 
  }
46
 
  else
47
 
  {
48
 
    if (Server->free_packet_count > 0)
49
 
    {
50
 
      server_packet= Server->free_packet_list;
51
 
      Server->free_packet_list= server_packet->next;
52
 
      Server->free_packet_count--;
53
 
    }
54
 
  }
55
 
 
56
 
  if (server_packet == NULL)
57
 
  {
58
 
    server_packet= (gearman_server_packet_st *)malloc(sizeof(gearman_server_packet_st));
59
 
    if (server_packet == NULL)
60
 
    {
61
 
      gearmand_perror("malloc");
62
 
      return NULL;
63
 
    }
64
 
  }
65
 
 
66
 
  server_packet->next= NULL;
67
 
 
68
 
  return server_packet;
69
 
}
70
 
 
71
 
void gearman_server_packet_free(gearman_server_packet_st *packet,
72
 
                                gearman_server_thread_st *thread,
73
 
                                bool from_thread)
74
 
{
75
 
  if (from_thread && Server->flags.threaded)
76
 
  {
77
 
    if (thread->free_packet_count < GEARMAN_MAX_FREE_SERVER_PACKET)
78
 
    {
79
 
      packet->next= thread->free_packet_list;
80
 
      thread->free_packet_list= packet;
81
 
      thread->free_packet_count++;
82
 
    }
83
 
    else
84
 
    {
85
 
      gearmand_crazy("free");
86
 
      free(packet);
87
 
    }
88
 
  }
89
 
  else
90
 
  {
91
 
    if (Server->free_packet_count < GEARMAN_MAX_FREE_SERVER_PACKET)
92
 
    {
93
 
      packet->next= Server->free_packet_list;
94
 
      Server->free_packet_list= packet;
95
 
      Server->free_packet_count++;
96
 
    }
97
 
    else
98
 
    {
99
 
      gearmand_crazy("free");
100
 
      free(packet);
101
 
    }
102
 
  }
103
 
}
104
 
 
105
 
gearmand_error_t gearman_server_io_packet_add(gearman_server_con_st *con,
106
 
                                              bool take_data,
107
 
                                              enum gearman_magic_t magic,
108
 
                                              gearman_command_t command,
109
 
                                              const void *arg, ...)
110
 
{
111
 
  gearman_server_packet_st *server_packet;
112
 
  va_list ap;
113
 
 
114
 
  server_packet= gearman_server_packet_create(con->thread, false);
115
 
  if (not server_packet)
116
 
    return GEARMAN_MEMORY_ALLOCATION_FAILURE;
117
 
 
118
 
  gearmand_packet_init(&(server_packet->packet), magic, command);
119
 
 
120
 
  va_start(ap, arg);
121
 
 
122
 
  while (arg)
123
 
  {
124
 
    size_t arg_size= va_arg(ap, size_t);
125
 
 
126
 
    gearmand_error_t ret= gearmand_packet_create(&(server_packet->packet), arg, arg_size);
127
 
    if (gearmand_failed(ret))
128
 
    {
129
 
      va_end(ap);
130
 
      gearmand_packet_free(&(server_packet->packet));
131
 
      gearman_server_packet_free(server_packet, con->thread, false);
132
 
      return ret;
133
 
    }
134
 
 
135
 
    arg= va_arg(ap, void *);
136
 
  }
137
 
 
138
 
  va_end(ap);
139
 
 
140
 
  gearmand_error_t ret= gearmand_packet_pack_header(&(server_packet->packet));
141
 
  if (gearmand_failed(ret))
142
 
  {
143
 
    gearmand_packet_free(&(server_packet->packet));
144
 
    gearman_server_packet_free(server_packet, con->thread, false);
145
 
    return ret;
146
 
  }
147
 
 
148
 
  if (take_data)
149
 
  {
150
 
    server_packet->packet.options.free_data= true;
151
 
  }
152
 
 
153
 
  (void) pthread_mutex_lock(&con->thread->lock);
154
 
  gearmand_server_con_fifo_add(con, server_packet);
155
 
  (void) pthread_mutex_unlock(&con->thread->lock);
156
 
 
157
 
  gearman_server_con_io_add(con);
158
 
 
159
 
  return GEARMAN_SUCCESS;
160
 
}
161
 
 
162
 
void gearman_server_io_packet_remove(gearman_server_con_st *con)
163
 
{
164
 
  gearman_server_packet_st *server_packet= con->io_packet_list;
165
 
 
166
 
  gearmand_packet_free(&(server_packet->packet));
167
 
 
168
 
  (void) pthread_mutex_lock(&con->thread->lock);
169
 
  gearmand_server_con_fifo_free(con, server_packet);
170
 
  (void) pthread_mutex_unlock(&con->thread->lock);
171
 
 
172
 
  gearman_server_packet_free(server_packet, con->thread, true);
173
 
}
174
 
 
175
 
void gearman_server_proc_packet_add(gearman_server_con_st *con,
176
 
                                    gearman_server_packet_st *packet)
177
 
{
178
 
  (void) pthread_mutex_lock(&con->thread->lock);
179
 
  gearmand_server_con_fifo_proc_add(con, packet);
180
 
  (void) pthread_mutex_unlock(&con->thread->lock);
181
 
 
182
 
  gearman_server_con_proc_add(con);
183
 
}
184
 
 
185
 
gearman_server_packet_st *
186
 
gearman_server_proc_packet_remove(gearman_server_con_st *con)
187
 
{
188
 
  gearman_server_packet_st *server_packet= con->proc_packet_list;
189
 
 
190
 
  if (server_packet == NULL)
191
 
    return NULL;
192
 
 
193
 
  (void) pthread_mutex_lock(&con->thread->lock);
194
 
  gearmand_server_con_fifo_proc_free(con, server_packet);
195
 
  (void) pthread_mutex_unlock(&con->thread->lock);
196
 
 
197
 
  return server_packet;
198
 
}
199
 
 
200
 
const char *gearmand_strcommand(gearmand_packet_st *packet)
201
 
{
202
 
  assert(packet);
203
 
  return gearman_command_info(packet->command)->name;
204
 
}
205
 
 
206
 
inline static gearmand_error_t packet_create_arg(gearmand_packet_st *packet,
207
 
                                                 const void *arg, size_t arg_size)
208
 
{
209
 
  size_t offset;
210
 
 
211
 
  if (packet->argc == gearman_command_info(packet->command)->argc &&
212
 
      (not (gearman_command_info(packet->command)->data) ||
213
 
       packet->data))
214
 
  {
215
 
    gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM, "too many arguments for command(%s)", gearman_command_info(packet->command)->name);
216
 
    return GEARMAN_TOO_MANY_ARGS;
217
 
  }
218
 
 
219
 
  if (packet->argc == gearman_command_info(packet->command)->argc)
220
 
  {
221
 
    packet->data= static_cast<const char *>(arg);
222
 
    packet->data_size= arg_size;
223
 
    return GEARMAN_SUCCESS;
224
 
  }
225
 
 
226
 
  if (packet->args_size == 0 and packet->magic != GEARMAN_MAGIC_TEXT)
227
 
    packet->args_size= GEARMAN_PACKET_HEADER_SIZE;
228
 
 
229
 
  if ((packet->args_size + arg_size) < GEARMAN_ARGS_BUFFER_SIZE)
230
 
  {
231
 
    packet->args= packet->args_buffer;
232
 
  }
233
 
  else
234
 
  {
235
 
    gearmand_log_info(GEARMAN_DEFAULT_LOG_PARAM, "resizing packet buffer");
236
 
    if (packet->args == packet->args_buffer)
237
 
    {
238
 
      packet->args= (char *)malloc(packet->args_size + arg_size);
239
 
      memcpy(packet->args, packet->args_buffer, packet->args_size);
240
 
    }
241
 
    else
242
 
    {
243
 
      char *new_args= (char *)realloc(packet->args, packet->args_size + arg_size);
244
 
      if (not new_args)
245
 
      {
246
 
        gearmand_perror("realloc");
247
 
        return GEARMAN_MEMORY_ALLOCATION_FAILURE;
248
 
      }
249
 
      packet->args= new_args;
250
 
    }
251
 
  }
252
 
 
253
 
  memcpy(packet->args + packet->args_size, arg, arg_size);
254
 
  packet->args_size+= arg_size;
255
 
  packet->arg_size[packet->argc]= arg_size;
256
 
  packet->argc++;
257
 
 
258
 
  if (packet->magic == GEARMAN_MAGIC_TEXT)
259
 
  {
260
 
    offset= 0;
261
 
  }
262
 
  else
263
 
  {
264
 
    offset= GEARMAN_PACKET_HEADER_SIZE;
265
 
  }
266
 
 
267
 
  for (uint8_t x= 0; x < packet->argc; x++)
268
 
  {
269
 
    packet->arg[x]= packet->args + offset;
270
 
    offset+= packet->arg_size[x];
271
 
  }
272
 
 
273
 
  return GEARMAN_SUCCESS;
274
 
}
275
 
 
276
 
/** @} */
277
 
 
278
 
/*
279
 
 * Public Definitions
280
 
 */
281
 
 
282
 
 
283
 
void gearmand_packet_init(gearmand_packet_st *packet, enum gearman_magic_t magic, gearman_command_t command)
284
 
{
285
 
  assert(packet);
286
 
 
287
 
  packet->options.complete= false;
288
 
  packet->options.free_data= false;
289
 
 
290
 
  packet->magic= magic;
291
 
  packet->command= command;
292
 
  packet->argc= 0;
293
 
  packet->args_size= 0;
294
 
  packet->data_size= 0;
295
 
 
296
 
  packet->args= NULL;
297
 
  packet->data= NULL;
298
 
}
299
 
 
300
 
gearmand_error_t gearmand_packet_create(gearmand_packet_st *packet,
301
 
                                          const void *arg, size_t arg_size)
302
 
{
303
 
  return packet_create_arg(packet, arg, arg_size);
304
 
}
305
 
 
306
 
void gearmand_packet_free(gearmand_packet_st *packet)
307
 
{
308
 
  if (packet->args != packet->args_buffer && packet->args != NULL)
309
 
  {
310
 
    gearmand_crazy("free");
311
 
    free(packet->args);
312
 
    packet->args= NULL;
313
 
  }
314
 
 
315
 
  if (packet->options.free_data && packet->data != NULL)
316
 
  {
317
 
    gearmand_crazy("free");
318
 
    free((void *)packet->data); //@todo fix the need for the casting.
319
 
    packet->data= NULL;
320
 
  }
321
 
}
322
 
 
323
 
gearmand_error_t gearmand_packet_pack_header(gearmand_packet_st *packet)
324
 
{
325
 
  uint64_t length_64;
326
 
  uint32_t tmp;
327
 
 
328
 
  if (packet->magic == GEARMAN_MAGIC_TEXT)
329
 
  {
330
 
    packet->options.complete= true;
331
 
    return GEARMAN_SUCCESS;
332
 
  }
333
 
 
334
 
  if (packet->args_size == 0)
335
 
  {
336
 
    packet->args= packet->args_buffer;
337
 
    packet->args_size= GEARMAN_PACKET_HEADER_SIZE;
338
 
  }
339
 
 
340
 
  switch (packet->magic)
341
 
  {
342
 
  case GEARMAN_MAGIC_TEXT:
343
 
    break;
344
 
 
345
 
  case GEARMAN_MAGIC_REQUEST:
346
 
    memcpy(packet->args, "\0REQ", 4);
347
 
    break;
348
 
 
349
 
  case GEARMAN_MAGIC_RESPONSE:
350
 
    memcpy(packet->args, "\0RES", 4);
351
 
    break;
352
 
 
353
 
  default:
354
 
    gearmand_error("invalid magic value");
355
 
    return GEARMAN_INVALID_MAGIC;
356
 
  }
357
 
 
358
 
  if (packet->command == GEARMAN_COMMAND_TEXT ||
359
 
      packet->command >= GEARMAN_COMMAND_MAX)
360
 
  {
361
 
    gearmand_error("invalid command value");
362
 
    return GEARMAN_INVALID_COMMAND;
363
 
  }
364
 
 
365
 
  tmp= packet->command;
366
 
  tmp= htonl(tmp);
367
 
  memcpy(packet->args + 4, &tmp, 4);
368
 
 
369
 
  length_64= packet->args_size + packet->data_size - GEARMAN_PACKET_HEADER_SIZE;
370
 
 
371
 
  // Check for overflow on 32bit(portable?).
372
 
  if (length_64 >= UINT32_MAX || length_64 < packet->data_size)
373
 
  {
374
 
    gearmand_error("data size too too long");
375
 
    return GEARMAN_ARGUMENT_TOO_LARGE;
376
 
  }
377
 
 
378
 
  tmp= (uint32_t)length_64;
379
 
  tmp= htonl(tmp);
380
 
  memcpy(packet->args + 8, &tmp, 4);
381
 
 
382
 
  packet->options.complete= true;
383
 
 
384
 
  return GEARMAN_SUCCESS;
385
 
}
386
 
 
387
 
static gearmand_error_t gearmand_packet_unpack_header(gearmand_packet_st *packet)
388
 
{
389
 
  uint32_t tmp;
390
 
 
391
 
  if (not memcmp(packet->args, "\0REQ", 4))
392
 
  {
393
 
    packet->magic= GEARMAN_MAGIC_REQUEST;
394
 
  }
395
 
  else if (not memcmp(packet->args, "\0RES", 4))
396
 
  {
397
 
    packet->magic= GEARMAN_MAGIC_RESPONSE;
398
 
  }
399
 
  else
400
 
  {
401
 
    gearmand_error("invalid magic value");
402
 
    return GEARMAN_INVALID_MAGIC;
403
 
  }
404
 
 
405
 
  memcpy(&tmp, packet->args + 4, 4);
406
 
  packet->command= static_cast<gearman_command_t>(ntohl(tmp));
407
 
 
408
 
  if (packet->command == GEARMAN_COMMAND_TEXT ||
409
 
      packet->command >= GEARMAN_COMMAND_MAX)
410
 
  {
411
 
    gearmand_error("invalid command value");
412
 
    return GEARMAN_INVALID_COMMAND;
413
 
  }
414
 
 
415
 
  memcpy(&tmp, packet->args + 8, 4);
416
 
  packet->data_size= ntohl(tmp);
417
 
 
418
 
  return GEARMAN_SUCCESS;
419
 
}
420
 
 
421
 
size_t gearmand_packet_pack(const gearmand_packet_st *packet,
422
 
                            gearman_server_con_st *con __attribute__ ((unused)),
423
 
                            void *data, size_t data_size,
424
 
                            gearmand_error_t *ret_ptr)
425
 
{
426
 
  if (packet->args_size == 0)
427
 
  {
428
 
    *ret_ptr= GEARMAN_SUCCESS;
429
 
    return 0;
430
 
  }
431
 
 
432
 
  if (packet->args_size > data_size)
433
 
  {
434
 
    *ret_ptr= GEARMAN_FLUSH_DATA;
435
 
    return 0;
436
 
  }
437
 
 
438
 
  memcpy(data, packet->args, packet->args_size);
439
 
  *ret_ptr= GEARMAN_SUCCESS;
440
 
  return packet->args_size;
441
 
}
442
 
 
443
 
size_t gearmand_packet_unpack(gearmand_packet_st *packet,
444
 
                              gearman_server_con_st *con __attribute__ ((unused)),
445
 
                              const void *data, size_t data_size,
446
 
                              gearmand_error_t *ret_ptr)
447
 
{
448
 
  uint8_t *ptr;
449
 
  size_t used_size;
450
 
 
451
 
  if (packet->args_size == 0)
452
 
  {
453
 
    if (data_size > 0 && ((uint8_t *)data)[0] != 0)
454
 
    {
455
 
      /* Try to parse a text-based command. */
456
 
      ptr= (uint8_t *)memchr(data, '\n', data_size);
457
 
      if (ptr == NULL)
458
 
      {
459
 
        *ret_ptr= GEARMAN_IO_WAIT;
460
 
        return 0;
461
 
      }
462
 
 
463
 
      packet->magic= GEARMAN_MAGIC_TEXT;
464
 
      packet->command= GEARMAN_COMMAND_TEXT;
465
 
 
466
 
      used_size= (size_t)(ptr - ((uint8_t *)data)) + 1;
467
 
      *ptr= 0;
468
 
      if (used_size > 1 && *(ptr - 1) == '\r')
469
 
        *(ptr - 1)= 0;
470
 
 
471
 
      size_t arg_size;
472
 
      for (arg_size= used_size, ptr= (uint8_t *)data; ptr != NULL; data= ptr)
473
 
      {
474
 
        ptr= (uint8_t *)memchr(data, ' ', arg_size);
475
 
        if (ptr != NULL)
476
 
        {
477
 
          *ptr= 0;
478
 
          ptr++;
479
 
          while (*ptr == ' ')
480
 
            ptr++;
481
 
 
482
 
          arg_size-= (size_t)(ptr - ((uint8_t *)data));
483
 
        }
484
 
 
485
 
        *ret_ptr= packet_create_arg(packet, data, ptr == NULL ? arg_size :
486
 
                                    (size_t)(ptr - ((uint8_t *)data)));
487
 
        if (*ret_ptr != GEARMAN_SUCCESS)
488
 
        {
489
 
          return used_size;
490
 
        }
491
 
      }
492
 
 
493
 
      return used_size;
494
 
    }
495
 
    else if (data_size < GEARMAN_PACKET_HEADER_SIZE)
496
 
    {
497
 
      *ret_ptr= GEARMAN_IO_WAIT;
498
 
      return 0;
499
 
    }
500
 
 
501
 
    packet->args= packet->args_buffer;
502
 
    packet->args_size= GEARMAN_PACKET_HEADER_SIZE;
503
 
    memcpy(packet->args, data, GEARMAN_PACKET_HEADER_SIZE);
504
 
 
505
 
    *ret_ptr= gearmand_packet_unpack_header(packet);
506
 
    if (gearmand_failed(*ret_ptr))
507
 
    {
508
 
      return 0;
509
 
    }
510
 
 
511
 
    used_size= GEARMAN_PACKET_HEADER_SIZE;
512
 
  }
513
 
  else
514
 
  {
515
 
    used_size= 0;
516
 
  }
517
 
 
518
 
  while (packet->argc != gearman_command_info(packet->command)->argc)
519
 
  {
520
 
    if (packet->argc != (gearman_command_info(packet->command)->argc - 1) ||
521
 
        gearman_command_info(packet->command)->data)
522
 
    {
523
 
      ptr= (uint8_t *)memchr(((uint8_t *)data) + used_size, 0, data_size - used_size);
524
 
      if (not ptr)
525
 
      {
526
 
        gearmand_log_crazy(GEARMAN_DEFAULT_LOG_PARAM, "Possible protocol error for %s, recieved only %u args", gearman_command_info(packet->command)->name, packet->argc);
527
 
        *ret_ptr= GEARMAN_IO_WAIT;
528
 
        return used_size;
529
 
      }
530
 
 
531
 
      size_t arg_size= (size_t)(ptr - (((uint8_t *)data) + used_size)) + 1;
532
 
      *ret_ptr= packet_create_arg(packet, ((uint8_t *)data) + used_size, arg_size);
533
 
 
534
 
      if (gearmand_failed(*ret_ptr))
535
 
        return used_size;
536
 
 
537
 
      packet->data_size-= arg_size;
538
 
      used_size+= arg_size;
539
 
    }
540
 
    else
541
 
    {
542
 
      if ((data_size - used_size) < packet->data_size)
543
 
      {
544
 
        *ret_ptr= GEARMAN_IO_WAIT;
545
 
        return used_size;
546
 
      }
547
 
 
548
 
      *ret_ptr= packet_create_arg(packet, ((uint8_t *)data) + used_size, packet->data_size);
549
 
      if (gearmand_failed(*ret_ptr))
550
 
      {
551
 
        return used_size;
552
 
      }
553
 
 
554
 
      used_size+= packet->data_size;
555
 
      packet->data_size= 0;
556
 
    }
557
 
  }
558
 
 
559
 
  *ret_ptr= GEARMAN_SUCCESS;
560
 
  return used_size;
561
 
}