Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Linux file I _ par 0

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

After years of work, some of the frequently used Linu O (very important, because everything is a file) function can be checked later. I'm not very busy at this time, just to take notes and sort them out.

There are some differences between the file Ipicuro and the standard iAccord O library: 1) the file Ipicuro is a system call, and the standard iUnip O library is a function library that encapsulates system calls; 2) the file Ipicuro operates on the file descriptor (kernel returns, which is a non-negative integer, where 0mem1ma2 represents standard input, standard output, and standard error), and the standard Icando operates on streams, that is, FILE objects. Therefore, it can be seen that the transplant line of standard Ibig O is better. Speaking of system calls, again, a system call is an interrupt that is implemented by the 0x80 kernel. The main purpose of this article is to record some system calls to the corresponding file Igamo.

1. Open the file

Int open (const char * pathname, int flag,/*mode_t mode*/) / * successfully returned file descriptor * /

Pathname: to open or create the name of a file, the system specifies a maximum length

Flag:O_RDONLY is read-only open. O_WRONLY only writes to open. O_RDWR read and write open.

O_APPEND is appended to the end of the file every time it is written.

If O_CREAT does not have a file, create it.

O_EXEL if O_CREAT is also specified and the file already exists, an error will occur.

If the O_TRUNC file exists and is opened as read-only or write-only, the length is truncated to 0.

If the O_NOCTTY is an end device, the device is not assigned as the control terminal for this process.

O_NONBLOCK sets a FIFO, a block special file, or a character file to nonblocking mode.

O_DSYNC, O_RSYNC, and O_SYNC are not very well understood. We will use them later to understand them in depth.

Mode: may or may not, meaning to set user permissions, group permissions, other user permissions, such as: 777: full access to read, write, executable.

2. Creat creates a file

Int creat (const char * pathname, mode_t mode); / * File descriptor is returned successfully * /

Parameter description of the pass open function

This function is equivalent to: open (pathname,O_WRONLY | O_CREAT | obliterated truncation mode)

3. Close closes an open file

Int close (int filedes)

Closing a file also releases the record lock added to the file; when a process terminates, the file is automatically closed, so there is no need to call close to close the file.

4. Set the current file offset lseek function

Off_t lseek (int filedes, off_t offset, int whence); / * New file offset was returned successfully * /

Parameter offset is related to whence, which is explained as follows:

SEEK_SET: sets the offset of the file to offset bytes from the beginning of the file.

SEEK_CUR: sets the offset of the file to the current value plus offset,offset can be positive or negative.

SEEK_END: set the offset of the file to the file length plus offset,offset can be positive or negative.

* you can use lseek (fd,0,SEEK_CUR). If the return value is-1, the offset is not set, otherwise the offset is set.

5. Read the data read function from the file

Ssize_t read (int filedes,void * buf,size_t nbytes); / * the number of bytes read is returned successfully. If 0 has been returned at the end of the file, the error returns-1 bytes /

6. Write data write to the open file

Ssize_t write (int filedes,const void * buf,size_t nbytes); / * the number of bytes written is returned successfully, and-1 bytes / is returned for failure.

If the O_APPEND parameter is specified when open, the file offset is set to the end before each write. After a success, the cheapness increases the number of bytes actually written.

7. Atomic operation function pread and pwrite function

In fact, atomic operations can be achieved with the O_APPEND parameter in the open function, and each time the write is at the end of the file.

Ssize_t pread (int fd,void * buf,size_t count,off_t offset)

Ssize_t pwrite (int fd,const void * buf,size_t count,off_t offset)

Equivalent to calling lseek and read or write sequentially

8. Copy an existing file descriptor DUP and dup2

Int dup (int filedes)

Int dup2 (int filedes,int filedes2)

Dup returns the value of the smallest file descriptor currently available: dup (1) returns 3pm 3 share 1

Dup2 returns filedes2 if the two parameters are equal, otherwise, filedes shares filedes2 and closes filedes2.

Take an example of developing a serial port into the zebra command line some time ago:

