📜  Node.js 操作系统常量

📅  最后修改于: 2022-05-13 01:56:28.104000             🧑  作者: Mango

Node.js 操作系统常量

os.constants是 os 模块的内置应用程序编程接口,用于获取操作系统指定的错误代码常量、信号常量、优先级常量、dlopen 常量等。
句法:

os.constants

返回值:它返回一个包含操作系统指定常量的对象。示例:错误代码、信号常量、优先级常量、dlopen 常量等。
可以使用 os.constants.signals 访问信号常量,并且可以使用 os.constants.priority 访问优先级常量。
下面列出了一些常量:
信号常数:

ConstantDescription
SIGHUPIt is sent to specify when a parent process exits or a controlling terminal is closed.
SIGINTIt is sent to specify when a user interrupts a process. (like (Ctrl+C)).
SIGQUITIt is sent to specify that a user wishes to terminate the process & perform a core dump.
SIGILLIt is sent to a process to notify that it has attempted to perform a malformed, unknown, illegal, or privileged instruction.
SIGTRAPIt is sent to a process on exception occurred.
SIGABRTIt is sent to a process to request abort.
SIGIOTIt is the synonym for SIGABRT.
SIGBUSIt is sent to a process to notify for a bus error.
SIGFPEIt is sent to a process to notify for an illegal arithmetic operation.
SIGKILLIt is sent to a process to terminate immediately.
SIGUSR1 SIGUSR2It is sent to a process to recognize user-defined conditions.
SIGSEGVIt is sent to a process to notify for a segmentation fault.
SIGPIPEIt is sent to a process when it has tried to write to a disconnected pipe.
SIGALRMIt is sent to a process for a system timer elapses.
SIGTERMIt is sent to a process to request termination.
SIGCHLDIt is sent to a process after a child process terminates.
SIGSTKFLTIt is sent to a process to specify a stack fault on a coprocessor.
SIGCONTIt is sent to tell the OS to continue a paused process.
SIGSTOPIt is sent to direct the OS to halt a process.
SIGTSTPIt is sent to a process to request it to stop.
SIGBREAKIt is sent to specify that a user wishes to interrupt a process.
SIGTTINIt is sent when a process reads from the TTY while in the background.
SIGTTOUIt is sent when a process writes to the TTY while in the background.
SIGURGIt is sent to a process to indicate that a socket has urgent data to read.
SIGXCPUIt is sent when a process has exceeded its CPU usage limit.
SIGXFSZIt is sent when a process grows a file larger than the maximum allowed.
SIGVTALRMIt is sent to a process for an elapsed virtual timer occurrence.
SIGPROFIt is sent to a process after a system timer has elapsed.
SIGWINCHIt is sent to a process after a controlling terminal changed its size.
SIGIOIt is sent to a process to signify that I/O is available.
SIGPOLLIt is the synonym for SIGIO.
SIGLOSTIt is sent to a process to indicate that a file lock has been lost.
SIGPWRIt is sent to a process to notify of a power failure.
SIGINFOIt is the synonym for SIGPWR.
SIGSYSIt is sent to a process to notify for bad argument.
SIGUNUSEDIt is the synonym for SIGSYS.

错误常数:
1. POSIX 错误常数

