4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI"
41 #include <sys/types.h>
48 #define NOHUP_PERM (S_IRUSR | S_IWUSR)
50 #define NOHUP_NOEXEC 126
51 #define NOHUP_ERROR 127
58 static int pnohup(int, char **);
60 static struct ps_prochandle
*g_proc
;
65 static volatile int g_interrupt
= 0;
75 static char nout
[PATH_MAX
] = "nohup.out";
82 int flags
= O_CREAT
| O_WRONLY
| O_APPEND
;
84 if ((fd
= open(nout
, flags
, NOHUP_PERM
)) < 0) {
85 if ((home
= getenv("HOME")) == NULL
)
88 if ((snprintf(nout
, sizeof (nout
),
89 "%s/nohup.out", home
) >= sizeof (nout
)) ||
90 (fd
= open(nout
, flags
, NOHUP_PERM
)) < 0) {
96 (void) fprintf(stderr
, gettext("Sending output to %s\n"), nout
);
102 main(int argc
, char **argv
)
108 if ((pname
= strrchr(argv
[0], '/')) == NULL
)
111 argv
[0] = ++pname
; /* for getopt */
113 (void) setlocale(LC_ALL
, "");
116 #define TEXT_DOMAIN "SYS_TEST"
119 (void) textdomain(TEXT_DOMAIN
);
121 while ((opt
= getopt(argc
, argv
, OPTSTR
)) != EOF
) {
144 goto usage
; /* need at least one argument */
151 return (pnohup(argc
, argv
));
154 goto usage
; /* only valid with -p or -g */
159 (void) signal(SIGHUP
, SIG_IGN
); /* POSIX.2 only SIGHUP */
161 (void) signal(SIGQUIT
, SIG_IGN
); /* Solaris compatibility */
164 if (isatty(STDOUT_FILENO
)) {
165 if ((fd
= open_file()) < 0)
168 (void) dup2(fd
, STDOUT_FILENO
);
171 if (isatty(STDERR_FILENO
)) {
172 if (fd
< 0 && (fd
= open_file()) < 0)
175 (void) dup2(fd
, STDERR_FILENO
);
181 (void) execvp(argv
[0], argv
);
184 (void) freopen("/dev/tty", "w", stderr
);
185 (void) fprintf(stderr
, gettext("nohup: %s: %s\n"), argv
[0],
188 return (err
== ENOENT
? NOHUP_ERROR
: NOHUP_NOEXEC
);
191 (void) fprintf(stderr
, gettext("nohup: cannot open/create "
192 "nohup.out: %s\n"), strerror(errno
));
193 return (NOHUP_ERROR
);
197 (void) fprintf(stderr
,
198 gettext("usage: nohup command [argument ...]\n"));
200 (void) fprintf(stderr
, gettext("usage:\n"
201 "\tnohup command [argument ...]\n"
202 "\tnohup -p [-Fa] pid [pid ...]\n"
203 "\tnohup -g [-Fa] pgid [pgid ...]\n"));
205 return (NOHUP_ERROR
);
211 * File descriptor iteration interface.
213 typedef int proc_fd_iter_f(void *, int);
216 Pfd_iter(struct ps_prochandle
*P
, proc_fd_iter_f
*cb
, void *data
)
223 if (Pstate(P
) == PS_DEAD
)
226 (void) sprintf(file
, "/proc/%d/fd", (int)Pstatus(P
)->pr_pid
);
227 if ((dirp
= opendir(file
)) == NULL
)
230 while ((dentp
= readdir(dirp
)) != NULL
) {
231 if (dentp
->d_name
[0] == '.')
234 if ((ret
= cb(data
, atoi(dentp
->d_name
))) != 0)
238 (void) closedir(dirp
);
245 fd_cb(void *data
, int fd
)
255 * See if this fd refers to the controlling tty.
257 if (pr_fstat64(g_proc
, fd
, &sbuf
) == -1 ||
258 sbuf
.st_rdev
!= Ppsinfo(g_proc
)->pr_ttydev
)
262 * tty's opened for input are usually O_RDWR so that the program
263 * can change terminal settings. We assume that if there's a
264 * controlling tty in the STDIN_FILENO file descriptor that is
265 * effectively used only for input. If standard in gets dup'ed to
266 * other file descriptors, then we're out of luck unless the
267 * program is nice enough to fcntl it to be O_RDONLY. We close the
268 * file descriptor before we call open to handle the case that
269 * there are no available file descriptors left in the victim. If
270 * our call to pr_open fails, we try to reopen the controlling tty.
272 flags
= pr_fcntl(g_proc
, fd
, F_GETFL
, NULL
);
273 if ((flags
& O_ACCMODE
) == O_RDONLY
|| fd
== STDIN_FILENO
) {
279 oflags
= O_RDWR
| O_APPEND
;
284 (void) pr_close(g_proc
, fd
);
286 tmpfd
= pr_open(g_proc
, file
, oflags
, 0);
289 (void) fprintf(stderr
,
290 gettext("nohup: process %d cannot open %s: %s\n"),
291 Pstatus(g_proc
)->pr_pid
, file
, strerror(errno
));
297 (void) pr_fcntl(g_proc
, tmpfd
, F_DUP2FD
,
298 (void *)(uintptr_t)fd
);
299 (void) pr_close(g_proc
, tmpfd
);
304 (void) pr_fcntl(g_proc
, *fdp
, F_DUP2FD
, (void *)(uintptr_t)fd
);
311 * The victim couldn't open nohup.out so we'll have it try to reopen
312 * its terminal. If this fails, we are left with little recourse.
314 tmpfd
= pr_open(g_proc
, "/dev/tty", O_RDWR
, 0);
316 if (tmpfd
!= fd
&& tmpfd
>= 0) {
317 (void) pr_fcntl(g_proc
, tmpfd
, F_DUP2FD
, (void *)(uintptr_t)fd
);
318 (void) pr_close(g_proc
, tmpfd
);
325 lwp_restartable(short syscall
)
356 lwp_abort(void *data
, const lwpstatus_t
*lsp
)
358 struct ps_lwphandle
*L
;
362 * Continue if this lwp isn't asleep in a restartable syscall.
364 if (!(lsp
->pr_flags
& PR_ASLEEP
) || !lwp_restartable(lsp
->pr_syscall
))
367 L
= Lgrab(g_proc
, lsp
->pr_lwpid
, &err
);
368 (void) Lsetrun(L
, 0, PRSABORT
);
372 * Indicate that we have aborted a syscall.
381 lwp_restart(void *data
, const lwpstatus_t
*lsp
)
383 struct ps_lwphandle
*L
;
387 * If any lwp is still sleeping in a restartable syscall, it means
388 * the lwp is wedged and we've screwed up.
390 if (lsp
->pr_flags
& PR_ASLEEP
) {
391 if (!lwp_restartable(lsp
->pr_syscall
))
393 (void) fprintf(stderr
, gettext("nohup: LWP %d failed "
394 "to abort syscall (%d) in process %d\n"),
395 lsp
->pr_lwpid
, lsp
->pr_syscall
, Pstatus(g_proc
)->pr_pid
);
399 if (lsp
->pr_why
== PR_SYSEXIT
&& lsp
->pr_errno
== EINTR
) {
400 L
= Lgrab(g_proc
, lsp
->pr_lwpid
, &err
);
401 (void) Lputareg(L
, R_R0
, ERESTART
);
410 do_pnohup(struct ps_prochandle
*P
)
414 const pstatus_t
*psp
;
419 * Make sure there's a pending procfs stop directive.
423 if (Pcreate_agent(P
) != 0) {
424 (void) fprintf(stderr
, gettext("nohup: cannot control "
425 "process %d\n"), psp
->pr_pid
);
430 * Set the disposition of SIGHUP and SIGQUIT to SIG_IGN. If either
431 * signal is handled by the victim, only adjust the disposition if
432 * the -a flag is set.
434 if (!opt_a
&& pr_sigaction(P
, SIGHUP
, NULL
, &sa
) != 0) {
435 (void) fprintf(stderr
, gettext("nohup: cannot read "
436 "disposition of SIGHUP for %d\n"), psp
->pr_pid
);
440 if (!opt_a
&& sa
.sa_handler
!= SIG_DFL
&& sa
.sa_handler
!= SIG_IGN
) {
441 (void) fprintf(stderr
, gettext("nohup: SIGHUP already handled "
442 "by %d; use -a to force process to ignore\n"), psp
->pr_pid
);
446 if (!opt_a
&& pr_sigaction(P
, SIGQUIT
, NULL
, &sa
) != 0) {
447 (void) fprintf(stderr
, gettext("nohup: cannot read "
448 "disposition of SIGQUIT for %d\n"), psp
->pr_pid
);
452 if (!opt_a
&& sa
.sa_handler
!= SIG_DFL
&& sa
.sa_handler
!= SIG_IGN
) {
453 (void) fprintf(stderr
, gettext("nohup: SIGQUIT already handled "
454 "by %d; use -a to force process to ignore\n"), psp
->pr_pid
);
458 sa
.sa_handler
= SIG_IGN
;
460 if (pr_sigaction(P
, SIGHUP
, &sa
, NULL
) != 0) {
461 (void) fprintf(stderr
, gettext("nohup: cannot set "
462 "disposition of SIGHUP for %d\n"), psp
->pr_pid
);
466 if (pr_sigaction(P
, SIGQUIT
, &sa
, NULL
) != 0) {
467 (void) fprintf(stderr
, gettext("nohup: cannot set "
468 "disposition of SIGQUIT for %d\n"), psp
->pr_pid
);
476 * We need to close and reassign some file descriptors, but we
477 * need to be careful about how we do it. If we send in the agent
478 * to close some fd and there's an lwp asleep in the kernel due to
479 * a syscall using that fd, then we have a problem. The normal
480 * sequence of events is the close syscall wakes up any threads
481 * that have the fd in question active (see kthread.t_activefd)
482 * and then waits for those threads to wake up and release the
483 * file descriptors (they then continue to user-land to return
484 * EBADF from the syscall). However, recall that if the agent lwp
485 * is present in a process, no other lwps can run, so if the agent
486 * lwp itself is making the call to close(2) (or something else
487 * like dup2 that involves a call to closeandsetf()) then we're in
488 * pretty bad shape. The solution is to abort and restart any lwp
489 * asleep in a syscall on the off chance that it may be using one
490 * of the file descriptors that we want to manipulate.
494 * We may need to chase some lwps out of the kernel briefly, so we
495 * send SIGCONT to the process if it was previously stopped due to
496 * a job control signal, and save the current signal to repost it
497 * when we detatch from the victim. A process that is stopped due
498 * to job control will start running as soon as we send SIGCONT
499 * since there is no procfs stop command pending; we use Pdstop to
500 * post a procfs stop request (above).
502 if ((psp
->pr_lwp
.pr_flags
& PR_STOPPED
) &&
503 psp
->pr_lwp
.pr_why
== PR_JOBCONTROL
) {
504 sig
= psp
->pr_lwp
.pr_what
;
505 (void) kill(psp
->pr_pid
, SIGCONT
);
509 (void) Psysexit(P
, 0, 1);
512 * Abort each syscall; set g_dirty if any lwp was asleep.
516 (void) Plwp_iter(P
, lwp_abort
, NULL
);
520 * Block until each lwp that was asleep in a syscall has
521 * wandered back up to user-land.
526 * Make sure that each lwp has successfully aborted its
527 * syscall and that the syscall gets restarted when we
530 if (Plwp_iter(P
, lwp_restart
, NULL
) != 0)
534 (void) Psysexit(P
, 0, 0);
536 if (Pcreate_agent(P
) != 0) {
537 (void) fprintf(stderr
, gettext("nohup: cannot control "
538 "process %d\n"), psp
->pr_pid
);
543 * See if the victim has access to the nohup.out file we created.
544 * If the user does something that would invalidate the result
545 * of this call from here until the call to pr_open, the process
546 * may be left in an inconsistent state -- we assume that the user
547 * is not intentionally trying to shoot himself in the foot.
549 if (pr_access(P
, nout
, R_OK
| W_OK
) != 0) {
550 (void) fprintf(stderr
, gettext("nohup: process %d can not "
551 "access %s: %s\n"), psp
->pr_pid
, nout
, strerror(errno
));
556 * Redirect output to the controlling tty to nohup.out and tty
557 * input to read from /dev/null.
563 (void) Pfd_iter(P
, fd_cb
, NULL
);
567 (void) kill(psp
->pr_pid
, sig
);
575 (void) kill(psp
->pr_pid
, sig
);
587 pnohup(int argc
, char **argv
)
589 struct ps_prochandle
*P
;
599 * Catch signals from the terminal.
601 if (sigset(SIGHUP
, SIG_IGN
) == SIG_DFL
)
602 (void) sigset(SIGHUP
, intr
);
603 if (sigset(SIGINT
, SIG_IGN
) == SIG_DFL
)
604 (void) sigset(SIGINT
, intr
);
605 if (sigset(SIGQUIT
, SIG_IGN
) == SIG_DFL
)
606 (void) sigset(SIGQUIT
, intr
);
607 (void) sigset(SIGPIPE
, intr
);
608 (void) sigset(SIGTERM
, intr
);
614 * Set nout to be the full path name of nohup.out and fname to be
615 * the simplified path name:
616 * nout = /cwd/nohup.out fname = nohup.out
617 * nout = $HOME/nohup.out fname = $HOME/nohup.out
619 if (getcwd(nout
, sizeof (nout
) - strlen("/nohup.out") - 1) != NULL
) {
620 fname
= &nout
[strlen(nout
)];
621 (void) strcpy(fname
, "/nohup.out");
624 nh_fd
= open(nout
, O_WRONLY
| O_CREAT
, NOHUP_PERM
);
627 if (nh_fd
== -1 && (home
= getenv("HOME")) != NULL
) {
628 if (snprintf(nout
, sizeof (nout
),
629 "%s/nohup.out", home
) < sizeof (nout
)) {
630 nh_fd
= open(nout
, O_WRONLY
| O_CREAT
, NOHUP_PERM
);
636 (void) fprintf(stderr
, gettext("nohup: cannot open/create "
637 "nohup.out: %s\n"), strerror(errno
));
639 return (NOHUP_ERROR
);
648 * Make nohup its own process group leader so that we
649 * don't accidently send SIGSTOP to this process.
651 (void) setpgid(0, 0);
654 * If a list of process group ids is specified, we want to
655 * first SIGSTOP the whole process group so that we can be
656 * sure not to miss any processes that belong to the group
657 * (it's harder to hit a moving target). We then iterate
658 * over all the processes on the system looking for
659 * members of the given process group to apply the
660 * do_pnohup function to. If the process was stopped due
661 * to our SIGSTOP, we send the process SIGCONT; if the
662 * process was already stopped, we leave it alone.
664 pgids
= calloc(argc
, sizeof (pid_t
));
668 for (i
= 0; i
< argc
; i
++) {
672 const pstatus_t
*psp
;
675 hrtime_t kill_time
, stop_time
;
677 if (isdigit(*argv
[i
])) {
678 pgid
= strtol(argv
[i
], &end
, 10);
681 * kill(2) with pid = 0 or -1 has a special
682 * meaning, so don't let pgid be 0 or 1.
684 if (*end
== '\0' && pgid
> 1)
688 (void) fprintf(stderr
, gettext("nohup: "
689 "bad process group %s\n"), argv
[i
]);
695 * We don't want to nohup a process group twice.
697 for (j
= 0; j
< npgids
; j
++) {
698 if (pgids
[j
] == pgid
)
705 pgids
[npgids
++] = pgid
;
708 * Have the kernel stop all members of the process
709 * group; record the time we stopped the process
710 * group so that we can tell if a member stopped
711 * because of this call to kill(2) or if it was
712 * already stopped when we got here. If the user
713 * job control stops the victim between the call
714 * to gethrtime(2) and kill(2), we may send
715 * SIGCONT when we really shouldn't -- we assume
716 * that the user is not trying to shoot himself in
719 kill_time
= gethrtime();
720 if (kill(-pgid
, SIGSTOP
) == -1) {
721 (void) fprintf(stderr
, gettext("nohup: cannot "
722 "stop process group %d: %s\n"), pgid
,
723 errno
!= ESRCH
? strerror(errno
) :
724 gettext("No such process group"));
730 dirp
= opendir("/proc");
732 while ((dent
= readdir(dirp
)) != NULL
&& !g_interrupt
) {
733 if (dent
->d_name
[0] == '.')
736 if (proc_arg_psinfo(dent
->d_name
,
737 PR_ARG_PIDS
, &psinfo
, &gcode
) == -1)
740 if (psinfo
.pr_pgid
!= pgid
)
746 if (psinfo
.pr_nlwp
== 0)
749 if ((P
= proc_arg_grab(dent
->d_name
,
750 PR_ARG_PIDS
, flag
, &gcode
)) == NULL
) {
751 (void) fprintf(stderr
, gettext("nohup: "
752 "cannot examine %s: %s\n"),
753 dent
->d_name
, Pgrab_error(gcode
));
755 (void) kill(psinfo
.pr_pid
, SIGCONT
);
760 * This implicitly restarts any process that
761 * was stopped via job control any time after
762 * the call to kill(2). This is the desired
763 * behavior since nohup is busy trying to
764 * disassociate a process from its controlling
768 if (psp
->pr_lwp
.pr_why
== PR_JOBCONTROL
) {
770 psp
->pr_lwp
.pr_tstamp
.tv_sec
;
771 stop_time
*= (hrtime_t
)NANOSEC
;
773 psp
->pr_lwp
.pr_tstamp
.tv_nsec
;
778 if (do_pnohup(P
) == 0)
782 * If the process was stopped because of
783 * our call to kill(2) (i.e. if it stopped
784 * some time after kill_time) then restart
787 if (kill_time
<= stop_time
)
788 (void) kill(psinfo
.pr_pid
, SIGCONT
);
794 * If we didn't successfully nohup any member of the
800 (void) closedir(dirp
);
803 for (i
= 0; i
< argc
&& !g_interrupt
; i
++) {
804 if ((P
= proc_arg_grab(argv
[i
], PR_ARG_PIDS
, flag
,
806 (void) fprintf(stderr
,
807 gettext("nohup: cannot examine %s: %s\n"),
808 argv
[i
], Pgrab_error(gcode
));
814 if (do_pnohup(P
) != 0)
824 return (NOHUP_ERROR
);
826 (void) fprintf(stderr
, gettext("Sending output to %s\n"), fname
);