Issue using kernel linked list in user space

Pravin Shedage pravinshedage2008 at gmail.com
Mon Sep 21 02:56:12 EDT 2015


On Sun, Sep 20, 2015 at 12:35 PM, Pranay Srivastava <pranjas at gmail.com>
wrote:
> On Sat, Sep 19, 2015 at 10:26 PM, Gunjan Mehta <gunjanmehta08 at 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 


More information about the Kernelnewbies mailing list