Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Add a Static System Call to the Linux Kernel Write a new static system call to incorporate to your custom Linux Kernel. The system call

Add a Static System Call to the Linux Kernel

Write a new static system call to incorporate to your custom Linux Kernel. The system call you write should take one argument and return the process state information for that process, its child processes, child threads and sibling processes. The prototype for your system call will be:

asmlinkage long sys_pinfo(struct pinfo *info);

pid_t is defined in /usr/include/sys/types.h and /usr/include/linux/types.h for user-space programs and in /usr/src/linux/include/linux/types.h(/usr/src/linux/include/uapi/asm-generic/posix_types.h) for kernel-space routines.

Also see /usr/include/asm-generic/posix_types.h

Define struct pinfo in /usr/src/linux/include/linux/pinfo.h as part of your solution (localize it for your test C program):

#include

struct pinfo {

pid_t pid; /* process id */

long state; /* current state of process */

long nice; /* process nice value */

pid_t parent_pid; /* process id of parent */

int nr_children; /* total number of child processes */

int nr_threads; /* total number of child threads */

pid_t youngest_child_pid; /* pid of youngest child */

pid_t younger_sibling_pid; /* pid of younger sibling */

pid_t older_sibling_pid; /* pid of older sibling */

unsigned long start_time; /* process start time */

long user_time; /* CPU time spent in user mode */

long sys_time; /* CPU time spent in system mode */

long cutime; /* total user time of children */ long cstime; /* total system time of children */

long uid; /* user id of process owner */

char comm[16]; /* name of program executed */

};

Sibling processes are those sharing the same parent. Younger, youngest and older comparisons between processes are made based on their start time.

Your system call should return 0 unless an error occurs. Your code should handle errors that can occur but not handle any errors that cannot occur. At a minimum, your system call should detect the following conditions and respond as described:

If the address for the pinfo structure is null, return -22.

If a value to be set in pinfo is accessible through a pointer which is null, set the corresponding field value in pinfo to -1. For example, the youngest_child_pid should be set to -1 if the process does not have a child. The referenced error code is defined as EINVAL in /usr/src/linux/include/uapi/asm-generic/errno-base.h /usr/src/linux/include/linux/errno.h /usr/src/linux/usr/include/asm/errno.h.

Each system call must be assigned a number which becomes its position in the system call vector. You must identify an available spot for your new system call in the vector for x86_64 system calls (see /usr/src/linux/arch/x86/syscalls/syscall_64.tbl and /usr/src/linux/include/uapi/asm-generic/unistd.h)

Linux maintains a list of all processes in a doubly linked list. Each entry in this list is a task_struct structure, which is defined in /usr/src/linux/include/linux/sched.h.

In /usr/src/linux/include/asm-generic/current.h, current is defined to inline a function which returns the address of the task_struct of the currently running process. All of the information to be returned in the pinfo structure can be determined by starting with current. Note that current.h includes /usr/src/linux/include/linux/thread_info.h

See /usr/src/linux/arch/x86/include/asm/thread_info.h

See code in:

/usr/src/linux/arch/x86/syscalls/syscall_64.tbl

/usr/src/linux/arch/x86/syscalls/syscall_32.tbl

Also see:

/usr/src/linux/include/uapi/asm-generic/unistd.h

/usr/include/asm/unistd.h

/usr/src/linux/include/linux/syscalls.h

/usr/src/linux/kernel/kallsyms.c

Add the following line below the entry that starts with 326 to /usr/src/linux/arch/x86/syscalls/syscall_64.tbl

327 common mypinfo sys_pinfo

Add the following lines to /usr/src/linux/include/linux/syscalls.h at the proper spots:

struct pinfo;

#include

asmlinkage long sys_pinfo(struct pinfo *info);

Create a directory in /usr/src/linux and call it pinfo. This directory should contain the implementation of your new static system call.

Edit the main Makefile in /usr/src/linux/Makefile and add the new pinfo/ directory to the following line:

core-y += kernel/ mm/ fs/ ipc/ security/ crypto/ block/ pinfo/

Create a Makefile in /usr/src/linux/pinfo with the following line in it:

obj-y := pinfo.o

Develop and implement the code of your new static system call in /usr/src/linux/pinfo.c The following should serve as a base template. Make sure to author and properly document your code in this source file.

See /usr/src/linux/include/linux/cred.h

See /usr/src/linux/include/linux/uidgid.h

struct pinfo;

#include

#include

#include

#include

#include

#include

#include

#include

#include

asmlinkage long sys_pinfo(struct pinfo *info)

{

// Local Declarations

if(info == NULL)

return -22;

// Main Logic

return 0;

}

Build your custom kernel and re-deploy it following the same steps you performed in Assignment 1 when you initially built and deployed the custom kernel in your system. Then make sure to boot your system from the custom made kernel that now includes the new system call and proceed to run your test program to verify the correctness of your new system call routine.

Testing your System Call

To test your system call, write a simple program which creates a few child processes and a few child threads before it calls the new pinfo system call.Your program should print all the process state information for the calling process. Run the program several times. Which fields in the pinfo structure change? Which ones do not? For each field in the pinfo structure, discuss how frequently it changes and why as part of your experiments and document in your learning report.

Although system calls are generally accessed through a library (glibc), your test program should access your system call directly. This is accomplished by utilizing the syscall() function. See man syscall. Your test program may include code like the following:

#include

#include

#include pinfo.h

Int main()

{

struct pinfo p_info;

int status;

/* create child processes and keep em active */

/* create child threads and keepem active */

/* If this is the parent process, call new system call */

status = syscall (327, &p_info); //NOTE: use proper system call number designated in the system call table file (/usr/src/linux/arch/x86/syscalls/syscall_64.tbl)

/* and output to standard output the info returned by new system call */

printf("p_info: ");

printf(

"pid = %d "

"state = %ld "

"nice = %ld "

"parent_pid = %d "

"children = %d "

"youngest_child_pid = %d "

"younger_sibling_pid = %d "

"older_sibling_pid = %d "

"start_time = %lu "

"user_time = %ld "

"sys_time = %ld "

"uid = %ld "

"comm = %s ",

p_info.pid, p_info.state, p_info.nice, p_info.parent_pid, p_info.children,

p_info.youngest_child_pid, p_info.younger_sibling_pid, p_info.older_sibling_pid,

p_info.start_time, p_info.user_time, p_info.sys_time, p_info.uid, p_info.comm);

exit(0);

}

Consider setting up a shell script from which you start several background processes (i.e. sleep x) prior to launching your testing C program so as to register siblings with a common parent. The child processes and child threads created from within your testing C program should block or sleep long enough to allow the new system call to collect statistics for their parent.

The output of the program should be easy to read. The ps and top commands will provide valuable help in verifying the accuracy of information printed by your program. You can access detailed information on these commands on the man pages.

-------------------------------------------------------------------------------------------------------------------------------------------------------

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What will you do or say to Anthony about this issue?

Answered: 1 week ago