~mm-yuhu/gearmand/server-funcs

« back to all changes in this revision

Viewing changes to bin/gearman.c

  • Committer: Brian Aker
  • Date: 2010-02-09 22:15:41 UTC
  • Revision ID: brian@gaz-20100209221541-6e647rjessi2vm54
Added pid/daemon options to gearman client (and test).

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
 */
13
13
 
14
14
#include <errno.h>
 
15
#include <fcntl.h>
15
16
#include <signal.h>
16
17
#include <stdio.h>
17
18
#include <stdlib.h>
18
19
#include <string.h>
 
20
#include <sys/stat.h>
 
21
#include <sys/types.h>
 
22
#include <sys/wait.h>
19
23
#include <unistd.h>
20
 
#include <sys/wait.h>
 
24
 
21
25
 
22
26
#include <libgearman/gearman.h>
23
27
 
47
51
  bool suppress_input;
48
52
  bool prefix;
49
53
  bool background;
 
54
  bool daemon;
50
55
  gearman_job_priority_t priority;
51
56
  int timeout;
52
57
  char **argv;
53
58
  gearman_task_st *task;
54
59
  char return_value;
 
60
  char *pid_file;
55
61
} gearman_args_st;
56
62
 
57
63
/**
106
112
 */
107
113
static void usage(char *name);
108
114
 
 
115
/*
 
116
  Pid file functions.
 
117
*/
 
118
static bool _pid_write(const char *pid_file)
 
119
{
 
120
  FILE *f;
 
121
 
 
122
  f= fopen(pid_file, "w");
 
123
  if (f == NULL)
 
124
  {
 
125
    fprintf(stderr, "gearmand: Could not open pid file for writing: %s (%d)\n",
 
126
            pid_file, errno);
 
127
    return true;
 
128
  }
 
129
 
 
130
  fprintf(f, "%" PRId64 "\n", (int64_t)getpid());
 
131
 
 
132
  if (fclose(f) == -1)
 
133
  {
 
134
    fprintf(stderr, "gearmand: Could not close the pid file: %s (%d)\n",
 
135
            pid_file, errno);
 
136
    return true;
 
137
  }
 
138
 
 
139
  return false;
 
140
}
 
141
 
 
142
static void _pid_delete(const char *pid_file)
 
143
{
 
144
  if (unlink(pid_file) == -1)
 
145
  {
 
146
    fprintf(stderr, "gearmand: Could not remove the pid file: %s (%d)\n",
 
147
            pid_file, errno);
 
148
  }
 
149
}
 
150
 
109
151
int main(int argc, char *argv[])
110
152
{
111
153
  int c;
112
154
  gearman_args_st args;
 
155
  bool close_stdio= false;
113
156
 
114
157
  memset(&args, 0, sizeof(gearman_args_st));
115
158
  args.priority= GEARMAN_JOB_PRIORITY_NORMAL;
120
163
  if (args.function == NULL)
121
164
    GEARMAN_ERROR("malloc:%d", errno)
122
165
 
123
 
  while ((c = getopt(argc, argv, "bc:f:h:HILnNp:Pst:u:w")) != -1)
 
166
  while ((c = getopt(argc, argv, "bc:f:h:HILnNp:Pst:u:wi:d")) != -1)
124
167
  {
125
168
    switch(c)
126
169
    {
132
175
      args.count= (uint32_t)atoi(optarg);
133
176
      break;
134
177
 
 
178
    case 'd':
 
179
      args.daemon= true;
 
180
      break;
 
181
 
135
182
    case 'f':
136
183
      args.function[args.function_count]= optarg;
137
184
      args.function_count++;
141
188
      args.host= optarg;
142
189
      break;
143
190
 
 
191
    case 'i':
 
192
      args.pid_file= strdup(optarg);
 
193
      break;
 
194
 
144
195
    case 'I':
145
196
      args.priority= GEARMAN_JOB_PRIORITY_HIGH;
146
197
      break;
185
236
    case 'H':
186
237
    default:
187
238
      usage(argv[0]);
188
 
      exit(1);
 
239
      exit(0);
189
240
    }
190
241
  }
191
242
 
192
243
  args.argv= argv + optind;
193
244
 
194
245
  if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
195
 
    GEARMAN_ERROR("signal:%d", errno)
 
246
    GEARMAN_ERROR("signal:%d", errno);
 
247
 
 
248
  if (args.daemon)
 
249
  {
 
250
    switch (fork())
 
251
    {
 
252
    case -1:
 
253
      fprintf(stderr, "gearmand: fork:%d\n", errno);
 
254
      return 1;
 
255
 
 
256
    case 0:
 
257
      break;
 
258
 
 
259
    default:
 
260
      return 0;
 
261
    }
 
262
 
 
263
    if (setsid() == -1)
 
264
    {
 
265
      fprintf(stderr, "gearmand: setsid:%d\n", errno);
 
266
      return 1;
 
267
    }
 
268
 
 
269
    close_stdio= true;
 
270
  }
 
271
 
 
272
  if (close_stdio)
 
273
  {
 
274
    int fd;
 
275
 
 
276
    /* If we can't remap stdio, it should not a fatal error. */
 
277
    fd= open("/dev/null", O_RDWR, 0);
 
278
 
 
279
    if (fd != -1)
 
280
    {
 
281
      if (dup2(fd, STDIN_FILENO) == -1)
 
282
      {
 
283
        fprintf(stderr, "gearmand: dup2:%d\n", errno);
 
284
        return 1;
 
285
      }
 
286
 
 
287
      if (dup2(fd, STDOUT_FILENO) == -1)
 
288
      {
 
289
        fprintf(stderr, "gearmand: dup2:%d\n", errno);
 
290
        return 1;
 
291
      }
 
292
 
 
293
      if (dup2(fd, STDERR_FILENO) == -1)
 
294
      {
 
295
        fprintf(stderr, "gearmand: dup2:%d\n", errno);
 
296
        return 1;
 
297
      }
 
298
 
 
299
      close(fd);
 
300
    }
 
301
  }
 
302
 
 
303
  if (args.pid_file != NULL && _pid_write(args.pid_file))
 
304
    return 1;
196
305
 
197
306
  if (args.worker)
198
307
    _worker(&args);
199
308
  else
200
309
    _client(&args);
201
310
 
 
311
  if (args.pid_file != NULL)
 
312
  {
 
313
    _pid_delete(args.pid_file);
 
314
    free(args.pid_file);
 
315
  }
 
316
 
202
317
  return args.return_value;
203
318
}
204
319
 
621
736
  printf("\t-H            - Print this help menu\n");
622
737
  printf("\t-p <port>     - Job server port\n");
623
738
  printf("\t-t <timeout>  - Timeout in milliseconds\n");
 
739
  printf("\t-i <pidfile>  - Create a pidfile for the process\n");
624
740
 
625
741
  printf("\nClient options:\n");
626
742
  printf("\t-b            - Run jobs in the background\n");