Int config_console_para (int iConsoleFd); / * modified by zhaoxiaohu*//*added by zhaoxiaohu for console to vty*/struct vty* vty_console_create (char * dev) {int fd; int iRet; struct vty* vty; fd = open (dev, O_RDWR, 0644); if (fd

< 0 ) { printf( "error: open console %s error.\r\n", dev ); return NULL; } iRet = config_console_para(fd); if(0 != iRet) { printf("console para set error.\r\n"); } vty = vty_get_new(); if( vty == NULL ) return NULL; vty->

Fd = fd; vty- > type = VTY_CONSOLE; strcpy (vty- > address, "Console"); vty- > node = LOGIN_NODE; vty- > fail = 0; vty_clear_buf (vty); vty- > status = VTY_NORMAL; vty_hello (vty); vty_prompt (vty); buffer_flush_all (vty- > obuf, vty- > fd); / * Add read/write thead * / vty_event (VTY_WRITE, vty) Vty_event (VTY_READ, vty); return vty;} / * b-console config added by zhaoxiaohu,2018-12-19. Configure serial port parameters * / int config_console_para (int iConsoleFd) {struct termios tConsolePara; if (iConsoleFd)

< 3 ) { printf("fd is error.\r\n"); return -1; } if(tcgetattr(iConsoleFd,&tConsolePara) != 0) { printf("get console para error.\r\n"); return -1; } tConsolePara.c_lflag &= ~ (ICANON | ECHO | ECHOE | ISIG); tConsolePara.c_cc[VERASE] = 1; if(tcsetattr(iConsoleFd,TCSANOW,&tConsolePara) != 0) { printf("config console para error.\r\n"); return -1; } if( cfsetispeed(&tConsolePara,B115200) != 0 )/*设置为115200Bps*/ { printf("config console para error.\r\n"); return -1; } if( cfsetospeed(&tConsolePara,B115200) != 0 )/*设置为115200Bps*/ { printf("config console para error.\r\n"); return -1; } return 0;}/*e-console config added by zhaoxiaohu,2018-12-19*//*b-added by zhaoxiaohu for console to connect vty shell*/void console_connect_vty(void ){ struct vty *vty; int iConsoleFd, iRet; vty = vty_console_create("/dev/console"); g_pVty = vty; g_iConsoleFd = vty->

Fd; iConsoleFd = vty- > fd; dup2 (iConsoleFd,0); / * close 0meme 1je 2, shared to serial port * / dup2 (iConsoleFd,1); / * * / dup2 (iConsoleFd,2); / * * / setvbuf (stdout,NULL,_IONBF,0); / * set stdout no buffer,printf to console .recording by zhaoxiaohu-2019.4.10*/ struct timeval time_now; struct timeval * time_wait While (1) {# if 1 time_now.tv_sec = vty- > vested timeout; time_now.tv_usec = 0; if (time_now.tv_sec > 0) time_wait = & time_now; else time_wait = NULL IRet = select (iConsoleFd + 1, & vty- > read_set, & vty- > write_set, NULL, time_wait); if (iRet type = = VTY_CONSOLE) {vty_timeout (vty); continue;} else {vty_timeout (vty); break } # endif if (FD_ISSET (iConsoleFd, & vty- > read_set)) vty_read (vty); if (FD_ISSET (iConsoleFd, & vty- > write_set)) vty_flush (vty); / * console can't close * / if (vty- > type = = VTY_CONSOLE) continue; if (vty- > status = = VTY_CLOSE) break } return;} / * e-added by zhaoxiaohu for console to connect vty shell*/

9. Change the property function fcntl of the open file

Int fcntl (int filedes,int cmd,/*int arg*/,/*flock record Lock * /)

The function has five functions by setting cmd:

1) copy an existing descriptor (F_DUPFD)

Dup (filed); / * equivalent to * / fcntl (filed,F_DUPFD,0); dup2 (filed,filed2); / * equivalent to * / close (filed2); fcntl (filed,F_DUPFD,filed2); / * the difference between dup2 and close+fcntl is that the former is an atomic operation while the latter is not * /

2) get or set the file descriptor token (F_GETFD or F_SETFD)

3) get or set file descriptor flags (F_GETFL or F_SETFL) that can be changed: O_APPEND, O_NONBLOCK, O_SYNC, O_DSYNC, O_RSYNC, O_FSYNC, O_ASYNC

4) get or set the file async F_GETOWN O ownership (F_GETOWN or F_SETOWN), and get or set the process ID or process group ID that receives SIGIO or SIGURG signals

5) get or set the record lock (F_GETLK, F_SETLK or F_SETLKW) through the structure flock.

10. The grocery store ioctl function operated by I _ par _ 0

Files that cannot be operated with the above function can be operated with ioctl

Int ioctl (int filedes,int request, / * void * arg*/)

There are many functions, which are roughly divided into: 1) socket 2) file operation 3) interface operation 4) arp cache operation 5) routing table operation 6) streaming system.

