Solved

passing a function pointer through systam call

Posted on 2006-11-07
6
347 Views
Last Modified: 2010-04-22
Hi,

I would like to pass a function pointer to a function returning a void and receiving one void* argument, i.e
void (*ptr_to_func) (void*)
as an argument to a system call. This is for linux 2.6.16 kernel.
The system call is called nb_request. I compiled the system call into the kernel image. The system call is:

#include <linux/kernel.h>
#include <linux/unistd.h>
#include <linux/sched.h>
#include <asm/current.h>

asmlinkage int sys_nb_request(int num, void (*nb_handler) (void *), void *handler_arg) {
      printk(KERN_INFO "The number passed is %d.\n", num);
      /* do something with current */
      return 1;
}

The user space program that calls the system call, called invoker1.c, is:
#define __NR_nb_request 312
_syscall3(int, nb_request, int, num, void (*) (void *), nb_handler, void *, handler_arg);

void my_handler(void *p);

int main(int argc, char * argv){
      void *a;
      int j=8;
      a =&j;
      printf("a points to value %d\n", *((int *)a));
      fprintf(stdout,"Executing system call... ");
      nb_request(5, &my_handler, a);
      fprintf(stdout,"Finished now\n");
      return 0;
}

void my_handler(void *p)
{
      //does something with p
}

My problem is that the compilation of the user-space program dies at line:
_syscall3(int, nb_request, int, num, void (*) (void *), nb_handler, void *, handler_arg);
and this is because I don't know how to define the pointer to a function in the _syscallN macro.
I'm using User Mode Linux, and I compile with
gcc -o invoke1 invoker.c -I./include
and invoker.c is placed in the root directory of the kernel tree.

Can someone, please, tell me how make this work?! I've been searching the web, but could not find anything except how to pass variables or void*

Mirna
0
Comment
Question by:mirnalim
  • 3
  • 3
6 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 17901871
_syscall3(int, nb_request, int, num, void (*) (void *), nb_handler, void *, handler_arg);
here nb_request looks like a typedef
      nb_request(5, &my_handler, a);
here nb_request looks like a function
which is it?
0
 

Author Comment

by:mirnalim
ID: 17903040
nb_request is a function that accepts three arguments. If I delete the second argument, i.e. void (*nb_handler) (void *), then the code compiles.
_syscall3(int, nb_request, int, num, void (*) (void *), nb_handler, void *, handler_arg);
is in the user space function that wants to call nb_request system call.
(I know now that I shouldn't be passing variables that lie in the caller stack, but let's forget that for a moment.)
The system call implementation is in the arch/um/kernel/nb_request.c, and looks just as in the message above. I hope this helps.
gcc complains that nb_handler is unrecognized. So my problem is, how do I declare the _syscall3 macro to pass a function pointer for the second argument.
0
 
LVL 84

Expert Comment

by:ozo
ID: 17905095
void(int,void (void *), void *)
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:mirnalim
ID: 17907532
I tried using it but still it doesn't work. Could you, please, write what the _syscall3 macro should look like? Do I need to change the definition of sys_nb_request?
0
 
LVL 84

Expert Comment

by:ozo
ID: 17911225
how is it defined now?
how did you want to use it?
in the code above it looked more like a function declatation than a macro
0
 

Author Comment

by:mirnalim
ID: 17912395
I talked with a friend and he figured out from your answer how I need to declare it. The problem was I didn't have a typdef for the function pointer. So the code now looks like this:
#include <asm/arch/unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

typedef void (*handler_ptr_type)(void*);

_syscall3(int, nb_request, int, num, handler_ptr_type, nb_handler, void*, handler_arg);

void my_handler(void *p);

int main(int argc, char * argv){
      int *t;
      void *a;
      t = (int *)malloc(sizeof(int));
      a = (void *)malloc(sizeof(int));
      *t = 8;
      *((int *)a) = 11;
      printf("t points to value %d\n", *t);
      printf("a points to value %d\n", *((int *)a));
      fprintf(stdout,"Executing system call... ");
      /*nb_request(5, &my_handler, a);*/
      fprintf(stdout,"Finished now\n");
      return 0;
}

void my_handler(void *p)
{
      //does something with p
}

while the implementation of the syscall (sys_nb_request.c) is the same one. And this works.
Thank you very much for your help. I really appreciate it!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Shell script awk command 2 91
generating rpm using noarch parameter 1 35
Error while installing rpm 1 63
Problem to run file 9 90
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now