Tips for implementing component return in Linux

Source: Internet
Author: User
Article Title: Tips for implementing component return in Linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Author: Guo Hongfeng
  
When writing a Linux Runtime Library, especially when it is submitted to other programmers as a component, it is necessary to provide a response message to improve the availability of the component during development. This article describes some practical skills.
When writing C/C ++ applications, we are very familiar with the processing of returned messages in system calls. Two types of information are contained in the returned message. One is the returned information code, indicating the returned information. The other is the text description of the returned information, indicating whether the call is successful. These two types of information have their respective functions and cannot be replaced by each other. For example, if the system call to load the memory has been loaded, the call may return an error, but the program may continue to run correctly. If you need to stop the program, you need to know the specific cause of program suspension. In this case, it is not enough to rely only on the returned Message ID. The returned message text instructions immediately let the user know the cause of the error. When writing a Linux Runtime Library, especially when it is submitted to other programmers as a component, it is necessary to provide similar response messages to improve the availability of the component during development.
  
1. Use the returned messages in the Standard C library
In the method or process of your component, if the message is average, you can directly use the return message in the Standard C library. The returned message code in the Standard C library is defined in the header file errno. in h, the returned message is represented by an integer starting from 0, where 0 is the correct return value, and others are the Warning type or error type return values. In the standard C library, a global variable errno is used to specify the returned message value of the current call. In the component method or process, you can directly set the errno value. Of course, it must comply with the type definition in errno. h. When calling a method or process in a component, you can directly call the perror () or strerrno () function in C to submit a text description of the returned message.
  
2. Implement custom response messages
Generally, messages in Standard C are insufficient to reflect the running status of methods or processes in their components. In this case, you need to implement your own unique response message. The main steps are as follows:
  
First, it is required to return the message code.
Some of the running states of methods or processes in components must be returned values. These return values must reflect multiple complex running states, there are successful return values, General running message values, warning values, general error values, and severe error values. The returned message code directly reflects these statuses. In addition, the return value also needs to reflect that the method or process is located in that component. We can use the first two digits of an integer to identify the return type. The successful return value and general running message values are identified as 0X20000000 and 0 X respectively. The warning value, general error value, and severe error value are negative values, which can be identified as 0X80000000,0X90000000, 0XA0000000 respectively. The component can use the second byte identifier. For example, if the transaction security component uses the 11 identifier, it can use the 0X000B00000 identifier. The specific code of the returned message can follow the Representation Method in the Standard C library. For example, the correct return value is identified by 0, and a serious error is represented by 5. For the transaction security component, the two messages can be identified as 0X100B0000 and 0XA00B0005.
  
Second, describe the text description of the returned message
The text description of the returned message is listed in a string array of the component. The text description of each message is determined by the message code value in the array.
  
For example, the transaction security component defines the following four returned messages: 0, the return value of success; 1, the transaction definition error; 2, the transaction commit error; 3, the transaction returns an error. You can define the following string array:
  
Char * ReturnMessage_11 [] =
{
"Successful return ",
"Transaction definition error ",
"Transaction commit error ",
"Transaction return error ",
(Char *) 0
};
  
};
  
  
  
The returned message codes are 0X100B0000, 0X900B0001, 0X900B0002, and 0X900B0003.
  
  
Third, the registration component returns a message.
When using components to develop applications, register components in the component constructor or initialization function to return messages. This step is hidden inside the component. An external application provides a global msg_ptr pointer to the component's returned message string array. The registration code is as follows:
  
Void * handle;
Char * messageShow;
  
// Load the dynamic library of this component
Handle = dlopen (NULL, RTLD_LAZY );
// Obtain the component ID
Sprintf (messageShow, "ReturnMessage _ % d", ComponentIdx );
// Get the pointer to the array of message strings returned by the component
Msg_ptr [ComponentIdx] = dlsym (handle, messageShow );
  
  
  
  
  
Fourth, obtain the text description of the message returned by the component.
After registering a component to return a message, when the application calls a method or process in the component, the returned message content can be determined directly from the returned code. You can also call the StrError (int rts) method in the component to obtain the text description of the message returned by the component.
  
The code for this method is as follows:
  
Int ErrnoIdx;
Int CompomentIdx;
// Obtain the specific code of the returned message
ErrnoIdx = rts & 0 xFFFF;
// Obtain the component ID
CompomentIdx = (rts & 00FF0000)> 16;
// Return the text description of the message returned by the component
Return msg_ptr [CompomentIdx] [ErrnoIdx];
  
  
  
  
  
  
Using the above techniques, we can make the released Linux package have good availability.
  
  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.