Take an example of developing static routes and querying arp cache tables at work some time ago:

/ * b-added by zhaoxiaohu to serch ip at arp cache*//* definition structure * / typedef struct tArpTable {struct tArpTable * pNext; unsigned char data [0];} * ptVlanIpDev;ptVlanIpDev g_ptVlanIpDevTable = NULL;/* shows the virtual interface added by the switch chip, vlan ip*/void vlanIpDevTableDisplayAll () {ptVlanIpDev pTemp = glossptVlanIpDevTable; while (pTemp) {printf ("% s\ r\ n", pTemp- > data); pTemp = pTemp- > pNext }} / * clear the virtual interface table * / void arpTableClear () {ptVlanIpDev pTemp = NULL; while (g_ptVlanIpDevTable) {pTemp = groomptVlanIpDevTable; g_ptVlanIpDevTable = pTemp- > pNext; free (pTemp); pTemp = NULL;}} / * get virtual interface * / int get_vlan_ip_dev () {unsigned char ucBuf [256] = {0}; FILE * fp Fp = popen ("find / sys/class/net/-name sw.*", "r"); if (NULL = = fp) {return-1;} arpTableClear (); while (fgets (ucBuf, sizeof (ucBuf), fp)) {ptVlanIpDev pTemp = NULL If (NULL = (pTemp = (ptVlanIpDev) calloc (1Magnesizeof (struct tArpTable) + sizeof (ucBuf) continue; memcpy (pTemp- > data,ucBuf,strlen (ucBuf)); pTemp- > pNext = groomptVlanIpDevTable; g_ptVlanIpDevTable = pTemp; memset (ucBuf,0,sizeof (ucBuf));} pclose (fp); return OK } int ipnet_arp_for_cache () {int sfd,ret; unsigned char * ucMac; unsigned char ucIpAddrStr [32] = {0}; struct arpreq arp_req; struct sockaddr_in * sin; get_vlan_ip_dev (); ptVlanIpDev pTemp = groomptVlanIpDevTable; ip2Str (g_ArpFindIpAddr, ucIpAddrStr) / * ip*/ while (pTemp) / * traverse all virtual interfaces * / {sin = (struct sockaddr_in *) & (arp_req.arp_pa); memset (& arp_req, 0, sizeof (arp_req)); sin- > sin_family = AF_INET; inet_pton (AF_INET, ucIpAddrStr, & (sin- > sin_addr)) Strncpy (arp_req.arp_dev, pTemp- > data+15,strlen (pTemp- > data+15)-1); sfd = socket (AF_INET, SOCK_DGRAM, 0); ret = ioctl (sfd, SIOCGARP, & arp_req); if (ret

< 0) { goto nextNode; } if ( arp_req.arp_flags & ATF_COM ) /*找到ip对应的mac地址*/ { ucMac = (unsigned char *)arp_req.arp_ha.sa_data; // printf("MAC: x:x:x:x:x:x\n", // ucMac[0], ucMac[1], ucMac[2], ucMac[3], ucMac[4], ucMac[5]); memcpy( gArpFindMac, ucMac, 6 ); } else { // printf("MAC: Not in the ARP cache.\n"); } nextNode: pTemp = pTemp->

PNext;} return OK;} / * e-added by zhaoxiaohu to serch ip at arp cache*/

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report