Skip to content

Assignment 3 part 2 haimts #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/github-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Run unit test
run: ./unit-test.sh
full-test:
container: cuaesd/aesd-autotest:assignment1
container: cuaesd/aesd-autotest:assignment3
runs-on: self-hosted
steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ crosstool-ng
aesd-build
.vscode/
test.sh.log
Test_*_Runner.c
build/
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ project(aesd-assignments)
set(AUTOTEST_SOURCES
test/assignment1/Test_hello.c
test/assignment1/Test_assignment_validate.c
../student-test/assignment1/Test_validate_username.c
test/assignment3/Test_systemcalls.c
)
# A list of all files containing test code that is used for assignment validation
set(TESTED_SOURCE
../examples/autotest-validate/autotest-validate.c
../examples/systemcalls/systemcalls.c
)
add_subdirectory(assignment-autotest)
4 changes: 4 additions & 0 deletions assignments/assignment2/cross-compile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Using built-in specs.
COLLECT_GCC=aarch64-linux-gnu-gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc-cross/aarch64-linux-gnu/11/lto-wrapper
/
1 change: 1 addition & 0 deletions assignments/assignment2/fileresult.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
writer: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, BuildID[sha1]=5dbecdf093d245d450bbe922259abd0281ef28d9, for GNU/Linux 3.7.0, with debug_info, not stripped
2 changes: 1 addition & 1 deletion conf/assignment.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
assignment1
assignment3
2 changes: 1 addition & 1 deletion conf/username.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
your-github-username-here-in-conf-file
haimts
2 changes: 1 addition & 1 deletion examples/autotest-validate/autotest-validate.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ bool this_function_returns_false()
*/
const char *my_username()
{
return "todo-please-enter-your-username-here-in-my_username";
return "haimts";
}
200 changes: 200 additions & 0 deletions examples/systemcalls/systemcalls.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
#include <stdlib.h>
#include <stdint.h> // general use
#include <unistd.h>
#include <signal.h> //
#include <wait.h> // waitpid()
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "systemcalls.h"

/**
* @param cmd the command to execute with system()
* @return true if the command in @param cmd was executed
* successfully using the system() call, false if an error occurred,
* either in invocation of the system() call, or if a non-zero return
* value was returned by the command issued in @param cmd.
*/
bool do_system(const char *cmd)
{

/*
* TODO add your code here
* Call the system() function with the command set in the cmd
* and return a boolean true if the system() call completed with success
* or false() if it returned a failure
*/
int rc = system(cmd);
if(cmd == NULL && rc == 0)
{
return false;
}
if (WIFSIGNALED(rc) &&
(WTERMSIG(rc) == SIGINT || WTERMSIG(rc) == SIGQUIT))
return false;

return true;
}

/**
* @param count -The numbers of variables passed to the function. The variables are command to execute.
* followed by arguments to pass to the command
* Since exec() does not perform path expansion, the command to execute needs
* to be an absolute path.
* @param ... - A list of 1 or more arguments after the @param count argument.
* The first is always the full path to the command to execute with execv()
* The remaining arguments are a list of arguments to pass to the command in execv()
* @return true if the command @param ... with arguments @param arguments were executed successfully
* using the execv() call, false if an error occurred, either in invocation of the
* fork, waitpid, or execv() command, or if a non-zero return value was returned
* by the command issued in @param arguments with the specified arguments.
*/

bool do_exec(int count, ...)
{
va_list args;
va_start(args, count);
char * command[count+1];
int i;
pid_t cpid, w;
int wstatus, ret = true;
for(i=0; i<count; i++)
{
command[i] = va_arg(args, char *);
}
command[count] = NULL;
/*
* TODO:
* Execute a system command by calling fork, execv(),
* and wait instead
pid_t childPid;of system (see LSP page 161).
* Use the command[0] as the full path to the command to execute
* (first argument to execv), and use the remaining arguments
* as second argument to the execv() command.
*
*/
fflush(stdout);
cpid = fork();
if (cpid == -1)
{
perror("fork");
ret = false;
} else if (cpid == 0) { /* Code executed by child */
printf("Child PID is %jd\n", (intmax_t) getpid());
if (-1 == execv(command[0], command))
{
printf("Unable to run command %s\n", command[0]);
_exit(1);
}
_exit(0);

} else { /* Code executed by parent */
w = waitpid(cpid, &wstatus, WUNTRACED | WCONTINUED);
if (w == -1) {
perror("waitpid");
ret = false;
}

if (WIFEXITED(wstatus)) {
printf("exited, status=%d\n", WEXITSTATUS(wstatus));
if (WEXITSTATUS(wstatus) != 0) {
ret = false;
}
} else if (WIFSIGNALED(wstatus)) {
printf("killed by signal %d\n", WTERMSIG(wstatus));
} else if (WIFSTOPPED(wstatus)) {
printf("stopped by signal %d\n", WSTOPSIG(wstatus));
} else if (WIFCONTINUED(wstatus)) {
printf("continued\n");
}
}

va_end(args);

return ret;
}

