~brianaker/libmemcached/1164440

« back to all changes in this revision

Viewing changes to libtest/server.cc

  • Committer: Continuous Integration
  • Date: 2012-04-03 15:33:28 UTC
  • mfrom: (991.4.1 workspace)
  • mto: This revision was merged to the branch mainline in revision 992.
  • Revision ID: ci@tangent.org-20120403153328-c3hchrmd2bvfxayb
Merge working tree with build tree.

Show diffs side-by-side

added added

removed removed

Lines of Context:
76
76
 
77
77
#define MAGIC_MEMORY 123570
78
78
 
79
 
Server::Server(const std::string& host_arg, const in_port_t port_arg, bool is_socket_arg) :
 
79
Server::Server(const std::string& host_arg, const in_port_t port_arg,
 
80
               const std::string& executable, const bool _is_libtool,
 
81
               bool is_socket_arg) :
80
82
  _magic(MAGIC_MEMORY),
81
83
  _is_socket(is_socket_arg),
82
 
  _pid(-1),
83
84
  _port(port_arg),
84
 
  _hostname(host_arg)
 
85
  _hostname(host_arg),
 
86
  _app(executable, _is_libtool)
85
87
{
86
88
}
87
89
 
88
90
Server::~Server()
89
91
{
90
 
  if (has_pid() and not kill(_pid))
91
 
  {
92
 
    Error << "Unable to kill:" << *this;
93
 
  }
 
92
}
 
93
 
 
94
bool Server::check()
 
95
{
 
96
  _app.slurp();
 
97
  _app.check();
 
98
  return true;
94
99
}
95
100
 
96
101
bool Server::validate()
104
109
  uint32_t limit= 3;
105
110
 
106
111
  // Try to ping, and kill the server #limit number of times
107
 
  pid_t current_pid;
108
112
  while (--limit and 
109
 
         is_pid_valid(current_pid= get_pid()))
 
113
         is_pid_valid(_app.pid()))
110
114
  {
111
 
    if (kill(current_pid))
 
115
    if (kill())
112
116
    {
113
 
      Log << "Killed existing server," << *this << " with pid:" << current_pid;
 
117
      Log << "Killed existing server," << *this;
114
118
      dream(0, 50000);
115
119
      continue;
116
120
    }
119
123
  // For whatever reason we could not kill it, and we reached limit
120
124
  if (limit == 0)
121
125
  {
122
 
    Error << "Reached limit, could not kill server pid:" << current_pid;
 
126
    Error << "Reached limit, could not kill server";
123
127
    return false;
124
128
  }
125
129
 
133
137
  return wait.successful();
134
138
}
135
139
 
 
140
bool Server::has_pid() const
 
141
{
 
142
  return (_app.pid() > 1);
 
143
}
 
144
 
 
145
 
136
146
bool Server::start()
137
147
{
138
148
  // If we find that we already have a pid then kill it.
139
 
  if (has_pid() and kill(_pid) == false)
140
 
  {
141
 
    Error << "Could not kill() existing server during start() pid:" << _pid;
142
 
    return false;
143
 
  }
144
 
 
145
 
  if (has_pid() == false)
 
149
  if (has_pid() == true)
146
150
  {
147
151
    fatal_message("has_pid() failed, programer error");
148
152
  }
149
153
 
150
 
  Application app(executable(), is_libtool());
151
 
 
152
 
  if (is_debug())
153
 
  {
154
 
    app.use_gdb();
155
 
  }
156
 
  else if (getenv("TESTS_ENVIRONMENT"))
157
 
  {
158
 
    if (strstr(getenv("TESTS_ENVIRONMENT"), "gdb"))
159
 
    {
160
 
      app.use_gdb();
161
 
    }
162
 
  }
163
 
 
164
 
  if (args(app) == false)
 
154
  // This needs more work.
 
155
#if 0
 
156
  if (gdb_is_caller())
 
157
  {
 
158
    _app.use_gdb();
 
159
  }
 
160
#endif
 
161
 
 
162
  if (getenv("YATL_VALGRIND_SERVER"))
 
163
  {
 
164
    _app.use_valgrind();
 
165
  }
 
166
  else if (args(_app) == false)
165
167
  {
166
168
    Error << "Could not build command()";
167
169
    return false;
168
170
  }
169
171
 
170
172
  Application::error_t ret;
171
 
  if (Application::SUCCESS !=  (ret= app.run()))
 
173
  if (Application::SUCCESS !=  (ret= _app.run()))
172
174
  {
173
175
    Error << "Application::run() " << ret;
174
176
    return false;
175
177
  }
176
 
  _running= app.print();
177
 
 
178
 
  if (Application::SUCCESS !=  (ret= app.wait()))
179
 
  {
180
 
    Error << "Application::wait() " << _running << " " << ret;
181
 
    return false;
182
 
  }
183
 
 
184
 
  if (is_valgrind())
 
178
  _running= _app.print();
 
179
 
 
180
  if (valgrind_is_caller())
185
181
  {
186
182
    dream(5, 50000);
187
183
  }
192
188
 
193
189
    if (wait.successful() == false)
194
190
    {
195
 
      libtest::fatal(LIBYATL_DEFAULT_PARAM,
196
 
                     "Unable to open pidfile for: %s",
197
 
                     _running.c_str());
 
191
      throw libtest::fatal(LIBYATL_DEFAULT_PARAM,
 
192
                           "Unable to open pidfile for: %s",
 
193
                           _running.c_str());
198
194
    }
199
195
  }
