I have added a hello world system call to Linux kernel 3.16, then I compile and ran it. I called my system call by syscall function but it did not print any thing and output of syscall function was not -1.
This is my system call code:
#include <linux/kernel.h>
asmlinkage long sys_hello(void){
printk("hello world\n");
return 0;
}
and this my c program code to call my system call:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <string.h>
#include <errno.h>
int main(void){
printf("function\n");
if(syscall(317)==-1){
printf("no\n");
}
else{
printf("yes\n");
}
return 0;
}
The output of c program is:
function
yes
How can I find my system call is added to kernel correctly?
printk wouldn't necessarily print to your current tty; to see your message use the dmesg command in your shell. See also this one if it does not show up on dmesg
Related
I have an assignment to do which requires me to use fork(), wait and execv(). However, every time I try to use them I get an error that they aren't defined. Here is my code:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
fork();
printf("Hello world!\n");
return 0;
}
Here is the errors I get:
I would appreciate it if anyone could help.
Using Ubuntu 18.04.3 LTS (GNU/Linux 5.0.21+ x86_64)
I created a simple syscall and am trying to pass input (the integer 5) through it by calling it in a c program and then see if I can get the input back out. I keep getting random junk as my output rather than my input.
Simple system call:
#include<linux/kernel.h>
#include<linux/init.h>
#include<linux/sched.h>
#include<linux/syscalls.h>
#include<linux/signal.h>
#include "tags.h"
asmlinkage int sys_get_tag(int pid)
{
return pid;
}
my userspace test code:
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <linux/kernel.h>
#include <sys/syscall.h>
#include <unistd.h>
int main()
{
int pid = syscall(335, 5); // 335 is the syscall number, 5 is the input to the syscall that I am trying to return
printf("return: %d \n", pid);
return 0;
}
my output:
return: 4980568
I get it to work when I return some integer in my syscall, like return 5; for example; so I know the syscall is being used.
I'm trying to use the memfd_create syscall in my C code. I tried to include sys/memfd.h as the man page for memfd_create says is appropriate, but GCC gives me an error "sys/memfd: No such file or directory".
I've tried Googling around and couldn't find anyone having the same problem. I noticed some versions of the manpage for memfd_create say that I should include sys/mman.h, but it didn't seem to help when I tried it. It would say memfd_create was implicitly declared.
Here is a minimal reproduction of my problem.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/memfd.h>
int main(){
int fd;
fd = memfd_create("test", MFD_CLOEXEC);
return 0;
}
I expect the above code to compile and run without error.
On older systems, you'll have to include linux/memfd.h for the MFD_ defines, and call memfd_create() via the the syscall(2) wrapper (and include unistd.h and sys/syscall.h for it work).
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <linux/memfd.h>
#include <err.h>
int main(void){
int fd;
if((fd = syscall(SYS_memfd_create, "test", MFD_CLOEXEC)) == -1)
err(1, "memfd_create");
return 0;
}
The Ubuntu man-pages in Bionic (18.04) are not up to date with this API (including its implementation in Bionic).
The Focal man-page correctly shows how to include memfd_create(). It says:
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <sys/mman.h>
So you only need to include <sys/mman.h>, and you need to build with -D_GNU_SOURCE in your compiler flags. Or, do as the man page says and literally #define _GNU_SOURCE before including the header. However, I recommend just compiling with -D_GNU_SOURCE instead.
I have a simple C Wrapper program to run a bash script in an elevated context. It sits behind a FastCGI wrapper and allows some service hooks to call my C Program which will then run my bash script as root. I am well aware of the security issues and my web server only allows a single IP address to call CGI-BIN scripts. I own both machines so there is little to no security risk at all. I am a complete noob a C and have literally copied a snippet off the internet.
So far this has worked fine:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
setuid( 0 );
return system( "./myscript.sh" );
}
However my program now needs to take in POST data from FastCGI. The CGI spec says it passes all raw POST data in via STDIN. What I would like is to be able to directly pipe this raw POST data from the STDIN of my C Program/Wrapper into my script. I've tried the following which didn't work:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
setuid( 0 );
dup2(1, 0);
return system( "./myscript.sh" );
}
The script works perfectly fine when using normal piping (eg. echo "Hey" | ./myscript.sh) however I am lost for how to pipe the STDIN of my C Program to the STDIN of my script.
Expanding on what Barmar commented above. Here is your modified C program:
/* foo.c*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
setuid( 0 );
return system( "wc -l");
}
The wc -l command will wait for input from your STDIN.
Now, compile and run the above program. You will see that it waits for the input and when you end the input using a Ctrl + D, you will see that it prints the number of lines:
/tmp> ./foo
Hello world
This is an input
2
Try this to add a newline at the end of the stdin:
int main()
{
setuid( 0 );
return system( "(cat; echo '') | ./myscript.sh" );
}
I am using the Big Nerd Ranch book Objective-C Programming, and it starts out by having us write in C in the first few chapters. In one of my programs it has me create, I use the sleep function. In the book it told me to put #include <stdlib.h> under the #include <stdio.h> part. This is supposed to get rid of the warning that says "Implicit declaration of function 'sleep' is invalid in C99". But for some reason after I put #include <stdlib.h>, the warning does not go away.. This problem does not stop the program from running fine, but I was just curious on which #include I needed to use!
The sleep man page says it is declared in <unistd.h>.
Synopsis:
#include <unistd.h>
unsigned int sleep(unsigned int seconds);
sleep is a non-standard function.
On UNIX, you shall include <unistd.h>.
On MS-Windows, Sleep is rather from <windows.h>.
In every case, check the documentation.
this is what I use for a cross-platform code:
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
int main()
{
pollingDelay = 100
//do stuff
//sleep:
#ifdef _WIN32
Sleep(pollingDelay);
#else
usleep(pollingDelay*1000); /* sleep for 100 milliSeconds */
#endif
//do stuff again
return 0;
}
What is the proper #include for the function 'sleep()'?
sleep() isn't Standard C, but POSIX so it should be:
#include <unistd.h>
sleep(3) is in unistd.h, not stdlib.h. Type man 3 sleep on your command line to confirm for your machine, but I presume you're on a Mac since you're learning Objective-C, and on a Mac, you need unistd.h.
Given that sleep is a non-standard function, I created a sleep function with the standard library time.h
#include <time.h>
void sleep(double s) {
time_t cur_time = time(NULL);
while ((difftime(time(NULL), cur_time)) < s);
}