All of lore.kernel.org
 help / color / mirror / Atom feed
From: pravinshedage2008@gmail.com (Pravin Shedage)
To: kernelnewbies@lists.kernelnewbies.org
Subject: Issue using kernel linked list in user space
Date: Mon, 21 Sep 2015 12:26:12 +0530	[thread overview]
Message-ID: <CABzL1ga=Q8vnJntHBYLX5Y0+EQiS871szMQNyvECJZPVg7CEsQ@mail.gmail.com> (raw)
In-Reply-To: <CA+aCy1H7oW4eZdotb0pGijB=nt1EcO7=UWp8-wLACMuboU1nOw@mail.gmail.com>

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 

      reply	other threads:[~2015-09-21  6:56 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CABzL1ga=Q8vnJntHBYLX5Y0+EQiS871szMQNyvECJZPVg7CEsQ@mail.gmail.com' \
    --to=pravinshedage2008@gmail.com \
    --cc=kernelnewbies@lists.kernelnewbies.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.