All of lore.kernel.org
 help / color / mirror / Atom feed
* Issue using kernel linked list in user space
@ 2015-09-19 16:56 Gunjan Mehta
  2015-09-20  7:05 ` Pranay Srivastava
  0 siblings, 1 reply; 3+ messages in thread
From: Gunjan Mehta @ 2015-09-19 16:56 UTC (permalink / raw)
  To: kernelnewbies

I have a linked list like this http://pastebin.com/bwF3jLb6 . The problem i
am facing is i have initialized the list head and
 then i am doing malloc for that struct object. What i need is i want to
make struct abc_st *r as head of linked list and other nodes after it.

typedef struct abc {
char client_id[18];
    char mac[18];
    st_list_head list;
}abc_st;

abc_st* check_fields (abc_st *ptr)
{
    char cmd[500];
    MYSQL_RES *result;
    MYSQL_ROW row;
    int num_rows;
    abc_st *r=NULL,*temp=NULL;
    INIT_LIST_HEAD(&r->list);   // i have intialized the head here

    sprintf(cmd, "SELECT * FROM ABCD_TABLE WHERE MAC = %.4x", ptr->mac);
        /* Running the sql query to check for fields with value in database
*/
    if (mysql_query(abc_db.db_handle, cmd)) {
        num_rows = -1;
        goto done;
    }
    result = mysql_store_result(abc_db.db_handle);
    if (result == NULL) {
        num_rows = -1;
        goto done;
    }
    num_rows = mysql_num_rows(result);

    while ((row = mysql_fetch_row(result)))
    {
        r= (abc_st *)malloc(sizeof(abc_st));
        memcpy(r->mac,row[1],strlen(row[1])+1);
        memcpy(r->client_id,row[0],strlen(row[0])+1);
How should i use list_add ?
        //list_add(struct list_head *new, struct list_head *head)
 //prototype of list_add
       // list_add(&temp->list,&r->list);  //it will go wrong here, i want
to make r(structure r as head and then add other strutcure objects)
     //   r->link = NULL;
        //list_add(r, ptr);

    }
done:
    mysql_free_result(result);

    return r;

}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20150919/a1854c7c/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: list.h
Type: text/x-chdr
Size: 1970 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20150919/a1854c7c/attachment.bin 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Issue using kernel linked list in user space
  2015-09-19 16:56 Issue using kernel linked list in user space Gunjan Mehta
@ 2015-09-20  7:05 ` Pranay Srivastava
  2015-09-21  6:56   ` Pravin Shedage
  0 siblings, 1 reply; 3+ messages in thread
From: Pranay Srivastava @ 2015-09-20  7:05 UTC (permalink / raw)
  To: kernelnewbies

On Sat, Sep 19, 2015 at 10:26 PM, Gunjan Mehta <gunjanmehta08@gmail.com> wrote:
> I have a linked list like this http://pastebin.com/bwF3jLb6 . The problem i
> am facing is i have initialized the list head and
>  then i am doing malloc for that struct object. What i need is i want to
> make struct abc_st *r as head of linked list and other nodes after it.
>
> typedef struct abc {
> char client_id[18];
>     char mac[18];
>     st_list_head list;
> }abc_st;
>
> abc_st* check_fields (abc_st *ptr)
> {
>     char cmd[500];
>     MYSQL_RES *result;
>     MYSQL_ROW row;
>     int num_rows;
>     abc_st *r=NULL,*temp=NULL;

huh? this is initialized?
>     INIT_LIST_HEAD(&r->list);   // i have intialized the head here

>
>     sprintf(cmd, "SELECT * FROM ABCD_TABLE WHERE MAC = %.4x", ptr->mac);
>         /* Running the sql query to check for fields with value in database
> */
>     if (mysql_query(abc_db.db_handle, cmd)) {
>         num_rows = -1;
>         goto done;
>     }
>     result = mysql_store_result(abc_db.db_handle);
>     if (result == NULL) {
>         num_rows = -1;
>         goto done;
>     }
>     num_rows = mysql_num_rows(result);
>
>     while ((row = mysql_fetch_row(result)))
>     {
>         r= (abc_st *)malloc(sizeof(abc_st));
>         memcpy(r->mac,row[1],strlen(row[1])+1);
>         memcpy(r->client_id,row[0],strlen(row[0])+1);
> How should i use list_add ?
>         //list_add(struct list_head *new, struct list_head *head)
> //prototype of list_add
>        // list_add(&temp->list,&r->list);  //it will go wrong here, i want
> to make r(structure r as head and then add other strutcure objects)
>      //   r->link = NULL;
>         //list_add(r, ptr);
>
>     }
> done:
>     mysql_free_result(result);
>
>     return r;
>
> }
>
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>



-- 
        ---P.K.S

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Issue using kernel linked list in user space
  2015-09-20  7:05 ` Pranay Srivastava
