-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpfork.c
116 lines (92 loc) · 2.75 KB
/
pfork.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
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: 2020,2024 sulincix <[email protected]>
// SPDX-FileCopyrightText: 2024-2025 kurth4cker <[email protected]>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include <pty.h>
mode_t umask_val = 0;
const char *logfile = "pfork.out";
const char *infile = "pfork.in";
bool silent = true;
void set_umask(mode_t value)
{
umask_val = value;
}
void skeleton_daemon()
{
pid_t pid;
/* Fork off the parent process */
pid = fork();
/* An error occurred */
if (pid < 0)
exit(EXIT_FAILURE);
/* Success: Let the parent terminate */
if (pid > 0)
exit(EXIT_SUCCESS);
/* On success: The child process becomes session leader */
if (setsid() < 0)
exit(EXIT_FAILURE);
/* Catch, ignore and handle signals */
// TODO: Implement a working signal handler
signal(SIGCHLD, SIG_IGN);
signal(SIGHUP, SIG_IGN);
/* Fork off for the second time*/
pid = fork();
/* An error occurred */
if (pid < 0)
exit(EXIT_FAILURE);
/* Success: Let the parent terminate */
if (pid > 0) {
fprintf(stderr, "\nDaemon started:\t[%d]\n",getpid ()+pid);
exit(EXIT_SUCCESS);
}
/* Set new file permissions */
umask(umask_val);
if (silent) {
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP;
int fdin = open(infile, O_RDONLY,mode);
int fdout = open(logfile, O_WRONLY | O_CREAT,mode);
int fderr = open(logfile, O_WRONLY | O_CREAT,mode);
/* Set in, out and err to file*/
close(0);
dup(fdin);
close(fdin);
close(1);
dup(fdout);
close(2);
dup(fderr);
close(fdout);
} else {
/* Set in, out and err to pty*/
int master_fd, slave_fd;
// Open a pseudo-terminal
if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) == -1) {
perror("openpty");
exit(EXIT_FAILURE);
}
printf("The master psuedo-terminal id is %d\n",master_fd);
// Replace stdin with the slave side of the PTY
if (dup2(slave_fd, STDIN_FILENO) == -1) {
perror("dup2");
exit(EXIT_FAILURE);
}
// Replace stdout with the slave side of the PTY
if (dup2(slave_fd, STDOUT_FILENO) == -1) {
perror("dup2");
exit(EXIT_FAILURE);
}
// Replace stderr with the slave side of the PTY
if (dup2(slave_fd, STDERR_FILENO) == -1) {
perror("dup2");
exit(EXIT_FAILURE);
}
// Close the unused PTY file descriptor
close(slave_fd);
}
}