Linux102系列会详细讲解Linux0.11版本中的102个文件,本文讲解linux0.11的第25个文件
include/errno.h的文件源码。
1.include/errno.h的主要作用
errno.h文件定义一系列标准错误码常量,用于标识系统操作中的具体错误类型。
具体来说是通过声明全局错误变量errno,供程序获取最近一次错误状态。
这些错误码是操作系统与应用程序之间沟通错误信息的标准化方式,几乎所有系统调用和库函数在执行失败时都会通过errno返回对应的错误码。
2.本源码文件产生的背景
在系统或者标准C语言中有个名为 errno 的变量,关于在C 标准中是否需要这个变量,在C 标准化
组织(X3J11)中引起了很大争论。但是争论的结果是没有去掉 errno,反而创建了名称为“errno.h"的头
文件。 因为标准化组织希望每个库函数或数据对象都需要在一个相应的标准头文件中作出声明。
主要原因在于:对于内核中的每个系统调用,如果其返回值就是指定系统调用的结果值的话,就很难报告出错情况。标准C 库函数返回值仅告知是否发生出错,**还必须从其他地方了解出错的类型,因此采用了errno 这个变量。**在某些情况下,程序从返回的-1 值知道出错了,但如果想知道具体的出错号,就可以通过读取 errno 的值来确定最后一次错误的出错号。
本文件虽然只是定义了Linux 系统中的一些出错码(出错号)的常量符号,而且 Linus 考虑程序的兼容性也想把这些符号定义成与POSIX标准中的一样。但是不要小看这个简单的代码,该文件也是 SCO公司指责Linux 操作系统侵犯其版权所列出的文件的之一。
下面再逐行解释源码:
3.源码注释版本
#ifndef _ERRNO_H
#define _ERRNO_H
// 声明一个外部全局整数变量errno。
// 作用:errno是 C 语言中用于保存最近一次系统调用或库函数错误状态的变量,由系统自动设置。
extern int errno;
/**
* data descp: 命名规则:以E开头,后续字母为错误描述的缩写(如EPERM即 "Error PERMission")。
* 这些错误码对应系统调用或库函数执行失败的具体原因,应用程序可通过errno获取并判断错误类型。
*/
#define ERROR 99 // 定义通用错误码ERROR,值为 99(可能用于未明确分类的错误)。
#define EPERM 1 // Operation not permitted(操作不允许:权限不足)
#define ENOENT 2 // No such file or directory(无此文件或目录)
#define ESRCH 3 // No such process(无此进程)
#define EINTR 4 // Interrupted system call(系统调用被中断)
#define EIO 5 // I/O error(输入/输出错误)
#define ENXIO 6 // No such device or address(无此设备或地址)
#define E2BIG 7 // Argument list too long(参数列表过长)
#define ENOEXEC 8 // Exec format error(执行格式错误:文件不可执行)
#define EBADF 9 // Bad file descriptor(无效的文件描述符)
#define ECHILD 10 // No child processes(无子进程)
#define EAGAIN 11 // Try again(资源暂时不可用,可重试)
#define ENOMEM 12 // Out of memory(内存不足)
#define EACCES 13 // Permission denied(权限被拒绝)
#define EFAULT 14 // Bad address(无效的内存地址)
#define ENOTBLK 15 // Block device required(需要块设备)
#define EBUSY 16 // Device or resource busy(设备或资源正忙)
#define EEXIST 17 // File exists(文件已存在)
#define EXDEV 18 // Cross-device link(跨设备链接)
#define ENODEV 19 // No such device(无此设备)
#define ENOTDIR 20 // Not a directory(不是目录)
#define EISDIR 21 // Is a directory(是目录,而非文件)
#define EINVAL 22 // Invalid argument(无效的参数)
#define ENFILE 23 // File table overflow(系统打开文件数超限)
#define EMFILE 24 // Too many open files(进程打开文件数超限)
#define ENOTTY 25 // Not a typewriter(非终端设备,不支持终端操作)
#define ETXTBSY 26 // Text file busy(文本文件正忙,如执行中的程序)
#define EFBIG 27 // File too large(文件过大)
#define ENOSPC 28 // No space left on device(设备存储空间不足)
#define ESPIPE 29 // Illegal seek(非法的seek操作,如管道)
#define EROFS 30 // Read-only file system(只读文件系统)
#define EMLINK 31 // Too many links(链接数过多)
#define EPIPE 32 // Broken pipe(管道已断开)
#define EDOM 33 // Math argument out of domain of func(数学函数参数域错误)
#define ERANGE 34 // Math result not representable(数学结果超出表示范围)
#define EDEADLK 35 // Resource deadlock would occur(可能发生资源死锁)
#define ENAMETOOLONG 36 // File name too long(文件名过长)
#define ENOLCK 37 // No locks available(无可用锁)
#define ENOSYS 38 // Function not implemented(函数未实现)
#define ENOTEMPTY 39 // Directory not empty(目录非空)
#endif
4.源码完整版
#ifndef _ERRNO_H
#define _ERRNO_H
/*
* ok, as I hadn't got any other source of information about
* possible error numbers, I was forced to use the same numbers
* as minix.
* Hopefully these are posix or something. I wouldn't know (and posix
* isn't telling me - they want $$$ for their f***ing standard).
*
* We don't use the _SIGN cludge of minix, so kernel returns must
* see to the sign by themselves.
*
* NOTE! Remember to change strerror() if you change this file!
*/
extern int errno;
#define ERROR 99
#define EPERM 1
#define ENOENT 2
#define ESRCH 3
#define EINTR 4
#define EIO 5
#define ENXIO 6
#define E2BIG 7
#define ENOEXEC 8
#define EBADF 9
#define ECHILD 10
#define EAGAIN 11
#define ENOMEM 12
#define EACCES 13
#define EFAULT 14
#define ENOTBLK 15
#define EBUSY 16
#define EEXIST 17
#define EXDEV 18
#define ENODEV 19
#define ENOTDIR 20
#define EISDIR 21
#define EINVAL 22
#define ENFILE 23
#define EMFILE 24
#define ENOTTY 25
#define ETXTBSY 26
#define EFBIG 27
#define ENOSPC 28
#define ESPIPE 29
#define EROFS 30
#define EMLINK 31
#define EPIPE 32
#define EDOM 33
#define ERANGE 34
#define EDEADLK 35
#define ENAMETOOLONG 36
#define ENOLCK 37
#define ENOSYS 38
#define ENOTEMPTY 39
#endif
5.源码图像版

