博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux系统调用出错时的处理函数
阅读量:6965 次
发布时间:2019-06-27

本文共 2708 字,大约阅读时间需要 9 分钟。

hot3.png

     在进行linux的系统调用, 要判断调用的成功与否, 调用失败的情况下就要进行一定的处理,除了打印出消息, 还可以打印系统调用的出错信息,  一般性的错误不必退出程序, 要是致命性的错误就终止整个程序, 基于这个思路《apue》, 和《unix网络编程》都提供了一系列的出错处理接口, 很有参考价值, 接口和代码罗列如下。

    头文件

#ifndef  _SYS_ERROR_H__#define  _SYS_ERROR_H__
//非致命的系统错误,不终止程序extern void err_msg(const char *fmt, ...);
//非致命的系统错误,不终止程序, 附带打印系统的出错信息extern void err_ret(const char *fmt, ...);
//致命的系统错误,终止程序extern void err_quit(const char *fmt, ...);
//致命的系统错误,终止程序, 附带打印系统的出错信息extern void err_sys(const char *fmt, ...);
//致命的系统错误,终止程序, 附带打印系统的出错信息extern void err_dump(const char *fmt, ...);
#endif //_SYS_ERROR_H__

   源文件:

 #include 
#include 
  /* ANSI C header file */#include 
  /* for syslog() */#include 
#include 
#include 
#include "syserror.h"
#define HAVE_VSNPRINTF 1#define MAXLINE 1024
int  daemon_proc;  /* set nonzero by daemon_init() */
static void err_doit(int, int, const char *, va_list);
/* Nonfatal error related to system call * Print message and return */
voiderr_ret(const char *fmt, ...){ va_list  ap;
 va_start(ap, fmt); err_doit(1, LOG_INFO, fmt, ap); va_end(ap); return;}
/* Fatal error related to system call * Print message and terminate */
voiderr_sys(const char *fmt, ...){ va_list  ap;
 va_start(ap, fmt); err_doit(1, LOG_ERR, fmt, ap); va_end(ap); exit(1);}
/* Fatal error related to system call * Print message, dump core, and terminate */
voiderr_dump(const char *fmt, ...){ va_list  ap;
 va_start(ap, fmt); err_doit(1, LOG_ERR, fmt, ap); va_end(ap); abort();  /* dump core and terminate */ exit(1);  /* shouldn't get here */}
/* Nonfatal error unrelated to system call * Print message and return */
voiderr_msg(const char *fmt, ...){ va_list  ap;
 va_start(ap, fmt); err_doit(0, LOG_INFO, fmt, ap); va_end(ap); return;}
/* Fatal error unrelated to system call * Print message and terminate */
voiderr_quit(const char *fmt, ...){ va_list  ap;
 va_start(ap, fmt); err_doit(0, LOG_ERR, fmt, ap); va_end(ap); exit(1);}
/* Print message and return to caller * Caller specifies "errnoflag" and "level" */
static voiderr_doit(int errnoflag, int level, const char *fmt, va_list ap){ int  errno_save, n; char buf[MAXLINE + 1];
 errno_save = errno;  /* value caller might want printed */#ifdef HAVE_VSNPRINTF vsnprintf(buf, MAXLINE, fmt, ap); /* safe */#else vsprintf(buf, fmt, ap);     /* not safe */#endif n = strlen(buf); if (errnoflag)  snprintf(buf + n, MAXLINE - n, ": %s", strerror(errno_save)); strcat(buf, "\n");
 if (daemon_proc) {  syslog(level, buf); } else {  fflush(stdout);  /* in case stdout and stderr are the same */  fputs(buf, stderr);  fflush(stderr); } return;}

 

转载于:https://my.oschina.net/u/218060/blog/192000

你可能感兴趣的文章
7-14
查看>>
memcached原理详述及配置
查看>>
Requested bean is currently in creation: Is there an unresolvable circular reference?
查看>>
SQL Loader 的使用详解
查看>>
OAF打开新的窗口
查看>>
Object-C 如何把一个时间戳转换为一个标准的时间格式?
查看>>
在window和linux上通用的SprtLock类头实现文件
查看>>
文字在div中垂直居中
查看>>
C++中四种类型装换
查看>>
[iOS Animation]-CALayer 图层几何学
查看>>
设置oracle服务自动启动
查看>>
Linux 学习日记 2: 目录结构和文件操作
查看>>
AndEngine引擎学习之绘制直线
查看>>
TCP_Wrappers 基于TCP的安全控制
查看>>
android开机启动代码
查看>>
加速你的php数组
查看>>
android中常用的快捷键
查看>>
Mysql 面试复习
查看>>
《Java编程思想》第四版读书笔记 第十四章 类型信息
查看>>
我的友情链接
查看>>