Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
452 views
in Technique[技术] by (71.8m points)

c - Determine Original Exit Status Code

In a software baseline I am maintaining, there are 150 statements spread out amongst various C applications that make a call to either another Linux command (e.g. rm -rf ...) or custom application using status = system(cmd)/256. When either is called, the status code returned from either the Linux command or custom application is divided by 256. So that when the status code is greater than 0, we know there was a problem. However, the way the software was written, it doesn't always log what command or application returned the status code. So that if the status code was say 32768, when divided by 256, the status code reported is 128.

The software is old and while I could make changes, it would be nice if any of the commands called or applications called reported their original status code elsewhere.

Is there a way to determine the original status code in a standard Linux log file and the application which returned it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How to write a wrapper

Following an example on how to apply a wrapper around the libc function system().

Create a new module (translation units) called system_wrapper.c like so:

The header system_wrapper.h:

#ifndef _SYSTEM_WRAPPER
#define _SYSTEM_WRAPPER

#define system(cmd) system_wrapper(cmd)

int system_wrapper(const char *);

#endif

The module system_wrapper.c:

#include <stdlib.h> /* to prototype the original function, that is libc's system() */
#include "system_wrapper.h"

#undef system

int system_wrapper(const char * cmd)
{
  int result = system(cmd);

  /* Log result here. */

  return result;
}

Add this line to all modules using system():

#include "system_wrapper.h"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...