Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where can I see the source code of system API like write/read of linux?

I have downloaded the sytem source code from here, but i can't find source code of read/write functions from the package. Could anybody tell me where I can get the code for these socket operation functions?

[why I want to check the source code]
I am developing a multithreaded linux application and need to know, if calling socket operation functions like write/read/sendmsg to access a same TCP socket from different threads concurrently is safe or not.

like image 549
Wallace Avatar asked Dec 26 '22 03:12

Wallace


2 Answers

you can search a function and other things in the kernel source code on LXR.

But before you search, you should know that write/read/sendmsg are system call,and their definitions aren't like usually functions. When you use read(), sys_read() works in fact,and itselves defintion is also confused:here

here is write() and sendmsg() .

If you want to know more about system call such as their definiton, you can read chapter.5 of Linux Kernel Development.

like image 119
vvy Avatar answered Dec 28 '22 16:12

vvy


1) Find out what library provides function you want to see source of

e.g. "man read"

NAME
   read - read from a file descriptor

SYNOPSIS
   #include <unistd.h>

   ssize_t read(int fd, void *buf, size_t count);

2) locate header file, e.g. "/usr/include/unistd.h"

3) find out what package provides this file, with Debian/Ubuntu e.g.

pwadas@kehillah:~$ dpkg -S /usr/include/unistd.h 
libc6-dev: /usr/include/unistd.h

4) Download source package and browse code

there's probably many "read" functions available from various libraries. You might want to try

man 7 socket
man 7 tcp 

or other relevant sources.

like image 24
Piotr Wadas Avatar answered Dec 28 '22 17:12

Piotr Wadas