1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
|
/*---------------------------------------------------------------------------*\
NAME
daemonize - run a command as a Unix daemon
DESCRIPTION
See accompanying man page for full details.
LICENSE
This source code is released under a BSD-style license. See the
LICENSE file for details.
Copyright (c) 2003-2010 Brian M. Clapper, bmc@clapper.org
\*---------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <grp.h>
#include <pwd.h>
#include <stdarg.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <libgen.h>
#include "config.h"
#include "version.h"
/*---------------------------------------------------------------------------*\
Globals
\*---------------------------------------------------------------------------*/
static const char *pid_file = NULL;
static const char *out_file = NULL;
static const char *err_file = NULL;
static const char *lock_file = NULL;
static bool be_verbose = FALSE;
static const char *user = NULL;
static char **cmd = NULL;
static const char *cwd = "/";
static int null_fd = -1;
static int out_fd = -1;
static int err_fd = -1;
static int append = 0;
/*---------------------------------------------------------------------------*\
Private Functions
\*---------------------------------------------------------------------------*/
/**
* Akin to perl's die() function, this function prints the specified message
* and exits the program with a non-zero error code.
*
* Parameters:
* format - a printf format string
* ... - any additional arguments as necessary to satisfy the format
*/
static void die(const char *format, ...)
{
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
exit(1);
}
/**
* Emits a message to standard output if the be_verbose flag is set.
*
* Parameters:
* format - a printf format string
* ... - any additional arguments as necessary to satisfy the format
*/
static void verbose(const char *format, ...)
{
va_list ap;
if (be_verbose)
{
va_start(ap, format);
vfprintf(stdout, format, ap);
va_end(ap);
}
}
/**
* Emit the usage message and abort the program with a non-zero exit code.
*
* Parameters:
* prog - name of program, from argv[0]
*/
static void usage(char *prog)
{
static const char *USAGE[] =
{
"Usage: %s [OPTIONS] path [arg] ...",
"",
"OPTIONS",
"",
"-a Append to, instead of overwriting, output files. Ignored ",
" unless -e and/or -o are specified.",
"-c <dir> Set daemon's working directory to <dir>.",
"-e <stderr> Send daemon's stderr to file <stderr>, instead of /dev/null.",
"-E var=value Pass environment setting to daemon. May appear multiple times.",
"-o <stdout> Send daemon's stdout to file <stdout>, instead of /dev/null.",
"-p <pidfile> Save PID to <pidfile>.",
"-u <user> Run daemon as user <user>. Requires invocation as root.",
"-l <lockfile> Single-instance checking using lockfile <lockfile>.",
"-v Issue verbose messages to stdout while daemonizing."
};
int i;
prog = basename(prog);
fprintf(stderr, "%s, version %s\n", prog, VERSION);
for (i = 0; i < sizeof(USAGE) / sizeof(const char *); i++)
{
fprintf(stderr, USAGE[i], prog);
fputc('\n', stderr);
}
exit(1);
}
/**
* Add a string of the name "name=value" to the environment. Aborts on error.
*
* Parameters:
* opt - option character, for errors
* envvar - string of the form name=value
*/
static void add_to_env(char opt, const char *envvar)
{
char *eq = strchr(envvar, '=');
if (eq == NULL)
die("Argument to -%c (\"%s\") is not of the form name=value.\n",
opt, envvar);
/* Split the string into its name/value substrings. */
int name_len = (int) (eq - envvar);
int val_len = (strlen(envvar) - name_len) - 1;
char *name = (char *) malloc(name_len + 1);
char *value = (char *) malloc(val_len + 1);
*name = '\0';
*value = '\0';
(void) strncat(name, envvar, name_len);
eq++;
(void) strncat(value, eq, val_len);
setenv(name, value, 1);
free(name);
free(value);
}
/**
* Parse the command-line parameters, setting the various globals that are
* affected by them.
*
* Parameters:
* argc - argument count, as passed to main()
* argv - argument vector, as passed to main()
*/
static void parse_params(int argc, char **argv)
{
int opt;
int argsLeft;
opterr = 0;
/*
NOTE: x_getopt() is the old public domain getopt(). The source lives
in "getopt.c". The function's name has been changed to avoid
conflicts with the native getopt() on the host operating system. So
far, I've seen two kinds of conflicts:
1. GNU getopt() (e.g., on Linux systems) behaves differently from the
old getopt(), unless POSIXLY_CORRECT is defined in the
environment. Specifically, it insists on processing options even
after the first non-option argument has been seen on the command
line. Initially, I got around this problem by forcing the use of
the included public domain getopt() function.
2. The types used in the included public domain getopt() conflict with
the types of the native getopt() on some operating systems (e.g.,
Solaris 8).
Using x_getopt() ensures that daemonize uses its own version, which
always behaves consistently.
*/
while ( (opt = x_getopt(argc, argv, "ac:u:p:vo:e:E:l:")) != -1)
{
switch (opt)
{
case 'a':
append = 1;
break;
case 'c':
cwd = x_optarg;
break;
case 'p':
pid_file = x_optarg;
break;
case 'v':
be_verbose = TRUE;
break;
case 'u':
user = x_optarg;
break;
case 'o':
out_file = x_optarg;
break;
case 'e':
err_file = x_optarg;
break;
case 'l':
lock_file = x_optarg;
break;
case 'E':
add_to_env('E', x_optarg);
break;
default:
fprintf(stderr, "Bad option: -%c\n", x_optopt);
usage(argv[0]);
}
}
argsLeft = argc - x_optind;
if (argsLeft < 1)
usage(argv[0]);
cmd = &argv[x_optind];
return;
}
/**
* Switch the process's effective and real user IDs to the ID associated with
* the specified user name. Also switches to that user's group ID to the
* primary group associated with the user.
*
* This function calls die() if anything fails.
*
* Parameters:
* user_name - name of user to which to switch
* uid - current process user ID
* pid_file - if non-NULL, specifies PID file path, ownership of which
* should be changed to the user
*/
static void switch_user(const char *user_name, uid_t uid, const char *pid_file)
{
struct passwd *pw;
if (uid != 0)
die("Must be root to specify a different user.\n");
if ( (pw = getpwnam(user_name)) == NULL )
die("Can't find user \"%s\" in password file.\n", user_name);
if (setgid(pw->pw_gid) != 0)
die("Can't set gid to %d: %s\n", pw->pw_gid, strerror (errno));
/*
For systems supporting multiple group memberships, make sure ALL
groups are added to the process, just in case someone depends on them.
Patch by Ken Farnen <kenf@14Cubed.COM>, 18 February 2010. Modified
to put the #ifdef in the config.h header, rather than in here.
*/
if (initgroups(pw->pw_name, pw->pw_gid) == -1)
die("Can't initialize secondary groups for \"%s\": %s\n",
pw->pw_name, strerror (errno));
if (pid_file != NULL)
{
verbose("Changing ownership of PID file to \"%s\" (%d)\n",
user, pw->pw_uid);
if (chown(pid_file, pw->pw_uid, pw->pw_gid) == -1)
{
die("Can't change owner of PID file \"%s\" to \"%s\" (%d): %s\n",
pid_file, user, pw->pw_uid, strerror (errno));
}
}
if (setegid(pw->pw_gid) != 0)
die("Can't set egid to %d: %s\n", pw->pw_gid, strerror (errno));
if (setuid(pw->pw_uid) != 0)
die("Can't set uid to %d: %s\n", pw->pw_uid, strerror (errno));
if (seteuid(pw->pw_uid) != 0)
die("Can't set euid to %d: %s\n", pw->pw_uid, strerror (errno));
/*
Initialize environment to match new username.
Patch by Ken Farnen <kenf@14Cubed.COM>, 18 February 2010.
*/
setenv("USER", pw->pw_name,1);
setenv("LOGNAME", pw->pw_name,1);
setenv("HOME", pw->pw_dir,1);
}
/**
* Open the specified output file. If the global "append" variable is set,
* the file is opened in append mode; otherwise, it is overwritten.
*
* Parameters:
* path - path to the output file
*
* Returns:
* the file descriptor for the open file, or -1 on error.
*/
static int open_output_file(const char *path)
{
int flags = O_CREAT | O_WRONLY;
if (append)
{
verbose("Appending to %s\n", path);
flags |= O_APPEND;
}
else
{
verbose("Overwriting %s\n", path);
flags |= O_TRUNC;
}
return open(path, flags, 0666);
}
/**
* Opens all output files.
*/
static void open_output_files()
{
/* open files for stdout/stderr */
if ((out_file != NULL) || (err_file != NULL))
{
if ((null_fd = open("/dev/null", O_WRONLY)) == -1)
die("Can't open /dev/null: %s\n", strerror (errno));
close(STDIN_FILENO);
dup2(null_fd, STDIN_FILENO);
if (out_file != NULL)
{
if ((out_fd = open_output_file(out_file)) == -1)
{
die("Can't open \"%s\" for stdout: %s\n",
out_file, strerror(errno));
}
}
else
{
out_fd = null_fd;
}
if (err_file != NULL)
{
if ((out_file != NULL) && (strcmp(err_file, out_file) == 0))
err_fd = out_fd;
else if ((err_fd = open_output_file(err_file)) == -1)
{
die("Can't open \"%s\" for stderr: %s\n",
err_file, strerror (errno));
}
}
else
{
err_fd = null_fd;
}
}
}
/**
* Redirects standard output and error to someplace safe.
*/
static int redirect_stdout_stderr()
{
int rc = 0;
/* Redirect stderr/stdout */
if ((out_file != NULL) || (err_file != NULL))
{
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
dup2(null_fd, STDIN_FILENO);
dup2(out_fd, STDOUT_FILENO);
dup2(err_fd, STDERR_FILENO);
rc = 1;
}
return rc;
}
/*---------------------------------------------------------------------------*\
Main Program
\*---------------------------------------------------------------------------*/
int main(int argc, char **argv)
{
uid_t uid = getuid();
int noclose = 0;
int lockFD;
struct stat st;
if (geteuid() != uid)
die("This executable is too dangerous to be setuid.\n");
parse_params(argc, argv);
if (cmd[0][0] != '/')
die("The 'path' parameter must be an absolute path name.\n");
/* Verify that the path to the command points to an existing file. */
if (access(cmd[0], X_OK) == -1)
die("File \"%s\" is not executable.\n", cmd[0]);
/*
Note: A directory will also pass the X_OK test, so test further with
stat().
*/
if (stat(cmd[0], &st))
die("File \"%s\" does not exist.\n", cmd[0]);
if(! S_ISREG(st.st_mode))
die("File \"%s\" is not regular file.\n", cmd[0]);
if (lock_file)
{
lockFD = open(lock_file, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
if (lockFD < 0)
die("Can't create lock file \"%s\": %s\n",
lock_file, strerror (errno));
if (flock(lockFD, LOCK_EX | LOCK_NB) != 0)
die("Can't lock the lock file \"%s\". "
"Is another instance running?\n",
lock_file);
}
if (pid_file != NULL)
{
int fd;
verbose("Creating PID file \"%s\".\n", pid_file);
fd = open(pid_file, O_CREAT | O_WRONLY,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd < 0)
{
die ("Can't create PID file \"%s\": %s\n",
pid_file, strerror (errno));
}
close(fd);
}
if (user != NULL)
switch_user(user, uid, pid_file);
open_output_files();
if (chdir (cwd) != 0)
{
die("Can't change working directory to \"%s\": %s\n",
cwd, strerror (errno));
}
verbose("Daemonizing...");
if (redirect_stdout_stderr())
noclose = 1;
if (daemon (1, noclose) != 0)
die("Can't daemonize: %s\n", strerror (errno));
if (chdir(cwd) != 0)
{
die("Can't change working directory to \"%s\": %s\n",
cwd, strerror (errno));
}
if (pid_file != NULL)
{
FILE *fPid = NULL;
verbose("Writing process ID to \"%s\".\n", pid_file);
if ( (fPid = fopen (pid_file, "w")) == NULL )
{
die("Can't open PID file \"%s\": %s\n",
pid_file, strerror (errno));
}
fprintf(fPid, "%d\n", getpid());
fclose(fPid);
}
/*
Make sure we have a relatively sane environment
Patch by Ken Farnen <kenf@14Cubed.COM>, 18 February 2010.
*/
if (getenv("IFS") == NULL)
setenv("IFS"," \t\n",1);
if (getenv("PATH") == NULL)
setenv("PATH","/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin", 1);
execvp(cmd[0], cmd);
die("Can't exec \"%s\": %s\n", cmd[0], strerror (errno));
}
/* vim: set et sw=4 sts=4 : */
|