Solved

passing a function pointer through systam call

Posted on 2006-11-07
6
356 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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 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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

636 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