Solved

passing a function pointer through systam call

Posted on 2006-11-07
6
355 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
linux rpm install 5 239
Shell script awk command 2 123
How to make linux dowload dependencies mentioned in the requires tag in rpm spec 5 67
Linux: disable vim auto-comment 7 171
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…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

732 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