4
votes

In Linux, to create a socket we include the sys/socket.h header file and use the socket() function. The header file is located at /usr/include/sys/socket.h.

extern int socket (int __domain, int __type, int __protocol) __THROW;

Can anyone please tell the location where the socket() function is actually implemented.

Thanks.

3

3 Answers

4
votes

Acutally, int socket (int __domain, int __type, int __protocol) __THROW

implemented in glibc,

and the glibc calls the kernel function sys_socket implemented in kernel file net/socket.c.

asmlinkage long sys_socket(int family, int type, int protocol);
1
votes

socket(2) is a ssytem call. The socket function inside Glibc is just a tiny wrapper to make the real system call.

From an application's point of view, system calls are atomic; in other words, the virtual machine on which your Linux application program is running is the x86 machine (the non-priviledged instruction set) augmented with the more than 300 system calls provided by the kernel. See also Assembly Howto which explains how a system call can be coded. Read more about the linux kernel and the syscalls(2) and intro(2) man page.

The real work about sockets is done inside the kernel, it is the networking subsystem.

0
votes

Here it is => socket.c.

Usually most of the socket functions, including this one, are just wrappers around system calls (direct calls to the kernel), hence it is all handled by the almighty kernel itself.

Here is the Kernel's implementation: SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol){...}