ConstantDescription
E2BIGIt is to specify a long list of arguments than expected.
EACCESIt is to specify insufficient permissions for the operation.
EADDRINUSEIt is to specify an already used network address.
EADDRNOTAVAILIt is to specify the network address currently unavailable.
EAFNOSUPPORTIt is to specify the unsupported network address family.
EAGAINIt is to specify that there is no data available and to try the operation again later.
EALREADYIt is to specify that the socket has a pending connection in progress.
EBADFIt is to specify an invalid file descriptor.
EBADMSGIt is to specify an invalid data message.
EBUSYIt is to specify that a device or resource is busy.
ECANCELEDIt is to specify that an operation was canceled.
ECHILDIt is to specify not to have any child processes.
ECONNABORTEDIt is to specify the aborted network connection.
ECONNREFUSEDIt is to specify the refused network connection.
ECONNRESETIt is to specify the reset network connection.
EDEADLKIt is to specify an avoided resource deadlock.
EDESTADDRREQIt is to specify that a destination address is required.
EDOMIt is to specify that an argument is out of the domain of the function.
EDQUOTIt is to specify that the disk quota has been exceeded.
EEXISTIt is to specify that the file already exists.
EFAULTIt is to specify an invalid pointer address.
EFBIGIt is to specify that the file is too large.
EHOSTUNREACHIt is to specify that the host is unreachable.
EIDRMIt is to specify that the identifier has been removed.
EILSEQIt is to specify an illegal byte sequence.
EINPROGRESSIt is to specify that an operation is already in progress.
EINTRIt is to specify that a function call was interrupted.
EINVALIt is to specify that an invalid argument was provided.
EIOIt is to specify an otherwise unspecified I/O error.
EISCONNIt is to specify that the socket is connected.
EISDIRIt is to specify that the path is a directory.
ELOOPIt is to specify too many levels of symbolic links in a path.
EMFILEIt is to specify that there are too many open files.
EMLINKIt is to specify that there are too many hard links to a file.
EMSGSIZEIt is to specify that the provided message is too long.
EMULTIHOPIt is to specify that a multihop was attempted.
ENAMETOOLONGIt is to specify that the filename is too long.
ENETDOWNIt is to specify that the network is down.
ENETRESETIt is to specify that the connection has been aborted by the network.
ENETUNREACHIt is to specify that the network is unreachable.
ENFILEIt is to specify too many open files in the system.
ENOBUFSIt is to specify that no buffer space is available.
ENODATAIt is to specify that no message is available on the stream head read queue.
ENODEVIt is to specify that there is no such device.
ENOENTIt is to specify that there is no such file or directory.
ENOEXECIt is to specify an exec format error.
ENOLCKIt is to specify that there are no locks available.
ENOLINKIt is to specify that a link has been severed.
ENOMEMIt is to specify that there is not enough space.
ENOMSGIt is to specify that there is no message of the desired type.
ENOPROTOOPTIt is to specify that a given protocol is not available.
ENOSPCIt is to specify that there is no space available on the device.
ENOSRIt is to specify that there are no stream resources available.
ENOSTRIt is to specify that a given resource is not a stream.
ENOSYSIt is to specify that a function has not been implemented.
ENOTCONNIt is to specify that the socket is not connected.
ENOTDIRIt is to specify that the path is not a directory.
ENOTEMPTYIt is to specify that the directory is not empty.
ENOTSOCKIt is to specify that the given item is not a socket.
ENOTSUPIt is to specify that a given operation is not supported.
ENOTTYIt is to specify an inappropriate I/O control operation.
ENXIOIt is to specify no such device or address.
EOPNOTSUPPIt is to specify that an operation is not supported on the socket. Although ENOTSUP and EOPNOTSUPP have the same value on Linux, according to POSIX.1 these error values should be distinct)
EOVERFLOWIt is to specify that a value is too large to be stored in a given data type.
EPERMIt is to specify that the operation is not permitted.
EPIPEIt is to specify a broken pipe.
EPROTOIt is to specify a protocol error.
EPROTONOSUPPORTIt is to specify an unsupported protocol.
EPROTOTYPEIt is to specify the wrong type of protocol for a socket.
ERANGEIt is to specify that the results are too large.
EROFSIt is to specify that the file system is read-only.
ESPIPEIt is to specify an invalid seek operation.
ESRCHIt is to specify that there is no such process.
ESTALEIt is to specify that the filehandle is stale.
ETIMEIt is to specify an expired timer.
ETIMEDOUTIt is to specify hat the connection timed out.
ETXTBSYIt is to specify that a text file is busy.
EWOULDBLOCKIt is to specify that the operation would block.
EXDEVIt is to specify an improper link.

2. Windows 特定错误

