LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-09-2005, 04:13 PM   #1
alltime
Member
 
Registered: Jan 2005
Distribution: Mandrake 10.1 with kernel 2.6.10
Posts: 60

Rep: Reputation: 15
strange segmentation fault


I am creating a program function to display the Ethernet cards MAC address. For some reaon, I am getting a segmentation fault. Shouldn't displaying the info be as simple as what I have below?
Code:
        struct net_device *dev;
        struct sk_buff *skb;
        struct smap_chan *smap;
        
/* Gather information from the kernel */
int cpu_info( char *buf, char **start, off_t off, int count )
{              
        printk(KERN_INFO "%s - %02x:%02x:%02x:%02x:%02x:%02x\n", dev->name
                                                               , dev->dev_addr[0]
                                                               , dev->dev_addr[1]
                                                               , dev->dev_addr[2]
                                                               , dev->dev_addr[3]
                                                               , dev->dev_addr[4]
                                                               , dev->dev_addr[5]);
}
I am declaring the following header files
Quote:
#include <linux/pci.h> /* For gathering MAC information */
#include <linux/kernel.h> /* for struct sysinfo */
#include <linux/unistd.h> /* for _syscallX macros/related stuff */
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/ioport.h>
#include <linux/skbuff.h>
#include <asm/processor.h>
#include <asm/bitops.h>
#include <asm/io.h>
 
Old 11-09-2005, 05:26 PM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Where do you get "dev" from?
 
Old 11-09-2005, 05:59 PM   #3
alltime
Member
 
Registered: Jan 2005
Distribution: Mandrake 10.1 with kernel 2.6.10
Posts: 60

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by Matir
Where do you get "dev" from?
dev is from struct net_device *dev;

dev_addr should display the hardware address (MAC).
 
Old 11-09-2005, 06:49 PM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
I think he means, where do you set dev to something. Just declaring it as a global as you have done there makes it a dangling pointer. Dangling pointers are bad and cause segfaults, which is what you are seeing. Somewhere in your code, you must have something on the order of:

dev = somefunctiontosetdev();

or maybe:
dev = malloc(sizeof(net_device));
somefunctiontosetdevproperties(dev);

Or maybe even:
somefunctiontofillindev(&dev);

Just having:

net_device *dev;

You can't expect it to magically have memory allocated for it and get all it's data filled in for you...

Not knowing much about the net_device struct, I can't say what exactly you need to call to initialize it with what you want, but maybe someone else here does.

Last edited by deiussum; 11-09-2005 at 06:50 PM.
 
Old 11-10-2005, 02:49 AM   #5
enthusiast_abdul
LQ Newbie
 
Registered: Oct 2005
Location: India
Distribution: Redhat 9.0
Posts: 20

Rep: Reputation: 0
first of all you have to initialise dev.
This can be done by :
struct net_device *dev=__dev_get_by_name("eth0");
This fn. is defined in /usr/src/linux-2.4.20-8/net/core/dev.c.
eth0 is the name of the ethernet interface.
This should prevent the seg. fault.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
Segmentation Fault ceenu99 Linux - Software 0 07-20-2005 05:42 AM
Segmentation fault, So strange.. sonitbk Programming 7 04-22-2004 08:17 PM
Segmentation fault compjinx Linux - Newbie 4 11-09-2003 04:51 PM
What is segmentation fault? Tommi Linux - General 11 05-06-2003 12:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration