ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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>
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.