×

Announcing: Slashdot Deals - Explore geek apps, games, gadgets and more. (what is this?)

Thank you!

We are sorry to see you leave - Beta is different and we value the time you took to try it out. Before you decide to go, please take a look at some value-adds for Beta and learn more about it. Thank you for reading Slashdot, and for making the site better!

Become a Linux Kernel Hacker and Write Your Own Module

Soulskill posted about 7 months ago | from the or-else dept.

Programming 143

M-Saunders (706738) writes "It might sound daunting, but kernel hacking isn't a mysterious black art reserved for the geekiest of programmers. With a bit of background knowledge, anyone with a grounding in C can implement a new kernel module and understand how the kernel works internally. Linux Voice explains how to write a module that creates a new device node, /dev/reverse, that reverses a string when it's written to it. Sure, it's not the most practical example in the world, but it's a good starting point for your own projects, and gives you an insight into how it all fits together."

Sorry! There are no comments related to the filter you selected.

Oh no! (5, Funny)

fizzer06 (1500649) | about 7 months ago | (#47102503)

Just to have Linus publicly cuss me out? No thanks!

just because (5, Funny)

Anonymous Coward | about 7 months ago | (#47102599)

you are 100% required to send all your code to linus, even your test builds

Re:just because (-1)

jcochran (309950) | about 7 months ago | (#47102633)

Typical misconception of GPL. You are required to share any code modifications /IF/ you distribute the modified executable. If your changes are for your use only, feel free to keep 'em to yourself.

Although to be honest, if I had mod points, I would have rated fizzer06's comment as +1 Funny.

Re:just because (3, Funny)

Anonymous Coward | about 7 months ago | (#47102647)

No it was simply a joke Admiral Aspergers.

Re:just because (0)

Jeremiah Cornelius (137) | about 7 months ago | (#47102705)

Will this proposed kernel module auto-genrate Edward Snowden stories for the Slashdot frontpage, with a POST through the kHTTPd?

Will these stories swap links with OTHER stories on the Slashdot frontpage?

What about Firehose?

Re:just because (1)

binarylarry (1338699) | about 7 months ago | (#47102797)

libsnowden is on hold because the author was detained by the NSA.

Re:just because (0)

Anonymous Coward | about 7 months ago | (#47102865)

libsnowden is on hold because the author was detained by the NSA.

But /dev/coldfjiord is OK.

Re:just because (1)

Jeremiah Cornelius (137) | about 7 months ago | (#47103187)

libsnowden is on hold because the author was detained by the NSA.

I think they just want to loosen his algorithm and get him to drop his entropy.

Re:just because (0)

Anonymous Coward | about 7 months ago | (#47103203)

BTW: NSA fags reading this, wipe the shit off the end of your dicks, before jabbing at the keyboards, OK?

Re:just because (1)

RightwingNutjob (1302813) | about 7 months ago | (#47102893)

Ah! So Captain Obvious finally has someone to report to! It's all falling into place...but where does Major Pain fit into the picture?

Re:just because (1)

stjobe (78285) | about 7 months ago | (#47102939)

where does Major Pain fit into the picture?

He's in another chain of command, he reports to General Mayhem.

Re:just because (0)

Anonymous Coward | about 7 months ago | (#47103085)

Actually General Disarray, who in turn is the lackey of Professor Chaos....

Re:just because (2)

QRDeNameland (873957) | about 7 months ago | (#47104063)

Don't forget about Colonel Panic!! (to bring this back kinda on-topic)

Re:just because (0)

Anonymous Coward | about 7 months ago | (#47104037)

You're missing a comma, Admiral Shittygrammar.

call for warranty service (2, Funny)

Anonymous Coward | about 7 months ago | (#47102699)

your sense of humor is broken

Re:just because (-1)

Anonymous Coward | about 7 months ago | (#47102765)

WHY DO YOU REPUBLICANS KEEP TELLING THAT LIE? I also heard Ballmer tell that lie in person about five years ago. Why does your kind claim that the GPL is a virus? We all know that is a lie. Just because you make a change doesn't mean you have to redistribute it no matter how many times you Republicans claim otherwise. You only have to redistribute the source to your changes if you redistribute the change outside of your organization. You are a liar.

Re:just because (0)

Anonymous Coward | about 7 months ago | (#47103447)

Idiot troll post. What a moron.

Re:just because (0)

Anonymous Coward | about 7 months ago | (#47103989)

Dickbreath???

Re:just because (1)

drolli (522659) | about 7 months ago | (#47102817)

And you actually have to share it only with the person you are giving the binary to.

Re:just because (-1)

Anonymous Coward | about 7 months ago | (#47103049)

Wrong.

GPLv2:

b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or,

Re:just because (0)

Anonymous Coward | about 7 months ago | (#47103231)

That language only applies if you don't give the source with the binary. If you give the source to the person to whom you gave the binary, your obligation is done.

Now, if that person gives out the binary, they THEY have to pass along the source or accompany it with a written offer...

Re:just because (-1)

Anonymous Coward | about 7 months ago | (#47103367)

You're simply wrong.

What does “written offer valid for any third party” mean in GPLv2? Does that mean everyone in the world can get the source to any GPL'ed program no matter what? (#WhatDoesWrittenOfferValid)
If you choose to provide source through a written offer, then anybody who requests the source from you is entitled to receive it.

If you commercially distribute binaries not accompanied with source code, the GPL says you must provide a written offer to distribute the source code later. When users non-commercially redistribute the binaries they received from you, they must pass along a copy of this written offer. This means that people who did not get the binaries directly from you can still receive copies of the source code, along with the written offer.

The reason we require the offer to be valid for any third party is so that people who receive the binaries indirectly in that way can order the source code from you.

http://www.gnu.org/licenses/gp... [gnu.org]

Read the italicized portion until it sinks in.

Re:just because (1)

devman (1163205) | about 7 months ago | (#47103523)

If you commercially distribute binaries not accompanied with source code, the GPL says you must provide a written offer to distribute the source code later. When users non-commercially redistribute the binaries they received from you, they must pass along a copy of this written offer. This means that people who did not get the binaries directly from you can still receive copies of the source code, along with the written offer.

You sort of proved the GP's point. Only if you choose to use written offer must the offer be good for any third party. If, instead, one distributes binaries with source, there is no obligation to third parties as they should have received source from their distributor.

Re:just because (2)

spitzak (4019) | about 7 months ago | (#47103553)

You seem to have not italicized the important portion, even though you cut and pasted it into your post! Here I will do it for you:

If you commercially distribute binaries not accompanied with source code, the GPL says you must provide a written offer to distribute the source code later. When users non-commercially redistribute the binaries they received from you, they must pass along a copy of this written offer. This means that people who did not get the binaries directly from you can still receive copies of the source code, along with the written offer.

I think it is your turn to read the italicized portion until it sinks in.

Re:just because (0)

Anonymous Coward | about 7 months ago | (#47103391)

To quote the FSF:

If you choose to provide source through a written offer, then anybody who requests the source from you is entitled to receive it.

The reason we require the offer to be valid for any third party is so that people who receive the binaries indirectly in that way can order the source code from you.

http://www.gnu.org/licenses/gp... [gnu.org]

Re:just because (-1)

Anonymous Coward | about 7 months ago | (#47102837)

The autism is strong in this one.

Re:just because (2)

craigminah (1885846) | about 7 months ago | (#47104135)

Funny they say programming something to the Linux kernel is "hacking" rather than "programming.' Most likely to draw in the l33t kids who want to haxor something.

Re:Oh no! (1)

TechyImmigrant (175943) | about 7 months ago | (#47102615)

Take it a badge of honour.

Re:Oh no! (2)

wjcofkc (964165) | about 7 months ago | (#47102671)

I've been following the Enlightenment project since its inception. A few months ago I had to hop on their mailing list and ask a question for the first time. Rasterman replied, which itself seemed an honor. Doubly so that he kinda called me an idiot in that round about way he does. It was a good day.

Re:Oh no! (1)

Anonymous Coward | about 7 months ago | (#47102829)

Hopefully your question was "why is enlightenment so horrible after all these years?"

Re:Oh no! (2)

wjcofkc (964165) | about 7 months ago | (#47103027)

It's not horrible, it's............ misunderstood. : P

Seriously though, I could write several thousand words on why I love Enlightenment. I've been using Linux since 1996 and right now I am running the Bodhi beta and it's the best experience I've ever had with a DE. Keep in mind, development stalled for over a decade and only recently restarted. Enlightenment is now being developed at breakneck pace and they have some really cool stuff going on. Although I personally can't imagine going with anything less, at the end of the day I advise most people to use something else like.... elementary OS. The fact is Enlightenment is simply to complicated for most people to bother with the learning curve.

Very true... (4, Interesting)

MindPrison (864299) | about 7 months ago | (#47102547)

...I remember my first meeting with Slackware, it was a Linux distro that provoked any user to learn stuff from scratch, and you HAD to use the command line (bash/shell) to install it if you wanted to use it. This forced me to learn Linux. (At least some of the basics)

It also came with a Kernel compilation system + all the needed libraries and packages, so compiling to your own computer was a few commands and worked right out of the box. And then my curiosity got piqued and this drove me to go into the configuration and find out how I could optimize my kernel to fit my needs. In the beginning it was a lot of trial and error, and it looked real daunting, but after a few tries - it wasn't nearly as scary. Before you knew it, I was coding my first stuff in C++. A lot of fun, actually.

So yeah, by all means - if you guys have the time, the curiosity, do go ahead and code something, but do yourself a favor - start off easy.

Re:Very true... (1)

by (1706743) (1706744) | about 7 months ago | (#47102911)

Soft spot in my heart for Slack, too -- although I started off with an old RedHat CD I picked up at a garage sale (2.0 kernel series, I think), Slack was the first distro I really started using. Great stuff, though I've now moved on to Debian.

As to the "start off easy" sentiment, I would highly recommend Project Euler: http://projecteuler.net/proble... [projecteuler.net] fun little programming exercises, and can really be done in any language.

Been there done that... (0)

Anonymous Coward | about 7 months ago | (#47102565)

... ten years ago, interesting exercise for the teachers, not sure the students liked it though except for the 1% ubergeeks.

Re:Been there done that... (2)

Verdatum (1257828) | about 7 months ago | (#47102579)

I feel like any OS course worth a damn should force you to write at least one OS kernel module; and unless your course uses one of the academic OSes, it might as well be on Linux.

Re:Been there done that... (1)

Anonymous Coward | about 7 months ago | (#47102631)

That was my point when I designed this course, I'm not sure I would have the energy and the will to do that again, and I'm only 35.
Give me another ten years and I'll teach VB...

Re:Been there done that... (1)

Mordok-DestroyerOfWo (1000167) | about 7 months ago | (#47102979)

Give me another ten years and I'll teach VB...

And in 20 more you'll be teaching macros in Filemaker. But I've heard that the lobotomy required for either skillset is rather painless.

Re:Been there done that... (1)

stjobe (78285) | about 7 months ago | (#47102981)

Are you me from 2004?

Ten years ago I taught my sysadmin students how to write kernel modules in Linux (on their LFS systems I had them build); these days I make a living coding in C#...

Also, in 2004 I was 35...

Umm (4, Insightful)

ADRA (37398) | about 7 months ago | (#47102567)

Well yes, any C developer (already a minority in the umbrella of 'programmers' these days) can write code for the kernel, but just because one can write software for the kernel doesn't mean they can write anything meaningful to be done in kernel space vs. anywhere else. If you're expecting a slew of new driver hackers reverse engineering chipsets, and implementing better drivers, testing all corner cases (because dev's LOVE testing) I think you're barking up a very small tree, but all the luck to you, becase what's good for Linux is good for me, you, us all.

Re:Umm (2)

TechyImmigrant (175943) | about 7 months ago | (#47102607)

Some of us build hardware and need to write device drivers.
Cutting through the cruft is exceedingly useful to those of us that want to get the device to work.

Re:Umm (0)

Anonymous Coward | about 7 months ago | (#47102625)

Good kernel developers have a number of options. For example, dealing with Android devices. If one can get the specs for SoC functions, there might be functionality on a tablet or smartphone just waiting to be taken advantage of by someone who can read the details and write a kernel module to take advantage of this.

Disclaimer: More kernel developers and fewer people making yet another fleshlight app on iOS or Android is a good thing. It takes real talent to program on this level due to resource limitations and the fact that one goof, and the bug is a show-stopper.

Re:Umm (1)

koreanbabykilla (305807) | about 7 months ago | (#47102869)

"Fleshlight app" lolol. Did you mean to do that?

Re:Umm (1)

Mordok-DestroyerOfWo (1000167) | about 7 months ago | (#47102987)

"Fleshlight app" lolol. Did you mean to do that?

Now that's an Android app that would give iOS a run for its money.

Re:Umm (1)

fizzer06 (1500649) | about 7 months ago | (#47103181)

What happened to all of the really good fart apps?

Re:Umm (2)

PhrostyMcByte (589271) | about 7 months ago | (#47102655)

I've developed Windows drivers before and can say that while yes, it is just plain C or a subset of C++, the APIs are entirely new and come with various curveballs user-mode devs will not have ever dealt with like keeping track of what IRQ level you're at.

A simple driver is... well, fairly simple. Once you try to do anything interesting though, there's a lot to learn before you can be useful. I'm curious if Linux is any different.

Re:Umm (2)

Richy_T (111409) | about 7 months ago | (#47102807)

The nice thing is, unless you are doing something totally off the wall, there is a good chance that there is already a module out there that does something similar to what you want to do and the open source love is revealed to you. This applies to many other open-source OSs as well, of course.

Try this with windows and there's a good chance you'll find some incomplete example code from three API revisions ago that won't even compile with the latest libraries (BTDT)

Re:Umm (2)

WalrusSlayer (883300) | about 7 months ago | (#47103227)

Try this with windows and there's a good chance you'll find some incomplete example code from three API revisions ago that won't even compile with the latest libraries (BTDT)

Uhhhhh.... for the most part, the kernel API in Windows has been remarkably stable. I have an *extremely* non-trivial Windows driver that works from NT all the way through Win 8. The only major disruption in the 10+ years between NT4 and Vista was the TDI client debacle where they deprecated TDI and there were some workarounds that needed to be implemented to run on the new kernel.

That. Was. All.

I'm not a Linux kernel dev (though have lots of user-mode Linux/Unix experience), but my understanding of that world is "we'll change anything and everything if and whenever we feel like it, and it's up to the rest of the world to keep up with those changes". So your example, ironically, would apply much more to a Linux driver sample than it would a Windows driver sample.

Re:Umm (2)

hobarrera (2008506) | about 7 months ago | (#47103283)

I'm not a Linux kernel dev (though have lots of user-mode Linux/Unix experience), but my understanding of that world is "we'll change anything and everything if and whenever we feel like it, and it's up to the rest of the world to keep up with those changes". So your example, ironically, would apply much more to a Linux driver sample than it would a Windows driver sample.

Linux will change their ABI if necesary, or interal APIs, but not external APIs.

Re:Umm (0)

Anonymous Coward | about 7 months ago | (#47103485)

Linux changes kernel APIs all over the place. It doesn't change user space things and system calls and similar.

I worked for a shop which had a set of high-performance PCIe drivers. Windows WDM-model drivers worked everywhere from NT to Windows 8 without any changes.

The Linux drivers on the other hand, had hundreds of #ifdef's against kernel versions everywhere. It seems that every 3 or 4 versions there was some symbol name change and/or semantic difference related to something critically important like interrupts.

When Linux people say they don't break the interface - they are only referring to user-space stuff. The kernel is all about optimization and forward-thinking. They do not care about backwards compatibility.

Re:Umm (-1)

Anonymous Coward | about 7 months ago | (#47104173)

Someone downmod this Windows $shill frog-hatted faggot; I DISAGREEEEEEEEEEE!!!!!!!!!!!!!!!!!!!!

Re:Umm (0)

Anonymous Coward | about 7 months ago | (#47102951)

I've developed Windows drivers before and can say that while yes, it is just plain C or a subset of C++, the APIs are entirely new and come with various curveballs user-mode devs will not have ever dealt with like keeping track of what IRQ level you're at.

A simple driver is... well, fairly simple. Once you try to do anything interesting though, there's a lot to learn before you can be useful. I'm curious if Linux is any different.

An old rule of thumb: compilers are 10x tricker to write than applications, OS-level code is 10x trickier than that.

I don't know about the compiler factor anymore, since modern GUI programs tend to be pretty complex themselves, but the problem with OS-level code is that concurrency is rampant plus you have to be aware of hardware state.

Re:Umm (1)

P Bacon (3557945) | about 7 months ago | (#47102669)

Well, the embedded systems market is due to flourish any minute now. There will likely be many small trees worth barking up. Arch on Rockchip!

Re:Umm (2)

swillden (191260) | about 7 months ago | (#47102953)

If you're expecting a slew of new driver hackers reverse engineering chipsets, and implementing better drivers, testing all corner cases (because dev's LOVE testing) I think you're barking up a very small tree

Don't be silly.

The goal is to get a nifty new string manipulation API implemented in kernel space. Imagine how slick it would be to use (error checking omitted for brevity):

char* buf; FILE* fp = fopen("/dev/strcat", "r+");
fwrite(string1, string1_length, 1, fp);
fwrite(string2, string2_length, 1, fp);
fflush(fp);
buf = malloc(string1_length + string2_length);
fread(buf, string1_length + string2_length, fp);
fclose(fp);

I mean, how awesome is that? Only 8 lines of code to concatenate two strings! It would even be trivial to include multiple copies of string1 and/or string2 if you wanted!

And since it's implemented in the kernel you know it'll be uber fast and secure.

Yay (0)

Anonymous Coward | about 7 months ago | (#47102573)

You too can modify the kernel with a module that has only been tried on one computer with only the bit of functionality you want.

just remember ... (5, Funny)

nblender (741424) | about 7 months ago | (#47102575)

In kernel space, no one can hear you scream.

Re:just remember ... (1)

Anonymous Coward | about 7 months ago | (#47102687)

In kernel space, no one can hear you scream.

Not until you write your own scream driver.

Re:just remember ... (2)

iggymanz (596061) | about 7 months ago | (#47102757)

"I have no dev device, and I must scream"

Re:just remember ... (0)

Anonymous Coward | about 7 months ago | (#47102695)

***********
KHAN !
***********

Re:just remember ... (3, Insightful)

mlts (1038732) | about 7 months ago | (#47102849)

There is always the ability to panic.

ATTENTION, like EVERYBODY (0)

Anonymous Coward | about 7 months ago | (#47102591)

If you are going to do this, seriously have your code reviewed by all the available static analyzers you can get your hands on. Double check all your buffer lengths with a calculator. Read a book on C security vulnerabilities.

In a kernel module, you know small C coding defects can catapult into serious problems because the kernel will execute your code with superuser authority.

Re:ATTENTION, like EVERYBODY (0)

Anonymous Coward | about 7 months ago | (#47102613)

Or don't run your alpha quality modded kernel on a public box?

Hello World! (0)

Anonymous Coward | about 7 months ago | (#47102645)

Searched TFA for "Hello World" ... left satisfied.

1982 all over again and boy am I excited (0)

Anonymous Coward | about 7 months ago | (#47102737)

Not really.

Maybe this is one way to indoctrinate next decade's "We work for free and like it dammit!" generation?

Re:1982 all over again and boy am I excited (0)

Anonymous Coward | about 7 months ago | (#47103111)

Really? I wish to subscribe to your newsletter containing listings of free jobs I can work for free.

Well (1)

Richy_T (111409) | about 7 months ago | (#47102777)

I sent it Pi and the universe imploded. Fortunately we had recent back-ups. Though the ones from after Jeb Bush* became president went *ahem* mysteriously missing.

*Replace with Hillary Clinton if you're on the other team.

Stop telling me what to do! I don't want to! (2)

wonkey_monkey (2592601) | about 7 months ago | (#47102975)

Become a Linux Kernel Hacker and Write Your Own Module

I don't want to. You do it. sudo you do it.

Re:Stop telling me what to do! I don't want to! (1)

Anonymous Coward | about 7 months ago | (#47103451)

You only sudo do that if you have only dollar no hash. You need to exchange the dollar for #. Then smoke the prompt while entering you command into the wonder buffer. Then see how magically the penguin box obeys even your dd-if=/dev/zero-of=/dev/sda-iest wishes.

sudo should be called SINU - sudo is not unix. Not evrey distro dealer can offer you rial pure sudo. Only use pure sudo! {Hard,Soft}links can harm you health!

To get really high even hash (#) is not enough, your'e really need to enter your kernel space quarters for ring 0 to rulez them all.

hacking (0)

Anonymous Coward | about 7 months ago | (#47102991)

I've heard of people hacking into a computer system or a network, but not the kernel. I guess malware can hack into the kernel in order to take over the computer system. I learned something new.

Good! Now I have questions: (0)

Anonymous Coward | about 7 months ago | (#47103031)

I want to drive mouse and keyboard directly from the user space (with admin privileges if needed). I.e. bypassing qt, x windows, so that from the other application it will be indistinguishable from user's input (almost). The question is where do I begin?

PS: Nothing criminal, just software automation.

Re:Good! Now I have questions: (2)

Tough Love (215404) | about 7 months ago | (#47103207)

/dev/input [linuxjournal.com]

Also uinput, /dev/input/mice, use the damn API (2)

raymorris (2726007) | about 7 months ago | (#47104615)

> PS: Nothing criminal, just software automation.

The GUI probably calls function in a library, if not command line binaries. You are normallybetter off using the library directly rather than emulating GUI input from the user.

To fake user input, see uinput. Also, you can capture the events using od tool from the /dev/input/mice and then replay them once you have decoded the sequence.

# cat /dev/input/mice | od -t x1 -w3
0000000 08 02 00
0000003 08 08 00
0000006 08 09 00
0000011 08 07 00
0000014 08 04 00
0000017 08 01 01
0000022 08 00 02
0000025 08 02 02

Uberdupe (0)

Anonymous Coward | about 7 months ago | (#47103043)

Why has this article from 15 years ago just appeared? Is it another beta bug?

GOTOs (0)

Anonymous Coward | about 7 months ago | (#47103055)

When I looked through the code, I initially cringed at the use of GOTOs, not knowing that the Linux kernel uses them extensively.

Here's a mailing list thread that explains the rationale:
http://koblents.com/Ches/Links/Month-Mar-2013/20-Using-Goto-in-Linux-Kernel-Code/

Re:GOTOs (1)

tomhath (637240) | about 7 months ago | (#47103461)

Interesting thread. But it would be nice if Linus would tell us what he really thinks...

My advice (1)

jgotts (2785) | about 7 months ago | (#47103075)

This item hit reddit earlier, so I'll repeat my advice.

It might be easier to begin with a kernel module that is useful to you, and either work with the developer or find a module where the developer has lost interest instead.

I worked on two kernel modules, one called oplbeep and the other was totem. Both very simple things that required porting when interfaces changed. Today I work on the vmware modules which are always requiring tweaking (at least unless you upgrade to the latest version of VMWare more regularly) but I do that just to get my work done.

Re:My advice (1)

Tough Love (215404) | about 7 months ago | (#47103223)

Starting with a dead simple module like this is correct.

First Tutorial I've seen with Goto... (1)

Prien715 (251944) | about 7 months ago | (#47103309)

From the tutorial:
----
static struct buffer *buffer_alloc(unsigned long size)
{
        struct buffer *buf;
        buf = kzalloc(sizeof(*buf), GFP_KERNEL);
        if (unlikely(!buf))
                goto out;
----
I haven't seen a tutorial written where "goto" was recommended...ever. When I was taught C back in the late 20th century, we weren't taught how to use goto or when. Now that I'm older and wiser, the instruction has its place -- especially in code where performance is paramount -- but it can also be less confusing than other control structures in the right situation. (I'm still uncomfortable using it myself.)

Any wise old vets want to chime in about when to use "goto" and when not to?

Re:First Tutorial I've seen with Goto... (1)

Anonymous Coward | about 7 months ago | (#47103425)

The goto statement comes in handy when a function exits from multiple
locations and some common work such as cleanup has to be done. If there is no
cleanup needed then just return directly.

The rationale is:

- unconditional statements are easier to understand and follow
- nesting is reduced
- errors by not updating individual exit points when making
        modifications are prevented
- saves the compiler work to optimize redundant code away ;)

https://www.kernel.org/doc/Documentation/CodingStyle

Re:First Tutorial I've seen with Goto... (0)

Anonymous Coward | about 7 months ago | (#47103431)

I'm not old or wise but I do use goto's it's fairly common in kernel code. I've written several drivers in Windows and Linux, and use goto's in plenty of spots, but for a very specific reason, and under a few constraints. The reasons being that there is some staging of setup that is going where you have to do something like:
{ ...
        if(setup1() != SUCCESS)
        {
                goto undo_setup1;
        } // Depends on setup1 success
        if(setup2() != SUCCESS)
        {
                goto undo_setup2:
        }

          return SUCCESS;

undo_setup2:
        cleanup2();
undo_setup1:
        cleanup1();
        return FAILED;
}

This allows you to free and unregister things in the correct order without making failure steps with a lot duplicated calls that amounts to the same thing anyway. So it is very useful for handling error conditions where you need to free allocations that have happened up to a point, unmap some IO, etc.

Re:First Tutorial I've seen with Goto... (3, Informative)

frank_adrian314159 (469671) | about 7 months ago | (#47103651)

Just like any other construct - when it makes the code more clean, clear, correct, and/or optimized. These are tradeoffs.

For instance, let's say you have a function having a deeply nested conditional:

if (!a) {
    if (!b) {
        if (!c) {
...

} } }

This code might be more simply understood as:

if (a) goto done;
if (b) goto done;
if (c) goto done;
...
done:
...

Re:First Tutorial I've seen with Goto... (2)

shoor (33382) | about 7 months ago | (#47103871)

I got my intro to programming in the mid 1960s with 'the college computer' a PDP-8 that we programmed in Fortran using punched cards. In those days, just getting access to a computer was a pretty big deal, but things were changing, so 'programming paradigms' started appearing, and the first one that I remember was 'structured programming'. This is where I first heard the mantra of 'goto-less' programming. (Before that, the mantra was not to write self-modifying code, which was something you almost had to be writing assembly language code to be able to do, though COBOL had an 'alters' statement as I recall.)

I remember being somewhat startled by the idea of excluding gotos. How could you write non trivial code without any goto statements? I actually thought of it almost as a challenge to figure out how to do so. The opposite of structured code was 'spaghetti code'. Anyway, it's become a conventional bit of wisdom that I suppose is just automatically passed down to each generation of students without anyone ever seriously questioning it, except those who find they really need it sometimes. At some point I started defiantly putting an occasional goto in my code again, but not often.

Re:First Tutorial I've seen with Goto... (1)

c (8461) | about 7 months ago | (#47104193)

Any wise old vets want to chime in about when to use "goto" and when not to?

Not claiming to be a wise old vet, but as a general rule of thumb, the ideal way to use it is to emulate control structures from even higher level languages. For example, if in C++ you'd write an error handling behavior in a function as an exception, in C you might goto a common area to cleanup transient memory and gracefully return an error code. Which is exactly what that snippet you've shown is doing.

Re:First Tutorial I've seen with Goto... (0)

Anonymous Coward | about 7 months ago | (#47104581)

What PP said: if that illustration of the use of "goto" isn't exception handling in a sheep's clothing, I don't know what is.

Writing modules near impossible (3, Interesting)

Eravnrekaree (467752) | about 7 months ago | (#47103379)

While the article shows a cute little example on how to write a useless module, it does not show anyone how to actually write a serious kernel module. The Linux kernel has never been known for documenting kernel internals, such documentation is scant at best and simply not sufficient to write a module. It is safe to say tha due to the poor practices of Kernel developers who constitently ignore good practice by not Documenting Their Crap, the kernel is an elite club of developers with knowledge that is secret. The practices of the Linux kernel development is just sheer sloppiness, horribly bad practice. They could have easily set up a Wiki and documented the interfaces and their architecture. What we see with the kernel developers is that they do not care about anyone else, not users, and not even outside techies, so why would they care about whether or not an outsider can understand the kernel, just as why would they care if a user can upgrade kernel versions without having all of their device drivers blow up. As anyone well versed in computer science knows, computer code is rarely self documenting, especially the kernel, and trying to reverse document a large software project is an outrageous waste of time and can be enough of a problem that it keeps even seasoned programmers away from the project. A huge piece of undocumented code is just not worth the effort to learn.

Hello World ... (1)

JackCorbae (693005) | about 7 months ago | (#47104249)

To be honest - I expected the example to be the programmer's equivalent of a guitarist playing Stairway to Heaven. I'm actually disappointed the example wasn't a kernel module to print "Hello World"! :)

Re:Writing modules near impossible (0)

Anonymous Coward | about 7 months ago | (#47104269)

Sounds like there needs to be an apprenticeship system. Get eager, smart younguns to work side-by-side (virtually) with experienced devs and generate documentation. The kernel gets that code documented and the younguns get deep experience with the internals, plus really juicy resume' bait. That's pretty standard practice in other professions, or used to anyway.

Re:Writing modules near impossible (1)

Darinbob (1142669) | about 7 months ago | (#47104403)

And none of the article is new. There have been examples of simple modules for linux kernels ever since modules were implemented, in print and on the web. What's new about this, except that Slashdot is now an advertiser for blog articles?

Re:Writing modules near impossible (0)

Anonymous Coward | about 7 months ago | (#47104409)

Most of the kernel interfaces that are used by driver writers are very well documented. There are large comments above important structures and functions, as well as a Documentation folder that describes large subsystem. Furthermore, there are numerous books that describe the kernel architecture from different perspectives - just pick the author you think best writes to your learning style.

The poorly-documented parts tend not to be in the core architecture, but in the fringes. The driver for that network card from 1990? The one that the kernel dev had to write by reverse engineering the hardware because the device wasn't documented? Those instances have poorer documentation (if any), and yet the source code reveals more information than if we had nothing. But if you have a new device for which you wish to write a Linux driver, the interfaces you need are all well-documented, and easier to use than the interfaces in other kernels such as Windows or FreeBSD (I know because I have programmed in all of them).

In my day... (1)

dfn5 (524972) | about 7 months ago | (#47103495)

... any competent Unix admin could write a device driver. Now a days they barely know the grep.

VERY OLD ARTICLE (0)

Anonymous Coward | about 7 months ago | (#47103653)

This is a repeat article that was on slashwhore about 6-8 years ago. Nothing new with this crap.

Thanks for the bug. (0)

Anonymous Coward | about 7 months ago | (#47103717)

You're fired!

"Daunting" (2)

Tyler Durden (136036) | about 7 months ago | (#47103761)

You know, I'd like to think there was a time when the majority of Slashdot users could cheerfully take on writing modules for the Linux kernel or *gasp* just code in C.

Re:"Daunting" (0)

Anonymous Coward | about 7 months ago | (#47104081)

Learn Rust or Visual Basic.net for thee kernel safety...

Re:"Daunting" (1)

jythie (914043) | about 7 months ago | (#47104273)

There was never such a time. Slashdot has always had a pretty good variety of tech people, so for some it would be daunting, some it would not, depending on how close it was to their own domain knowledge.

Re:"Daunting" (1)

Tyler Durden (136036) | about 7 months ago | (#47104339)

Well then I guess I'll just have to take your word for it Mister 914043. ;)

Re:"Daunting" (1)

Ummon (15714) | about 7 months ago | (#47104419)

Take mine too.

Re:"Daunting" (0)

hax4bux (209237) | about 7 months ago | (#47104527)

And mine. There never was a world of "with many eyeballs, all bugs are shallow"

I got to the part about udev and closed the window (0)

Gothmolly (148874) | about 7 months ago | (#47104179)

When will people give up this love affair with the systemd/NetworkManager/udev/automagic crap? What makes Linux kickass is how NOT Windows-like it is.

screw it i'm gonna put my hosts file in the kernel (4, Insightful)

Anonymous Coward | about 7 months ago | (#47104289)

Take that APK! I'm going to put my motherfucking hosts file in the kernel..that's right.. I'm going to have a /dev/hosts

You heard me right! /dev/motherfucking/hosts

True ring 0 baby!

Load More Comments
Slashdot Login

Need an Account?

Forgot your password?