-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnohup_u.c
143 lines (136 loc) · 3.51 KB
/
nohup_u.c
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
/* nohup - toolbox
Copyright 2015 libdll.so
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 of the License, 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.
*/
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
int nohup_main(int argc, char *argv[]) {
//pid_t check;
int err_fd = -1;
if(argc > 1 && argv[1][0] == '-') switch(argv[1][1]) {
case 0:
break;
case '-':
if(!argv[1][2]) {
argv[1] = argv[0];
argv++;
argc--;
break;
}
fprintf(stdout, "%s: invalid option '%s'\n"
"Try '%s -h' for more information.\n",
argv[0], argv[1], argv[0]);
return 1;
case 'h':
fprintf(stdout, "Usage: %s <command> [<arg>] [...]\n", argv[0]);
return 0;
default:
fprintf(stdout, "%s: invalid option '-%c'\n"
"Try '%s -h' for more information.\n",
argv[0], argv[1][1], argv[0]);
return 1;
}
if(argc == 1) {
fprintf(stderr, "%s: missing operand\n"
"Try '%s -h' for more information.\n",
argv[0], argv[0]);
return -1;
}
#if 0
pid_t pid = fork();
if(pid == -1) {
perror("fork");
return 126;
}
if(pid) {
int r;
if(waitpid(pid, &r, 0) < 0) {
perror("waitpid");
return 126;
}
if(WIFSIGNALED(r)) {
fprintf(stderr, "%s: Child process was terminated by signal %d\n", WTERMSIG(r));
}
return r;
}
#endif
if(isatty(STDOUT_FILENO)) {
int fd = open("nohup.out", O_RDWR | O_CREAT | O_APPEND, 0666);
if(fd == -1) {
const char *home = getenv("HOME");
if(!home) {
fprintf(stderr, "%s: HOME not defined\n", argv[0]);
fprintf(stderr, "%s: Cannot open 'nohup.out'\n", argv[0]);
return 126;
}
size_t home_len = strlen(home);
char path[home_len + 1 + 9 + 1];
memcpy(path, home, home_len);
path[home_len] = '/';
strcpy(path + home_len + 1, "nohup.out");
fd = open(path, O_RDWR | O_CREAT | O_APPEND, 0666);
if(fd == -1) {
fprintf(stderr, "%s: Cannot open '%s', %s\n", argv[0], path, strerror(errno));
return 126;
} else fprintf(stderr, "%s: Appending output to '%s'\n", argv[0], path);
} else fprintf(stderr, "%s: Appending output to 'nohup.out'\n", argv[0]);
if(dup2(fd, STDOUT_FILENO) == -1) {
perror("dup2");
//perror(argv[0]);
return 126;
}
}
if(isatty(STDERR_FILENO)) {
err_fd = dup(STDERR_FILENO);
#if !defined __INTERIX && !defined __sun
if(err_fd != -1) stderr = fdopen(err_fd, "w");
#endif
if(dup2(STDOUT_FILENO, STDERR_FILENO) == -1) {
perror("dup2");
return 126;
}
}
signal(SIGHUP, SIG_IGN);
#if 0
check = fork();
/* fprintf(stdout, "1pid is = %d\n", check); */
if(check == -1) {
perror("Fork error");
return -1;
}
if(check == 0) {
check = fork();
/* fprintf(stdout, "2pid is = %d\n", check); */
if(check == -1) {
perror("Fork error");
return 1;
}
if(check == 0) {
argv++;
/* fprintf(stdout, "execing\n"); */
execvp(*argv, argv);
perror("execerr");
return 2;
}
return 0;
}
return 0;
#else
argv++;
execvp(*argv, argv);
int e = errno;
#ifdef __INTERIX
if(err_fd != -1) dup2(err_fd, STDERR_FILENO);
#endif
//perror("execvp");
perror(argv[0]);
if(err_fd != -1) close(err_fd);
return e == ENOENT ? 127 : 126;
#endif
}