/**
* @param outputfile - The full path to the file to write with command output.
* This file will be closed at completion of the function call.
* All other parameters, see do_exec above
*/
bool do_exec_redirect(const char *outputfile, int count, ...)
{
va_list args;
va_start(args, count);
char * command[count+1];
int i;
pid_t cpid, w;
int wstatus, ret = true;
for(i=0; i<count; i++)
{
command[i] = va_arg(args, char *);
}
command[count] = NULL;

/*
* TODO
* Call execv, but first using https://stackoverflow.com/a/13784315/1446624 as a refernce,
* redirect standard out to a file specified by outputfile.
* The rest of the behaviour is same as do_exec()
*
*/
fflush(stdout);
cpid = fork();
if (cpid == -1)
{
perror("fork");
ret = false;
} else if (cpid == 0) { /* Code executed by child */
int fd;
fd = open(outputfile, O_WRONLY|O_TRUNC|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
if (dup2(fd , 1) < 0)
{
perror("dup2 fail");
_exit(-2);
}
if (-1 == execv(command[0], command))
{
printf("Unable to run command %s", command[0]);
_exit(1);
}
close(fd);
_exit(0);

} else { /* Code executed by parent */
w = waitpid(cpid, &wstatus, WUNTRACED | WCONTINUED);
if (w == -1) {
perror("waitpid");
ret = false;
}

if (WIFEXITED(wstatus)) {
printf("exited, status=%d\n", WEXITSTATUS(wstatus));
if (WEXITSTATUS(wstatus) != 0) {
ret = false;
}
} else if (WIFSIGNALED(wstatus)) {
printf("killed by signal %d\n", WTERMSIG(wstatus));
} else if (WIFSTOPPED(wstatus)) {
printf("stopped signal %d\n", WSTOPSIG(wstatus));
} else if (WIFCONTINUED(wstatus)) {
printf("continued\n");
}
}

va_end(args);

return ret;
}

/*
int main(int argc, char * argv[], char * envp[])
{
int ret = true;
do_system("ls -l");
do_exec(2, "/bin/ls", "-l");
do_exec_redirect("tempfile.tmp", 2, "/bin/ls", "-l", "-R");
return ret;
}
*/
9 changes: 9 additions & 0 deletions examples/systemcalls/systemcalls.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <stdio.h>
#include <stdbool.h>
#include <stdarg.h>

bool do_system(const char *command);

bool do_exec(int count, ...);

bool do_exec_redirect(const char *outputfile, int count, ...);
9 changes: 9 additions & 0 deletions finder-app/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CC=gcc
CFLAGS=-g -Wall
all: writer writer.o writer.c
writer: writer.o
$(CROSS_COMPILE)$(CC) $< -o $@
writer.o: writer.c
$(CROSS_COMPILE)$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -f writer.o writer
12 changes: 12 additions & 0 deletions finder-app/autorun-qemu.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh
cd $(dirname $0)
echo "Running test script"
./finder-test.sh
rc=$?
if [ ${rc} -eq 0 ]; then
echo "Completed with success!!"
else
echo "Completed with failure, failed with rc=${rc}"
fi
echo "finder-app execution complete, dropping to terminal"
/bin/sh
8 changes: 8 additions & 0 deletions finder-app/dependencies.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
# Dependency installation script for kernel build.
# Author: Siddhant Jajoo.


sudo apt-get install -y libssl-dev
sudo apt-get install -y u-boot-tools
sudo apt-get install -y qemu
4 changes: 2 additions & 2 deletions finder-app/finder-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ echo "Writing ${NUMFILES} files containing string ${WRITESTR} to ${WRITEDIR}"
rm -rf "${WRITEDIR}"

# create $WRITEDIR if not assignment1
assignment=`cat ../conf/assignment.txt`
assignment=`cat conf/assignment.txt`

if [ $assignment != 'assignment1' ]
then
Expand All @@ -54,7 +54,7 @@ fi

for i in $( seq 1 $NUMFILES)
do
./writer.sh "$WRITEDIR/${username}$i.txt" "$WRITESTR"
./writer "$WRITEDIR/${username}$i.txt" "$WRITESTR"
done

OUTPUTSTRING=$(./finder.sh "$WRITEDIR" "$WRITESTR")
Expand Down
18 changes: 18 additions & 0 deletions finder-app/finder.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/sh
filesdir=$1
searchstr=$2
if [ $# -ne 2 ]
then
echo must specify serch dir and tooken
exit 1
fi
if [ -d $1 ]
then
fileslist=$( ls -1 $filesdir )
filesCount=$( ls -1 $filesdir | wc -l )
searchInFilesCount=$( grep -r "$searchstr" $filesdir 2>/dev/null |wc -l )
echo The number of files are $filesCount and the number of matching lines are $searchInFilesCount
else
echo $1 not seems to be a directory
exit 1
fi
Loading