Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I found some bizarre code in the linux kernel, could some one explain it to me?

Tags:

c

linux-kernel

I found this in initramfs.c, I haven't seen this syntax before, could some one explain what it is doing?

static __initdata int (*actions[])(void) = {   [Start]   = do_start,   [Collect] = do_collect,   [GotHeader] = do_header,   [SkipIt]  = do_skip,   [GotName] = do_name,   [CopyFile]  = do_copy,   [GotSymlink]  = do_symlink,   [Reset]   = do_reset, }; 

Source Code (line 366): initramfs.c

like image 765
Seamus Connor Avatar asked Feb 11 '10 00:02

Seamus Connor


People also ask

What is Linux kernel code?

The Linux® kernel is the main component of a Linux operating system (OS) and is the core interface between a computer's hardware and its processes. It communicates between the 2, managing resources as efficiently as possible.

What is the starting point of Linux kernel?

The start_kernel is the entry of the generic and architecture independent kernel code, although we will return to the arch/ folder many times. If you look inside of the start_kernel function, you will see that this function is very big. For this moment it contains about 86 calls of functions.

Why io_ uring?

With io_uring system calls can be completed asynchronously. This means an application thread does not have to block while waiting for the kernel to complete the system call. It can simply submit a request for a system call and retrieve the results later; no time is wasted by blocking.

What is Linux eBPF?

eBPF is a kernel technology (fully available since Linux 4.4). It lets programs run without needing to add additional modules or modify the kernel source code. You can conceive of it as a lightweight, sandboxed virtual machine (VM) within the Linux kernel.


2 Answers

This is an out-of-sequence array initialization by index. It's like writing

actions[Start] = do_start; actions[Collect] = do_collect; 

except that you can do it as a static initializer.

like image 89
Variable Length Coder Avatar answered Sep 20 '22 18:09

Variable Length Coder


This is a feature from ISO C99 known as designated initializers. It creates an array and initializes specific elements of that array, not necessarily the first N in order. It's equivalent to the following snippet:

static __initdata int (*actions[SOME_SIZE])(void); actions[Start]   = do_start; actions[Collect] = do_collect; actions[GotHeader] = do_header; actions[SkipIt]  = do_skip; actions[GotName] = do_name; actions[CopyFile]  = do_copy; actions[GotSymlink]  = do_symlink; actions[Reset]   = do_reset; 

Except that the array will only be as large as it needs to (equal in size to one more than the largest index), and it can be initialized statically at global scope -- you can't run the above code at global scope.

This is not a feature of ANSI C89, but GCC provides this feature as an extension even when compiling code as C89.

like image 31
Adam Rosenfield Avatar answered Sep 21 '22 18:09

Adam Rosenfield