こんにちは、私の目的は、呼び出されたときに別のプログラムを実行し、子プロセスの動作を制御しようとする C プログラムを作成することです。
子が任意のファイル (事前に開いているファイル stdin、stdout、stderr 以外) を開く場合、これを防止し、プログラムを終了する必要があります。
上記のプログラム要件を実装するために、次のプログラムを作成しました。幸いなことに、私のプログラムは、KITTY セッションから LINUX OS を再起動した後にプログラムが実行されたときにのみ、子供が任意のファイル (事前に開いたファイル stdin、stdout、stderr を除く) を開いたときに生成される SIGIO シグナルを一貫してインターセプトします。問題は、プログラムが初めて正常に実行された後、その後の SIGIO シグナルの傍受の試行で失敗することです。プログラムで何が間違っているのか、どうすれば修正できるのか教えてください。
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/mman.h>
#include <errno.h>
#include <semaphore.h>
#include <sys/sysinfo.h>
#include <pthread.h>
#include <sys/ptrace.h>
#define MAXLINE 256
#define MAXARGS 128
#define SHELL "/h/fchang03"
#define READ 0
#define WRITE 1
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
int fd[2];
pthread_spinlock_t spinlock;
struct statStuff {
int pid; // %d
char comm[256]; // %s
char state; // %c
int ppid; // %d
int pgrp; // %d
int session; // %d
int tty_nr; // %d
int tpgid; // %d
unsigned long flags; // %lu
unsigned long minflt; // %lu
unsigned long cminflt; // %lu
unsigned long majflt; // %lu
unsigned long cmajflt; // %lu
unsigned long utime; // %lu
unsigned long stime; // %lu
long cutime; // %ld
long cstime; // %ld
long priority; // %ld
long nice; // %ld
long num_threads; // %ld
long itrealvalue; // %ld
unsigned long starttime; // %lu
unsigned long vsize; // %lu
long rss; // %ld
unsigned long rlim; // %lu
unsigned long startcode; // %lu
unsigned long endcode; // %lu
unsigned long startstack; // %lu
unsigned long kstkesp; // %lu
unsigned long kstkeip; // %lu
unsigned long signal; // %lu
unsigned long blocked; // %lu
unsigned long sigignore; // %lu
unsigned long sigcatch; // %lu
unsigned long wchan; // %lu
unsigned long nswap; // %lu
unsigned long cnswap; // %lu
int exit_signal; // %d
int processor; // %d
unsigned long rt_priority; // %lu
unsigned long policy; // %lu
unsigned long long delayacct_blkio_ticks; // %llu
} ;
static int readStat(int pid, struct statStuff *s) {
const char *format = "%d %s %c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld %lu %lu %ld %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu %llu";
char buf[256];
FILE *proc;
sprintf(buf,"/proc/%d/stat",pid);
proc = fopen(buf,"r");
if (proc) {
if (42==fscanf(proc, format,
&s->pid,
s->comm,
&s->state,
&s->ppid,
&s->pgrp,
&s->session,
&s->tty_nr,
&s->tpgid,
&s->flags,
&s->minflt,
&s->cminflt,
&s->majflt,
&s->cmajflt,
&s->utime,
&s->stime,
&s->cutime,
&s->cstime,
&s->priority,
&s->nice,
&s->num_threads,
&s->itrealvalue,
&s->starttime,
&s->vsize,
&s->rss,
&s->rlim,
&s->startcode,
&s->endcode,
&s->startstack,
&s->kstkesp,
&s->kstkeip,
&s->signal,
&s->blocked,
&s->sigignore,
&s->sigcatch,
&s->wchan,
&s->nswap,
&s->cnswap,
&s->exit_signal,
&s->processor,
&s->rt_priority,
&s->policy,
&s->delayacct_blkio_ticks
)) {
printf("Group Number = %d\n",s->session);
printf("Stack start = %x\n",s->startstack);
printf("Stack end = %x\n",s->kstkesp);
fclose(proc);
return s->session;
} else {
fclose(proc);
return 0;
}
} else {
return 0;
}
}
int eval(const char *cmdline)
{
char *argv[MAXARGS];
int bg;
pid_t pid;
int status;
struct statStuff test;
int value;
FILE* fp;
struct rusage usage;
pid = fork();
if (pid > 0){
/* This is the child process. */
char* p;
int num_procs;
int n_bytes = 256;
int bytes_read;
char buffer[1024];
char stuff[256];
char* ptr;
struct rlimit rl;
int x;
sscanf(cmdline,"%s %d",stuff,&x);
rl.rlim_cur = 15;
rl.rlim_max = 15;
setrlimit(RLIMIT_NPROC,&rl);
ptr = 0;
if (x == 0){
sprintf(buffer,"/proc/%d/stat",pid);
}
else{
sprintf(buffer,"/proc/%d/stat",x);
}
int fd = open(buffer,O_RDONLY);
while (read(fd,buffer,256) > 0){
sprintf(cmdline,"%s %d",stuff,readStat(x,&test));
printf("CMDLINE = %s\n",cmdline);
eval(cmdline);
lseek(fd,256,SEEK_CUR);
}
close(fd);
wait3(&status,0,&usage);
exit(1);
}
else if (pid < 0){
/* The fork failed. Report failure. */
status = -1;
}
else{
close(fd[READ]);
close(fileno(stdout));
dup(fd[WRITE]);
fcntl(0,F_SETFL,O_ASYNC);
fcntl(1,F_SETFL,O_ASYNC);
fcntl(2,F_SETFL,O_ASYNC);
fcntl(3,F_SETFL,O_ASYNC);
execl("/h/fchang03/five","five",0);
perror("execl");
}
return 0;
}
void *reader(int signal)
{
pid_t pid1,pid2;
int status;
int fd[2];
char buf[256];
char buffer[256];
char filename[256];
char dbgfilename[256];
int myfd;
int length;
struct rusage usage;
// printf("SIGNAl %d received\n",signal);
printf("%s"," ");
length = 256;
if (signal == 0){
return NULL;
}
pipe(fd);
if ((pid1 = fork())) { /* child subprocess */
pthread_spin_lock(&spinlock);
sprintf(filename,"/proc/%d/fdinfo/1",pid1);
myfd = open(filename,O_RDONLY);
pthread_spin_unlock(&spinlock);
if (myfd == -1){
printf("myfd = %d filename = %s\n",myfd,filename);
perror("myerror");
exit(1);
}
fcntl(0,F_SETOWN,getpid());
fcntl(1,F_SETOWN,getpid());
fcntl(2,F_SETOWN,getpid());
fcntl(3,F_SETOWN,getpid());
if (signal == SIGIO){
printf("SIGNAL SIGIO RECEIVED");
kill(pid1,SIGKILL);
}
close(myfd);
close(fd[WRITE]);
FILE *read = fdopen(fd[READ],"r");
while (!feof(read)) {
fgets(buf,256,read);
}
close(fd[READ]);
pid2 = wait3(&status,0, &usage);
if (signal == SIGKILL){
exit(1);
}
}
else {
close(fd[READ]);
close(fileno(stdout));
dup(fd[WRITE]);
// child only unblock mask
fcntl(0, F_SETFL, O_ASYNC);
fcntl(1, F_SETFL, O_ASYNC);
fcntl(2, F_SETFL, O_ASYNC);
fcntl(3, F_SETFL, O_ASYNC);
execl("/h/fchang03/five","five",NULL);
}
}
int main(void)
{
pthread_t tid1,tid2;
char cmdline[MAXLINE];
char temp[MAXLINE];
int start;
struct sigaction set,myset;
pthread_t thread;
pthread_t thread2;
sigemptyset(&set);
sigemptyset(&myset);
memset(&set,0,sizeof(struct sigaction));
memset(&myset,0,sizeof(struct sigaction));
sigaddset(&set.sa_mask, SIGINT);
sigaddset(&set.sa_mask, SIGCHLD);
sigaddset(&set.sa_mask, SIGIO);
set.sa_flags = SA_RESTART | SA_NOCLDSTOP | SA_SIGINFO;
set.sa_handler = reader;
sigaddset(&myset.sa_mask, SIGIO);
myset.sa_flags = SA_RESTART | SA_NOCLDSTOP | SA_SIGINFO;
myset.sa_handler = reader;
sigprocmask(SIG_UNBLOCK, &myset, &set); // later explicitly just unblock SIGIO
sigaction(SIGCHLD,&myset,&set);
sigaction(SIGIO,&myset,&set);
pthread_spin_init(&spinlock, 0);
pthread_create(&tid1,NULL,reader,NULL);
pthread_create(&tid2,NULL,reader,NULL);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
start = 0;
while (1){
printf("> ");
fgets(temp, MAXLINE, stdin);
sprintf(cmdline,"./%s %d",temp,start);
if (feof(stdin)){
exit(0);
}
if (eval(cmdline) == -1){
break;
}
}
}