Nic*_*nks 5 c linux cross-platform
Windows有不同类型的状态码,HRESULT以及NTSTATUS例如,允许用于表示成功和失败的值。这样就可以使用FAILED和这样的宏NT_SUCCESS。成功状态代码的一个示例是STATUS_PENDING指示IO尚未完成。
我正在尝试在Linux中映射相似的样式状态代码。我有一个(WIP)跨平台库,该库在Windows上使用上述类型的状态代码(用户模式和内核模式)。我有包装上述宏的“ FAILED”宏。对于这一切,我都需要一个等效的Linux。
我环顾四周,没有发现任何有趣/有帮助的地方。对于可能返回并出错的任何系统/平台功能(套接字,线程,锁),我需要确保将其转换为适当的状态码类型。Windows在这类事情上有很多帮助者,但是我不确定Linux。
伪代码示例:
MY_STATUS SendAsync(...) {
ASYNC_OPERATION* Operation = MY_ALLOC(...)
if (Operation == NULL) {
return MY_OUT_OF_MEMORY_ERROR; // Error status
}
// Build the operation
MY_STATUS Status = QueueOperation(Operation);
if (MY_FAILED(Status)) {
return Status;
}
return MY_STATUS_PENDING; // Success status
}
Run Code Online (Sandbox Code Playgroud)
然后,调用方应该可以具有以下代码:
MY_STATUS Status = SendAsync(...)
if (MY_FAILED(Status)) {
// Bail
}
// Handle success
Run Code Online (Sandbox Code Playgroud)
我认为问题的症结在于,我不知道如何将系统/平台函数调用中的状态代码填充到支持成功/失败的通用类型中。通常,看起来所有状态代码都是肯定的,所以我只能说我的成功错误代码是<= 0。但是我不确定这是否是一个很好的解决方案。
需要明确的是,由于有人提出这个问题可能是答案,所以我并没有试图将Linux状态/错误代码强制HRESULT明确地格式化。我只是试图找到一种将错误代码表示为成功或失败的方法。如果我必须一路转换系统错误代码(否定它们?),那是可以接受的。如果没有真正解决此问题的方法,那将是一个可以接受的答案(尽管不希望如此),我将不得不寻找一些解决方法。
For error codes in linux there are the constants defined in errno.h header file. The error constants are available from man page man 3 errno. Also there is: What errno means
As for exit status codes, there is no standard set of codes. The value 0 has always denoting success, and non-zero value has denoted an error state. Bash, for example has this Bash exit status information.
All of the Bash builtins return an exit status of zero if they succeed and a non-zero status on failure, so they may be used by the conditional and list constructs. All builtins return an exit status of 2 to indicate incorrect usage, generally invalid options or missing arguments.
有sysexits这是试图定义一组状态代码,但它在Linux软件领域在很大程度上被忽略。
有关更多信息:Linux中的退出状态代码
| 归档时间: |
|
| 查看次数: |
72 次 |
| 最近记录: |