6.源码注释版图像


本系列将直击C语言的本质基础,流利处理出各个易错、实用的实战点,并非从零开始学习C。
专注讲解Linux中的常用命令,共计发布100+文章。
本系列将精讲Linux0.11内核中的每一个文件,共计会发布100+文章。
😉【Linux102】11-kernel/vsprintf.c
😉【Linux102】12-include/stdarg.h
😉【Linux102】14-kernel/system_call.s
😉【Linux102】15-include/linux/sched.h
😉【Linux102】15-1-task_struct 进程结构体
😉【Linux102】18-include/signal.h
😉【Linux102】19-include/sys/types.h
😉【Linux102】20-include/linux/kernel.h
😉【Linux102】21-include/asm/segment.h
😉【Linux102】22-include/linux/head.h
😉【Linux102】23-include/linux/mm.h
😉【Linux102】24-include/linux/fs.h
和Linux内核102系列不同,本系列将会从全局描绘Linux内核的各个模块,而非逐行源码分析,适合想对Linux系统有宏观了解的家人阅读。
😉【Linux】Linux概述1-linux对物理内存的使用
关于小希
😉嘿嘿嘿,我是小希,专注Linux内核领域,同时讲解C语言、汇编等知识。
我的微信:C_Linux_Cloud,期待与您学习交流!

加微信请备注哦
小希的座右铭:
别看简单,简单也是难。别看难,难也是简单。我的文章都是讲述简单的知识,如果你喜欢这种风格:
下一期想看什么?在评论区留言吧!我们下期见!

797

被折叠的 条评论
为什么被折叠?



