summaryrefslogtreecommitdiff
path: root/src/nohup.c
blob: 79a8c5043220021629a4cbdbee6de8308c5f429c (plain)
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
/* nohup -- run a command immume to hangups, with output to a non-tty
   Copyright (C) 2003, 2004 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

/* Written by Jim Meyering  */

#include <config.h>
#include <getopt.h>
#include <stdio.h>
#include <sys/types.h>
#include <signal.h>

#include "system.h"

#include "error.h"
#include "long-options.h"
#include "path-concat.h"
#include "quote.h"
#include "cloexec.h"

#define PROGRAM_NAME "nohup"

#define AUTHORS "Jim Meyering"

/* Exit statuses.  */
enum
  {
    /* `nohup' itself failed.  */
    NOHUP_FAILURE = 127
  };

char *program_name;

void
usage (int status)
{
  if (status != EXIT_SUCCESS)
    fprintf (stderr, _("Try `%s --help' for more information.\n"),
	     program_name);
  else
    {
      printf (_("\
Usage: %s COMMAND [ARG]...\n\
  or:  %s OPTION\n\
"),
	      program_name, program_name);

      fputs (_("\
Run COMMAND, ignoring hangup signals.\n\
\n\
"), stdout);
      fputs (HELP_OPTION_DESCRIPTION, stdout);
      fputs (VERSION_OPTION_DESCRIPTION, stdout);
      printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
    }
  exit (status);
}

int
main (int argc, char **argv)
{
  int fd;
  int saved_stderr_fd = -1;
  bool stderr_isatty;

  initialize_main (&argc, &argv);
  program_name = argv[0];
  setlocale (LC_ALL, "");
  bindtextdomain (PACKAGE, LOCALEDIR);
  textdomain (PACKAGE);

  initialize_exit_failure (NOHUP_FAILURE);
  atexit (close_stdout);

  parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
		      usage, AUTHORS, (char const *) NULL);
  if (getopt (argc, argv, "+") != -1)
    usage (EXIT_FAILURE);

  if (argc <= optind)
    {
      error (0, 0, _("missing operand"));
      usage (NOHUP_FAILURE);
    }

  /* If standard output is a tty, redirect it (appending) to a file.
     First try nohup.out, then $HOME/nohup.out.  */
  if (isatty (STDOUT_FILENO))
    {
      char *in_home = NULL;
      char const *file = "nohup.out";
      int flags = O_CREAT | O_WRONLY | O_APPEND;
      mode_t mode = S_IRUSR | S_IWUSR;

      fd = open (file, flags, mode);
      if (fd == -1)
	{
	  int saved_errno = errno;
	  char const *home = getenv ("HOME");
	  if (home)
	    {
	      in_home = path_concat (home, file, NULL);
	      fd = open (in_home, flags, mode);
	    }
	  if (fd == -1)
	    {
	      int saved_errno2 = errno;
	      error (0, saved_errno, _("failed to open %s"), quote (file));
	      if (in_home)
		error (0, saved_errno2, _("failed to open %s"),
		       quote (in_home));
	      exit (NOHUP_FAILURE);
	    }
	  file = in_home;
	}

      /* Redirect standard output to the file.  */
      if (dup2 (fd, STDOUT_FILENO) == -1)
	error (NOHUP_FAILURE, errno, _("failed to redirect standard output"));

      error (0, 0, _("appending output to %s"), quote (file));
      if (in_home)
	free (in_home);
    }
  else
    {
      fd = STDOUT_FILENO;
    }

  /* If stderr is on a tty, redirect it to stdout.  */
  if ((stderr_isatty = isatty (STDERR_FILENO)))
    {
      /* Save a copy of stderr before redirecting, so we can use the original
	 if execve fails.  It's no big deal if this dup fails.  It might
	 not change anything, and at worst, it'll lead to suppression of
	 the post-failed-execve diagnostic.  */
      saved_stderr_fd = dup (STDERR_FILENO);

      if (saved_stderr_fd != -1
	  && set_cloexec_flag (saved_stderr_fd, true) != 0)
	error (NOHUP_FAILURE, errno,
	       _("failed to set the copy of stderr to close on exec"));

      if (dup2 (fd, STDERR_FILENO) == -1)
	error (NOHUP_FAILURE, errno, _("failed to redirect standard error"));
    }

  signal (SIGHUP, SIG_IGN);

  {
    int exit_status;
    int saved_errno;
    char **cmd = argv + optind;

    execvp (*cmd, cmd);
    exit_status = (errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE);
    saved_errno = errno;

    /* The execve failed.  Output a diagnostic to stderr only if:
       - stderr was initially redirected to a non-tty, or
       - stderr was initially directed to a tty, and we've
	 just dup2'd it to point back to that same tty.
       In other words, output the diagnostic if possible, but not if
       it'd go to nohup.out.  */
    if ( ! stderr_isatty
	|| (saved_stderr_fd != -1
	    && dup2 (saved_stderr_fd, STDERR_FILENO) != -1))
      error (0, saved_errno, _("cannot run command %s"), quote (*cmd));

    exit (exit_status);
  }
}