Oracle® Solaris 11.2 Dynamic Tracing Guide

Exit Print View

Updated: July 2014
 
 

ftruncate

ftruncate action truncates standard output file, but does not truncate standard error file. To truncate standard error file, you have to re-open stdout as stderr, truncate, then re-open the original stdout. Following is an example:

# cat > script.d <<EOF
#!/usr/sbin/dtrace -s

#pragma D option quiet
#pragma D option destructive

BEGIN
{
	printf("data\n");
	freopen("/dev/stderr");
	ftruncate();
	freopen("");	/* re-open original stdout */
	printf("more data\n");
	exit(0);
}
EOF
# chmod a+x script.d
# echo foo > stdout
# echo foofoo > stderr
# ls -l std*
-rw-r--r--   1 root     root           7 Feb 27 14:21 stderr
-rw-r--r--   1 root     root           4 Feb 27 14:21 stdout
# ./script.d >> stdout 2 >> stderr
# ls -l std*
-rw-r--r--   1 root     root           0 Feb 27 14:26 stderr
-rw-r--r--   1 root     root          19 Feb 27 14:26 stdout
# cat stdout
foo
data
more data
#