Constant CodesConstant Description
WSAEINTRIt is to specify an interrupted function call.
WSAEBADFIt is to specify an invalid filehandle.
WSAEACCESIt is to specify insufficient permissions to complete the operation.
WSAEFAULTIt is to specify an invalid pointer address.
WSAEINVALIt is to specify that an invalid argument was passed.
WSAEMFILEIt is to specify that there are too many open files.
WSAEWOULDBLOCKIt is to specify that a resource is temporarily unavailable.
WSAEINPROGRESSIt is to specify that an operation is currently in progress.
WSAEALREADYIt is to specify that an operation is already in progress.
WSAENOTSOCKIt is to specify that the resource is not a socket.
WSAEDESTADDRREQIt is to specify that a destination address is required.
WSAEMSGSIZEIt is to specify that the message size is too long.
WSAEPROTOTYPEIt is to specify the wrong protocol type for the socket.
WSAENOPROTOOPTIt is to specify a bad protocol option.
WSAEPROTONOSUPPORTIt is to specify that the protocol is not supported.
WSAESOCKTNOSUPPORTIt is to specify that the socket type is not supported.
WSAEOPNOTSUPPIt is to specify that the operation is not supported.
WSAEPFNOSUPPORTIt is to specify that the protocol family is not supported.
WSAEAFNOSUPPORTIt is to specify that the address family is not supported.
WSAEADDRINUSEIt is to specify that the network address is already in use.
WSAEADDRNOTAVAILIt is to specify that the network address is not available.
WSAENETDOWNIt is to specify that the network is down.
WSAENETUNREACHIt is to specify that the network is unreachable.
WSAENETRESETIt is to specify that the network connection has been reset.
WSAECONNABORTEDIt is to specify that the connection has been aborted.
WSAECONNRESETIt is to specify that the connection has been reset by the peer.
WSAENOBUFSIt is to specify that there is no buffer space available.
WSAEISCONNIt is to specify that the socket is already connected.
WSAENOTCONNIt is to specify that the socket is not connected.
WSAESHUTDOWNIt is to specify that data cannot be sent after the socket has been shutdown.
WSAETOOMANYREFSIt is to specify that there are too many references.
WSAETIMEDOUTIt is to specify that the connection has timed out.
WSAECONNREFUSEDIt is to specify that the connection has been refused.
WSAELOOPIt is to specify that a name cannot be translated.
WSAENAMETOOLONGIt is to specify that a name was too long.
WSAEHOSTDOWNIt is to specify that a network host is down.
WSAEHOSTUNREACHIt is to specify that there is no route to a network host.
WSAENOTEMPTYIt is to specify that the directory is not empty.
WSAEPROCLIMIt is to specify that there are too many processes.
WSAEUSERSIt is to specify that the user quota has been exceeded.
WSAEDQUOTIt is to specify that the disk quota has been exceeded.
WSAESTALEIt is to specify a stale file handle reference.
WSAEREMOTEIt is to specify that the item is remote.
WSASYSNOTREADYIt is to specify that the network subsystem is not ready.
WSAVERNOTSUPPORTEDIt is to specify that the winsock.dll version is out of range.
WSANOTINITIALISEDIt is to specify that successful WSAStartup has not yet been performed.
WSAEDISCONIt is to specify that graceful shutdown is in progress.
WSAENOMOREIt is to specify that there are no more results.
WSAECANCELLEDIt is to specify that an operation has been canceled.
WSAEINVALIDPROCTABLEIt is to specify that the procedure call table is invalid.
WSAEINVALIDPROVIDERIt is to specify an invalid service provider.
WSAEPROVIDERFAILEDINITIt is to specify that the service provider failed to initialize.
WSASYSCALLFAILUREIt is to specify a system call failure.
WSASERVICE_NOT_FOUNDIt is to specify that a service was not found.
WSATYPE_NOT_FOUNDIt is to specify that a class type was not found.
WSA_E_NO_MOREIt is to specify that there are no more results.
WSA_E_CANCELLEDIt is to specify that the call was canceled.
WSAEREFUSEDIt is to specify that a database query was refused.

dlopen 常量:

Constant CodeConstant Description
RTLD_LAZYIt performs lazy binding. Node.js sets this flag by default.
RTLD_NOWIt solve all undefined symbols in the library before dlopen(3) returns.
RTLD_GLOBALThese symbols defined by the library will be made available for symbol resolution of subsequently loaded libraries.
RTLD_LOCALThe converse of RTLD_GLOBAL. This is the default behavior if neither flag is specified.
RTLD_DEEPBINDthis make a self-contained library use its own symbols in preference to symbols from previously loaded libraries.

优先级常数:

Constant CodesConstant Description
PRIORITY_LOWThe minimum process scheduling priority.
PRIORITY_BELOW_NORMALThe process scheduling priority is below PRIORITY_NORMAL and above PRIORITY_LOW.
PRIORITY_NORMALThe process scheduling priority is normal or default.
PRIORITY_ABOVE_NORMALThe process scheduling priority is below PRIORITY_HIGH and above PRIORITY_NORMAL.
PRIORITY_HIGHThe process scheduling priority below PRIORITY_HIGHEST and above PRIORITY_ABOVE_NORMAL.
PRIORITY_HIGHESTProcess scheduling priority is maximum.

libuv 常量:

Constant CodesConstant Description
UV_UDP_REUSEADDR 

下面的例子说明了os.constants在 Node.js 中的使用:
示例 1:

javascript
// Import os module
const os = require('os');
 
constants = os.constants;
 
// Printing all os.constants
console.log(constants);


javascript
// Import os module
const os = require('os');
 
constants = os.constants;
 
// Printing os.constants
 
// Signals constants
if(constants.signals != undefined){
    console.log("Signals:");
    console.log(constants.signals);
}
 
// Priority constants
if(constants.priority!=undefined){
    console.log("priority:");
    console.log(constants.priority);
}


输出:

[Object: null prototype] {
  UV_UDP_REUSEADDR: 4,
  dlopen: [Object: null prototype] {},
  errno:
   [Object: null prototype] {
     E2BIG: 7,
     EACCES: 13,
     EADDRINUSE: 100,
     . . .
     PRIORITY_HIGH: -14,
     PRIORITY_HIGHEST: -20 } }

示例 2:

javascript

// Import os module
const os = require('os');
 
constants = os.constants;
 
// Printing os.constants
 
// Signals constants
if(constants.signals != undefined){
    console.log("Signals:");
    console.log(constants.signals);
}
 
// Priority constants
if(constants.priority!=undefined){
    console.log("priority:");
    console.log(constants.priority);
}

输出:

Signals:
[Object: null prototype] {
  SIGHUP: 1,
  SIGINT: 2,
  . . .
  PRIORITY_HIGH: -14,
  PRIORITY_HIGHEST: -20 }

注意:以上程序将使用 node index.js 命令编译运行。
参考: https://nodejs.org/api/os.html#os_os_constants