@ 2015-09-21  6:56   ` Pravin Shedage
  0 siblings, 0 replies; 3+ messages in thread
From: Pravin Shedage @ 2015-09-21  6:56 UTC (permalink / raw)
  To: kernelnewbies

On Sun, Sep 20, 2015 at 12:35 PM, Pranay Srivastava <pranjas@gmail.com>
wrote:
> On Sat, Sep 19, 2015 at 10:26 PM, Gunjan Mehta <gunjanmehta08@gmail.com>
wrote:
>> I have a linked list like this http://pastebin.com/bwF3jLb6 . The
problem i
>> am facing is i have initialized the list head and
>>  then i am doing malloc for that struct object. What i need is i want to
>> make struct abc_st *r as head of linked list and other nodes after it.
>>
>> typedef struct abc {
>> char client_id[18];
>>     char mac[18];
>>     st_list_head list;
>> }abc_st;
>>
>> abc_st* check_fields (abc_st *ptr)
>> {
>>     char cmd[500];
>>     MYSQL_RES *result;
>>     MYSQL_ROW row;
>>     int num_rows;
>>     abc_st *r=NULL,*temp=NULL;
>
> huh? this is initialized?
>>     INIT_LIST_HEAD(&r->list);   // i have intialized the head here
>
>>
>>     sprintf(cmd, "SELECT * FROM ABCD_TABLE WHERE MAC = %.4x", ptr->mac);
>>         /* Running the sql query to check for fields with value in
database
>> */
>>     if (mysql_query(abc_db.db_handle, cmd)) {
>>         num_rows = -1;
>>         goto done;
>>     }
>>     result = mysql_store_result(abc_db.db_handle);
>>     if (result == NULL) {
>>         num_rows = -1;
>>         goto done;
>>     }
>>     num_rows = mysql_num_rows(result);
>>
>>     while ((row = mysql_fetch_row(result)))
>>     {
>>         r= (abc_st *)malloc(sizeof(abc_st));
>>         memcpy(r->mac,row[1],strlen(row[1])+1);
>>         memcpy(r->client_id,row[0],strlen(row[0])+1);
>> How should i use list_add ?
>>         //list_add(struct list_head *new, struct list_head *head)
>> //prototype of list_add
>>        // list_add(&temp->list,&r->list);  //it will go wrong here, i
want
>> to make r(structure r as head and then add other strutcure objects)
>>      //   r->link = NULL;
>>         //list_add(r, ptr);
>>
>>     }
>> done:
>>     mysql_free_result(result);
>>
>>     return r;
>>
>> }
>>
>>
>> _______________________________________________
>> Kernelnewbies mailing list
>> Kernelnewbies at kernelnewbies.org
>> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>>
>
>
>
> --
>         ---P.K.S
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


INIT_LIST_HEAD gets a struct list head * and initializes list->next &
list->prev.
Use calloc to initialize other members from struct abc.

st_list_head list;
I hope this will be typedef of struct list_head.

INIT_LIST_HEAD(&r->list); <-- This indicate r as a List Head.

Just small change you need. r remains be your list head. To add new node,
create newnode of type (abc_st *) and add it in the list.
check the code modification which clarify you better.


typedef struct abc {
char client_id[18];
    char mac[18];
    st_list_head list;
}abc_st;

abc_st* check_fields (abc_st *ptr)
{
    char cmd[500];
    MYSQL_RES *result;
    MYSQL_ROW row;
    int num_rows;
    abc_st *r=NULL,*temp=NULL;
    abc_st *newnode = NULL;
    INIT_LIST_HEAD(&r->list);   // i have intialized the head here

    sprintf(cmd, "SELECT * FROM ABCD_TABLE WHERE MAC = %.4x", ptr->mac);
        /* Running the sql query to check for fields with value in database
*/
    if (mysql_query(abc_db.db_handle, cmd)) {
        num_rows = -1;
        goto done;
    }
    result = mysql_store_result(abc_db.db_handle);
    if (result == NULL) {
        num_rows = -1;
        goto done;
    }
    num_rows = mysql_num_rows(result);

    while ((row = mysql_fetch_row(result)))
    {
        if ((newnode = (abc_st *)calloc(sizeof(abc_st))) == NULL) {
            fprintf(stderr, "%s\n", strerror(errno));
            goto done;
        }
        memcpy(newnode->mac,row[1],strlen(row[1])+1);
        memcpy(newnode->client_id,row[0],strlen(row[0])+1);

        list_add(&(newnode->list), &(r->list));
    }
done:
    mysql_free_result(result);

    return r;

}

- Thanks & Regards,
    PraviN
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20150921/3df3f2ee/attachment.html 

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-09-21  6:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-09-19 16:56 Issue using kernel linked list in user space Gunjan Mehta
2015-09-20  7:05 ` Pranay Srivastava
2015-09-21  6:56   ` Pravin Shedage

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.