200
196
 
 
197
  uint32_t this_wait;
201
198
  bool pinged= false;
202
199
  {
203
200
    uint32_t timeout= 20; // This number should be high enough for valgrind startup (which is slow)
204
201
    uint32_t waited;
205
 
    uint32_t this_wait;
206
202
    uint32_t retry;
207
203
 
208
204
    for (waited= 0, retry= 1; ; retry++, waited+= this_wait)
228
224
    {
229
225
      if (kill_file(pid_file()) == false)
230
226
      {
231
 
        fatal_message("Failed to kill off server after startup occurred, when pinging failed");
 
227
        throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Failed to kill off server after startup occurred, when pinging failed: %s", pid_file().c_str());
232
228
      }
233
 
      Error << "Failed to ping() server started, having pid_file. exec:" << _running;
 
229
      Error << "Failed to ping(), waited:" << this_wait 
 
230
        << " server started, having pid_file. exec:" << _running 
 
231
        << " error:" << _app.stderr_result();
234
232
    }
235
233
    else
236
234
    {
240
238
    return false;
241
239
  }
242
240
 
243
 
  // A failing get_pid() at this point is considered an error
244
 
  _pid= get_pid(true);
245
 
 
246
241
  return has_pid();
247
242
}
248
243
 
250
245
{
251
246
  _running.clear();
252
247
  _pid_file.clear();
253
 
  _pid= -1;
254
248
}
255
249
 
256
 
pid_t Server::pid()
 
250
pid_t Server::pid() const
257
251
{
258
 
  return _pid;
 
252
  return _app.pid();
259
253
}
260
254
 
261
255
void Server::add_option(const std::string& arg)
333
327
  int fd;
334
328
  if ((fd= mkstemp(file_buffer)) == -1)
335
329
  {
336
 
    libtest::fatal(LIBYATL_DEFAULT_PARAM, "mkstemp() failed on %s with %s", file_buffer, strerror(errno));
 
330
    throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "mkstemp() failed on %s with %s", file_buffer, strerror(errno));
337
331
  }
338
332
  close(fd);
339
333
 
346
340
{
347
341
 
348
342
  // Set a log file if it was requested (and we can)
349
 
  if (has_log_file_option())
 
343
  if (false and has_log_file_option())
350
344
  {
351
345
    set_log_file();
352
346
    log_file_option(app, _log_file);
367
361
    pid_file_option(app, pid_file());
368
362
  }
369
363
 
370
 
  assert(daemon_file_option());
371
 
  if (daemon_file_option() and not is_valgrind() and not is_helgrind())
372
 
  {
373
 
    app.add_option(daemon_file_option());
374
 
  }
375
 
 
376
364
  if (has_socket_file_option())
377
365
  {
378
366
    if (set_socket_file() == false)
403
391
  return true;
404
392
}
405
393
 
406
 
bool Server::is_debug() const
407
 
{
408
 
  return bool(getenv("LIBTEST_MANUAL_GDB"));
409
 
}
410
 
 
411
 
bool Server::is_valgrind() const
412
 
{
413
 
  return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
414
 
}
415
 
 
416
 
bool Server::is_helgrind() const
417
 
{
418
 
  return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
419
 
}
420
 
 
421
 
bool Server::kill(pid_t pid_arg)
422
 
{
423
 
  if (check_pid(pid_arg) and kill_pid(pid_arg)) // If we kill it, reset
 
394
bool Server::kill()
 
395
{
 
396
  if (check_pid(_app.pid())) // If we kill it, reset
424
397
  {
 
398
    _app.murder();
425
399
    if (broken_pid_file() and pid_file().empty() == false)
426
400
    {
427
401
      unlink(pid_file().c_str());