Find the answer to your Linux question:
Results 1 to 10 of 10
hi..all i'm new to the kernel hacking ...... i'm trying to run the a system call as: In /usr/src/linux-2.6.26.6/include/linux/wait.h, I added struct evnt_queue { struct evnt_queue *next_event; wait_queue_head_t waitq; int ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2009
    Posts
    6

    problem in adding new system call


    hi..all i'm new to the kernel hacking ......
    i'm trying to run the a system call as:

    In /usr/src/linux-2.6.26.6/include/linux/wait.h, I added

    struct evnt_queue
    {
    struct evnt_queue *next_event;
    wait_queue_head_t waitq;
    int eid;
    };

    In /usr/src/linux-2.6.26.6/arch/x86/kernel/syscall_table_32.S I added
    .long sys_evntopen
    .long sys_evntclose /* 328 */
    .long sys_evntwait
    .long sys_evntsig

    In /usr/src/linux-2.6.26.6/include/asm-x86/unistd_32.h I added

    #define __NR_evntopen 327
    #define __NR_evntclose 328
    #define __NR_evntwait 329
    #define __NR_evntsig 330

    In /usr/src/linux-2.6.26.6/init/main.c I added

    struct evnt_queue *eq;

    static void __init event_init(void)
    {
    eq=(struct evnt_queue *)kmalloc(
    sizeof(struct evnt_queue), GFP_KERNEL);
    eq->next_event=NULL;
    eq->eid=0;
    init_waitqueue_head(&(eq->waitq));
    }
    asmlinkage void __init start_kernel(void)
    {
    ......
    event_init(); /* last statement */
    }


    In /usr/src/linux-2.6.26.6/sush/sys.c : I add the code of the new system call
    asmlinkage int sys_evntopen(int eid)
    {
    int newid=-1;
    struct evnt_queue *temp;

    printk("sys_evntopen");
    if (eq==NULL)
    {
    eq=(struct evnt_queue *)kmalloc(
    sizeof(struct evnt_queue), GFP_KERNEL);
    eq->next_event=NULL;
    eq->eid=0;
    init_waitqueue_head(&(eq->waitq));
    }

    temp=eq->next_event;
    if (eid>0)
    {
    while ((temp!=NULL)&&(temp->eid!=eid))
    {
    temp=temp->next_event;
    }
    if ((temp!=NULL)&&(temp->eid==eid)) newid=eid;
    }
    else if (eid==0)
    {
    while (temp!=NULL)
    {
    if (newid<temp->eid) newid=temp->eid;
    temp=temp->next_event;
    }
    newid++;
    if (newid<1) newid=1;
    temp=(struct evnt_queue *)kmalloc(
    sizeof(struct evnt_queue), GFP_KERNEL);
    if (temp==NULL) newid=-1;
    else
    {
    temp->eid=newid;
    temp->next_event=eq->next_event;
    eq->next_event=temp;
    init_waitqueue_head(&(temp->waitq));
    }
    }

    return(newid);
    }

    asmlinkage int sys_evntclose(int eid)
    {
    int result=-1;
    struct evnt_queue *prev, *temp;

    printk("sys_evntclose");
    prev=eq;
    temp=prev->next_event;
    while ((temp!=NULL)&&(temp->eid!=eid))
    {
    prev=temp;
    temp=prev->next_event;
    }

    if ((temp!=NULL)&&(waitqueue_active(&(temp->waitq))))
    wake_up_interruptible(&(temp->waitq));
    if (temp!=NULL)
    {
    result=0;
    prev->next_event=temp->next_event;
    kfree(temp);
    }

    return(result);
    }

    asmlinkage int sys_evntwait(int eid)
    {

    int result=-1;
    struct evnt_queue *temp;

    printk("sys_evntwait");
    temp=eq->next_event;
    while ((temp!=NULL)&&(temp->eid!=eid))
    {
    temp=temp->next_event;
    }

    if (temp!=NULL)
    {
    result=0;
    interruptible_sleep_on(&(temp->waitq));
    }

    return(result);
    }

    asmlinkage int sys_evntsig(int eid)
    {

    int result=-1;
    struct evnt_queue *temp;

    printk("sys_evntsig");
    temp=eq->next_event;
    while ((temp!=NULL)&&(temp->eid!=eid))
    {
    temp=temp->next_event;
    }

    if (temp!=NULL)
    {
    result=0;
    wake_up_interruptible(&(temp->waitq));
    }

    return(result);
    }


    And this is the test programm which use the system call :

    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include <linux/unistd.h>
    #include <linux/wait.h>
    #include <linux/errno.h>


    _syscall1(int, evntopen, int, eid);
    _syscall1(int, evntclose, int, eid);
    _syscall1(int, evntwait, int, eid);
    _syscall1(int, evntsig, int, eid);


    void *task_wait(void *eid) /* eventwait */
    {
    printf("event %d waiting\n", eid);
    evntwait((int)eid);
    printf("event %d waked up\n", eid);
    }


    void *task_signal(void *eid) /* eventsig */
    {
    printf("event %d signaling\n", eid);
    evntsig((int)eid);
    }


    int main(int arg, char ** argv)
    {
    int i, k, n;
    int eids[10];
    pthread_t tasks[10];

    n=3; /* open 3 threads */

    for (i=0; i<n; i++)
    {
    eids[i]=evntopen(0); /* open events */
    if (eids[i]>0) printf("event %d opened\n", eids[i]);
    else printf("event #%d could not open\n", i);
    }

    for (i=0; i<n; i++)
    {
    /* creat threads */
    pthread_create(&tasks[i], NULL, task_wait, (void *)eids[i]);
    sleep(2);
    }

    for (i=n-1; i>=0; i--)
    {
    pthread_create(&tasks[i], NULL, task_signal, (void *)eids[i]);
    sleep(2);
    }

    for (i=0; i<n; i++)
    {
    k=evntclose(eids[i]); /* test eventclose */
    if (k==0) printf("event %d closed\n", eids[i]);
    else printf("event %d could not close\n", eids[i]);
    }

    for (i=0; i<n; i++)
    {
    pthread_join(tasks[i], NULL);
    }

    return(0);
    }



    everything goes fine but while running the user program test.c,
    i'm getting the following output:

    event #0 could not open
    event #1 could not open
    event #2 could not open
    event -1 waiting
    event -1 waked up
    event -1 waiting
    event -1 waked up
    event -1 waiting
    event -1 waked up
    event -1 signaling
    event -1 signaling
    event -1 signaling
    event -1 could not close
    event -1 could not close
    event -1 could not close

    this is happening so because calling the first system call in my case is returning -1 value.
    in fact if i'm running any system call then every system call is returning -1 value
    what should i do???? where is the problem??????? i'm also looking at strace file but not getting any solution.
    thank you in advance.
    any help will be appreciable.

  2. #2
    Just Joined!
    Join Date
    Jul 2009
    Posts
    6
    plzzzz sumone help...........

  3. #3
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,763

    Exclamation

    I'm not sure whether you will receive exact answer from here. Drop mail here
    MailingList - Linux Kernel Newbies

    Try and post your strace output ,I think should provide some hints

    n fact if i'm running any system call then every system call is returning -1 value
    So obviously it's an error.To make sure write a simple c program with a system call and see whether that also producing errors

    Code:
    main(){
    char a='X';
    write(1,&a,1);
    }
    Does this gives error?
    First they ignore you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -----
    FOSS India Award winning ext3fs Undelete tool www.giis.co.in. Online Linux Terminal http://www.webminal.org

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Jul 2009
    Posts
    6
    thank u sir..
    i've attached the strace output.

    i've tried with the same program as u suggested me it gives no error...everything is ok in this case...

    -1 is an error...but why this is happening??? infact this is the case with every new system call which i'm implementing..

    thank you ...
    Attached Files Attached Files

  6. #5
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,763

    Exclamation

    Your strace says
    SYS_328(0xffffffff, 0xbf8af9b8, 0x804865f, 0xffffffff, 0) = -1 ENOSYS (Function not implemented)
    write(1, "event -1 could not close\n", 25event -1 could not close
    Error code details are
    ENOSYS:
    Function not implemented. This indicates that the function called is not implemented at all, either in the C library itself or in the operating system. When you get this error, you can be sure that this particular function will always fail with ENOSYS unless you install a new version of the C library or the operating system.
    Are you following proper guide for adding a new system call? You might have missed something ,somewhere...
    First they ignore you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -----
    FOSS India Award winning ext3fs Undelete tool www.giis.co.in. Online Linux Terminal http://www.webminal.org

  7. #6
    Just Joined!
    Join Date
    Jul 2009
    Posts
    6
    thank you sir thank you so much......
    sir i've installed & build a new kernel. and i think i'm following every steps involved correctly.sir still problem is dere it means sumthing wrong i'm doing.
    sir could you plz give me any way any suggetion so dat i can get out of this.

  8. #7
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,763
    what's the guide ,you are following? any book ?

    Best method would be try a simple "hello world" system call.If everything if fine,then you can go ahead with your program. If "hello world" didn't work,then there is something wrong with your custom kernel (may be missed some modules)

    I found this link :
    Adding a Custom System Call to Ubuntu - Tech Today
    First they ignore you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -----
    FOSS India Award winning ext3fs Undelete tool www.giis.co.in. Online Linux Terminal http://www.webminal.org

  9. #8
    Just Joined!
    Join Date
    Jul 2009
    Posts
    6
    no sir not any perticuler guide or book i'm following. i just do googling for dis dere i found so many tutes and link den for perticular architecture and kernel version i followd one of them.
    i go through the link u provided same thing i'm doing.....u r right it seems dere is sum problem with my custom kernel i'm going to install & compile another kernel....may be dat would help me out...
    thank you so much for your time sir.

  10. #9
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,763
    I just google out the above link So not sure about results.

    But here is another one,
    Kernel command using Linux system calls

    I would suggest you to download the same kernel version as mentioned in the documents.
    First they ignore you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -----
    FOSS India Award winning ext3fs Undelete tool www.giis.co.in. Online Linux Terminal http://www.webminal.org

  11. #10
    Just Joined!
    Join Date
    Jul 2009
    Posts
    6
    k sir thank you now i'm going tom try again with the same....
    thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •