Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Networking
User Name
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.


Closed Thread
  Search this Thread
Old 01-21-2009, 05:10 AM   #1
LQ Newbie
Registered: Jul 2008
Distribution: Red Hat
Posts: 7

Rep: Reputation: 0
Unhappy How to get link state of interface

How do I get the link state (whether cable is plugged in or not) of an ethernet interface? Is it enough to query the IFF_RUNNING flag in if_flags field of ifreq structure, via a SIOCGIFFLAGS ioctl, or is something more required.

(BTW, I already tried the ioctl method, with no luck. Below is the code I used.)

int retVal;
struct ifreq ifr;
retVal = ioctl(sockData->sockInt, SIOCGIFFLAGS, &ifr);
if (retVal < 0)
debug_printf(DEBUG_NORMAL, "Interface %s not found!\n", ctx->intName);
return FALSE;

if (((ifr.ifr_flags & IFF_UP) == IFF_UP) && ((ifr.ifr_flags & IFF_RUNNING) == IFF_RUNNING))
return TRUE;
} else {
SET_FLAG(ctx->flags, WAS_DOWN);
return FALSE;

Any ideas, guys?
Old 01-22-2009, 02:12 PM   #2
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,047
Blog Entries: 11

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
I'd be looking at the source of ethtool and see how they do it ;}
Old 01-28-2009, 07:19 AM   #3
LQ Newbie
Registered: Jul 2008
Distribution: Red Hat
Posts: 7

Original Poster
Rep: Reputation: 0

Thanks Tinkster. Tried it but no dice. Its using the netif_carrier_ok function, which I think cannot be used in userspace. Further on the netif_carrier_ok function tests the flag (or macro) __LINK_STATE_NOCARRIER, from a set of flags contained in net_device structure, which, again, cannot be used from userspace (I think ). Do correct me if I'm wrong.

Any other ideas, dude? Thanks in advance
Old 01-28-2009, 11:48 AM   #4
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
Save as (and make executable, chmod 775
ifconfig $1 | egrep 'UP |DOWN ' | sed "s/\s*\(\w*\)\s*.*/$1: \1/"
Usage: eth?

Example: eth0

eth0: UP

Last edited by JulianTosh; 01-28-2009 at 11:54 AM.
Old 01-29-2009, 12:18 AM   #5
LQ Newbie
Registered: Jul 2008
Distribution: Red Hat
Posts: 7

Original Poster
Rep: Reputation: 0

Perhaps i need to make myself clearer. I have to do this via a C program ONLY, no scripts allowed.

Another thing. I am doing this as part of running xsupplicant (open source Linux version). I notice that the the IFF_RUNNING flag is updated properly each time before I run xsupplicant. However, after I simply run xsupplicant once, the flag is no longer updated. Wierd.

Any ideas guys?
Old 11-16-2010, 05:48 AM   #6
LQ Newbie
Registered: Nov 2010
Posts: 3

Rep: Reputation: 0
do you have found working code sample?
Old 11-16-2010, 03:52 PM   #7
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 658Reputation: 658Reputation: 658Reputation: 658Reputation: 658Reputation: 658
klsc: Please look at the date of the last post to prevent resurrecting a thread that is almost two years old.

Closed Thread

ethernet, ioctl, link, state

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
not work: iptables -I INPUT 5 -m state --state NEW -m tcp -p tcp --dport 3306 -j DROP abefroman Linux - Security 1 07-18-2007 09:19 AM
LXer: State by state, Microsoft responds to creeping threat LXer Syndicated Linux News 0 05-01-2007 08:16 AM
Identify eth interface used by each pppoe link noisebleed Linux - Networking 6 04-19-2007 05:59 AM
need a link on multple network interface simulator kapsikum Linux User Groups (LUG) 1 02-14-2006 03:18 AM
How do I retrieve the link state? bb002 Linux - Networking 1 10-31-2005 02:49 AM

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

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