Solved

passing a function pointer through systam call

Posted on 2006-11-07
6
350 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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 tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

808 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