Trusted Solaris Developer's Guide

Translating Process Clearances

Clearances (like labels) can be represented in binary, text, or hexadecimal. Within the kernel all clearances are stored in binary form, and binary is the form used for clearances passed to and received from programming interfaces.

Binary and Text

This example translate a binary clearance to text using long words. The process running at TS A B equals the clearance and needs no privilege.


Note -

The text input and output formats, rules, and flags are presented in "Binary and Text Label Translation".


#include <tsol/label.h>
main()
{
	int          retval, length = 0;
	bclear_t     pclear;
	char         *string = (char *)0;

	retval = getclearance(&pclear);
	retval = bcleartos(&pclear, &string, length, LONG_WORDS);
	printf("Process clearance = %s\n", string);
}

The printf(1) statement prints the following:


Process clearance = TS ABLE BAKER

This example clips the process label to five characters. The clipping occurs when the number of characters in pclear is greater than the specified length.

#include <tsol/label.h>

main()
{
	int          retval;
	bclear_t     pclear;
	char         *string = (char *)0;

	retval = getclearance(&pclear);
	string = sbcleartos(&pclear, 5);
	printf("Clipped process clearance = %s\n", string);
}

The printf statement prints the following. The left arrow is a clipped indicator to show the name has been clipped. The number of characters to which the name is clipped includes two characters for the clipped indicator.


Clipped process clearance = TS<-

This example translates a text string to a binary clearance.

#include <tsol/label.h>

main()
{
	int          retval, error;
	bclear_t     bclear;
	char         *labelstring = "TS ABLE BAKER";

	retval = stobclear(labelstring, &bclear, NEW_LABEL, &error);
	if (retval == 0)
		printf("Error = %d\n", error);
	else
		printf("Retval = %d\n", retval);
}

The printf(1) statement prints the following:


Retval = 1

Binary and Hexadecimal

There are two types of binary to hexadecimal routines: regular and reentrant. Both types of routines return a pointer to a string that contains the result of the translation or NULL if the clearance passed in is not type bclear_t.

Regular

This example translates the binary process clearance to hexadecimal and back.

#include <tsol/label.h>

main()
{
	int          retval;
	bclear_t     hclear;
	char         *string ;

	retval = getclearance(&hclear);

	if((string = bcleartoh(&hclear)) != 0)
		printf("Hex string = %s\n", string);

	retval = htobclear(string, &hclear);
	printf("Return Value = %d\n", retval);
}

The first printf statement prints the binary clearance in the following hexadecimal format:


Hex string = 0xClearance hexadecimal value

The second printf statement prints the following where non-zero indicates a successful translation:


Return Value = 1

Reentrant

The reentrant (MT-SAFE) routine bcleartoh_r(3TSOL) requires the allocation and freeing of memory for the value returned. The h_alloc(3TSOL) routine is used to allocate this memory, sizing it appropriately for the type of label (in this case hexadecimal) to be converted.

type where type is a hexadecimal value that indicates that a defined clearance (SUN_CLR_ID) is translated to hexadecimal.

This example allocates memory for the translation type, translates the binary process clearance to hexadecimal, and frees the memory at the end.

#include <tsol/label.h>

main()
{
	bclear_t     hclear;
	char         *string, *hex;

	getclearance(&hclear);
	hex = h_alloc(SUN_CLR_ID);
	if((string = bcleartoh_r(&hclear, hex)) != 0);
		printf("Hex string = %s\n", string);

	h_free(hex);
}

The printf statement prints the binary clearance in the following hexadecimal format:


Hex string = 0x0006cc0000000000000000000000000000000000000 
000000003ffffffffffff0000