Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
waffleShell
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
julien.seemulle
waffleShell
Commits
b435ebcd
Commit
b435ebcd
authored
4 years ago
by
julien.seemulle
Browse files
Options
Downloads
Patches
Plain Diff
Sending SIGINT to foreground jobs working.
parent
f430091a
No related branches found
No related tags found
No related merge requests found
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
bin/command.c
+5
-2
5 additions, 2 deletions
bin/command.c
bin/command.h
+2
-2
2 additions, 2 deletions
bin/command.h
bin/command.o
+0
-0
0 additions, 0 deletions
bin/command.o
bin/main.c
+32
-5
32 additions, 5 deletions
bin/main.c
bin/main.o
+0
-0
0 additions, 0 deletions
bin/main.o
wshell
+0
-0
0 additions, 0 deletions
wshell
with
39 additions
and
9 deletions
bin/command.c
+
5
−
2
View file @
b435ebcd
...
@@ -80,7 +80,7 @@ void exec_builtin(char **argv, int argc)
...
@@ -80,7 +80,7 @@ void exec_builtin(char **argv, int argc)
return
;
return
;
}
}
void
exec_job
(
cmd_t
*
cmd
,
bool
foreground
)
void
exec_job
(
cmd_t
*
cmd
,
bool
foreground
,
int
*
pid_job
)
{
{
int
child_status
;
int
child_status
;
...
@@ -111,6 +111,7 @@ void exec_job(cmd_t *cmd, bool foreground)
...
@@ -111,6 +111,7 @@ void exec_job(cmd_t *cmd, bool foreground)
// Create child with fork
// Create child with fork
pid_t
pid
=
fork
();
pid_t
pid
=
fork
();
*
pid_job
=
pid
;
if
(
pid
==
0
)
if
(
pid
==
0
)
{
{
...
@@ -230,7 +231,7 @@ void exec_job(cmd_t *cmd, bool foreground)
...
@@ -230,7 +231,7 @@ void exec_job(cmd_t *cmd, bool foreground)
// Function where the piped system commands is executed
// Function where the piped system commands is executed
// We use the pipe function to get two file descriptors, then we redirect the output fd to the input fd
// We use the pipe function to get two file descriptors, then we redirect the output fd to the input fd
void
exec_job_piped
(
cmd_t
*
cmd
,
bool
foreground
)
void
exec_job_piped
(
cmd_t
*
cmd
,
bool
foreground
,
int
*
pid_job
)
{
{
// pipe() syscall needs an array of 2 fd
// pipe() syscall needs an array of 2 fd
// 0 is the read file descriptor (used by process 2)
// 0 is the read file descriptor (used by process 2)
...
@@ -251,6 +252,8 @@ void exec_job_piped(cmd_t *cmd, bool foreground)
...
@@ -251,6 +252,8 @@ void exec_job_piped(cmd_t *cmd, bool foreground)
// Fork to prepare running our first command
// Fork to prepare running our first command
pid
=
fork
();
pid
=
fork
();
*
pid_job
=
pid
;
if
(
pid
<
0
)
if
(
pid
<
0
)
{
{
printf
(
"Error while forking on the first processs
\n
"
);
printf
(
"Error while forking on the first processs
\n
"
);
...
...
This diff is collapsed.
Click to expand it.
bin/command.h
+
2
−
2
View file @
b435ebcd
...
@@ -22,11 +22,11 @@ void exec_builtin(char** argv, int argc);
...
@@ -22,11 +22,11 @@ void exec_builtin(char** argv, int argc);
//IN:
//IN:
// cmd: struct countaining the command to be executed
// cmd: struct countaining the command to be executed
// foreground: if the task has to be run in foreground or background ('&' sign)
// foreground: if the task has to be run in foreground or background ('&' sign)
void
exec_job
(
cmd_t
*
cmd
,
bool
foreground
);
void
exec_job
(
cmd_t
*
cmd
,
bool
foreground
,
pid_t
*
pid_job
);
// Execute a parsed pipe command as job (forks process)
// Execute a parsed pipe command as job (forks process)
//IN:
//IN:
// cmd: struct countaining the commands to be executed and piped together
// cmd: struct countaining the commands to be executed and piped together
// [UNUSED !!!] foreground: if the task has to be run in foreground or background ('&' sign)
// [UNUSED !!!] foreground: if the task has to be run in foreground or background ('&' sign)
void
exec_job_piped
(
cmd_t
*
cmd
,
bool
foreground
);
void
exec_job_piped
(
cmd_t
*
cmd
,
bool
foreground
,
pid_t
*
pid_job
);
#endif
#endif
\ No newline at end of file
This diff is collapsed.
Click to expand it.
bin/command.o
+
0
−
0
View file @
b435ebcd
No preview for this file type
This diff is collapsed.
Click to expand it.
bin/main.c
+
32
−
5
View file @
b435ebcd
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
#include
<stdlib.h>
#include
<stdlib.h>
#include
<errno.h>
#include
<errno.h>
#include
<signal.h>
#include
<signal.h>
#include
<unistd.h>
#include
<sys/types.h>
#include
<sys/types.h>
#include
<sys/wait.h>
#include
<sys/wait.h>
#include
<sys/signal.h>
#include
<sys/signal.h>
...
@@ -12,6 +13,9 @@
...
@@ -12,6 +13,9 @@
#define MAX_CMD_LEN 255
#define MAX_CMD_LEN 255
// Stores the PID of the current foreground job
int
MAIN_JOB_PID
=
0
;
void
sigchld_handler
(
int
signal
)
void
sigchld_handler
(
int
signal
)
{
{
int
status
;
int
status
;
...
@@ -28,13 +32,33 @@ void sigchld_handler(int signal)
...
@@ -28,13 +32,33 @@ void sigchld_handler(int signal)
}
}
}
}
void
sighup
(
int
signum
)
{
exit
(
EXIT_SUCCESS
);
}
void
sigint_job
(
int
signum
)
{
if
(
MAIN_JOB_PID
)
{
kill
(
MAIN_JOB_PID
,
signum
);
printf
(
"
\n
Foreground job interrupted.
\n
"
);
}
}
int
main
(
int
argc
,
char
*
const
argv
[])
int
main
(
int
argc
,
char
*
const
argv
[])
{
{
printf
(
BOLDCYAN
"Welcome to WaffleShell (#sh), use at your own risk !
\n
"
RESET
);
printf
(
BOLDCYAN
"Welcome to WaffleShell (#sh), use at your own risk !
\n
"
RESET
);
// Ignore "CTRL + C" (Interrupt signal)
// Redirect SIGHUP to all (main + subprocesses)
//signal(SIGINT, SIG_IGN);
signal
(
SIGHUP
,
sighup
);
//signal(SIGQUIT, SIG_IGN);
// Redirect SIGINT to the foreground job, else it's ignored
signal
(
SIGINT
,
sigint_job
);
// Ignore SIGTERM and SIGQUIT (default bash behaviour)
signal
(
SIGTERM
,
SIG_IGN
);
signal
(
SIGQUIT
,
SIG_IGN
);
// Prepare a handler to handle sigchild
// Prepare a handler to handle sigchild
struct
sigaction
sig_action
;
struct
sigaction
sig_action
;
...
@@ -51,6 +75,9 @@ int main(int argc, char *const argv[])
...
@@ -51,6 +75,9 @@ int main(int argc, char *const argv[])
while
(
true
)
while
(
true
)
{
{
// Reset main job PID
MAIN_JOB_PID
=
0
;
char
*
cmd_str
=
malloc
((
size_t
)
sizeof
(
char
)
*
MAX_CMD_LEN
);
char
*
cmd_str
=
malloc
((
size_t
)
sizeof
(
char
)
*
MAX_CMD_LEN
);
cmd_t
*
cmd_in
=
malloc
((
size_t
)
sizeof
(
cmd_t
));
cmd_t
*
cmd_in
=
malloc
((
size_t
)
sizeof
(
cmd_t
));
...
@@ -83,12 +110,12 @@ int main(int argc, char *const argv[])
...
@@ -83,12 +110,12 @@ int main(int argc, char *const argv[])
if
(
cmd_in
->
type
==
CMD_PIPE
)
if
(
cmd_in
->
type
==
CMD_PIPE
)
{
{
// Piped job
// Piped job
exec_job_piped
(
cmd_in
,
cmd_in
->
foreground
);
exec_job_piped
(
cmd_in
,
cmd_in
->
foreground
,
&
MAIN_JOB_PID
);
}
}
else
else
{
{
// Regular job (with or without input/output redirection)
// Regular job (with or without input/output redirection)
exec_job
(
cmd_in
,
cmd_in
->
foreground
);
exec_job
(
cmd_in
,
cmd_in
->
foreground
,
&
MAIN_JOB_PID
);
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
bin/main.o
+
0
−
0
View file @
b435ebcd
No preview for this file type
This diff is collapsed.
Click to expand it.
wshell
+
0
−
0
View file @
b435ebcd
No preview for this file type
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment