File: doaccnt.c

package info (click to toggle)
cti-ifhp 2.1.8-2
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 932 kB
  • ctags: 845
  • sloc: ansic: 6,036; sh: 1,524; makefile: 318
file content (155 lines) | stat: -rw-r--r-- 3,821 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**************************************************************************
 * LPRng IFHP Filter
 * Copyright 1994-1997 Patrick Powell, San Diego, CA <papowell@sdsu.edu>
 *
 * Based on the CTI printer filters.
 *  See COPYRIGHT for details.
 *
 * $Id: doaccnt.c,v 2.1 1997/01/05 18:41:38 papowell Exp $
 */

#include "portable.h"
#include "common.h"
#include "hp4.h"


/*
 *  doaccnt()
 *  writes the accounting information to the accounting file
 *  This has the format: user host printer pages format date
 */

static int argc;
static char *parms[256];
static char list[1024];
static char list2[1024];

void inv( char *key, char *value )
{
	int len, len2;
	len = strlen( list );
	len2 = strlen( list2 );
	if( value && *value ){
		if( key ){
			plp_snprintf( list+len, sizeof(list)-len,
					" %s'%s'", key, value );
			plp_snprintf( list2+len2, sizeof(list2)-len2,
					" %s%s", key, value );
		} else {
			plp_snprintf( list+len, sizeof(list)-len,
					" '%s'", value );
			plp_snprintf( list2+len2, sizeof(list2)-len2,
					" %s", value );
		}
		parms[argc++] = &list2[len2+1];
	}
	parms[argc] = 0;
}

void doaccnt(int start)
{
	int fd;
	int i, err, pid;
	int status;
	char pid_str[32];
	char totalpages[32];
	char pages[32];
	char *sh;
	struct stat statb;
	char opt_str[3];

	strcpy( opt_str, "-x" );
	sh = Accounting_script;
	log(2,"Accounting script '%s'", Accounting_script );

	plp_snprintf( pid_str, sizeof(pid_str),  "%d", (int)(getpid()) );

	/* we first set up the output line and arguments */
	plp_snprintf( pages, sizeof(pages),  "%d", npages );

	if( start ){
		strcpy( list, "start " );
	} else {
		strcpy( list, "end " );
	}
	argc = 0;
	parms[argc++] = sh;
	for( i = 0; i < 26; ++i ){
		if( Loweropts[i] ){
			opt_str[1] = i + 'a';
			inv( opt_str, Loweropts[i] );
		}
	}
	for( i = 0; i < 26; ++i ){
		if( Upperopts[i] ){
			opt_str[1] = i + 'A';
			inv( opt_str, Upperopts[i] );
		}
	}
	if( !start ){
		plp_snprintf( totalpages, sizeof(totalpages), "%d", npages-initialpagecount );
		inv( "-b", totalpages );
	}
	inv( "-q", pid_str );
	inv( "-p", pages );
	inv( "-t", Time_str() );

	/* this is probably the best way to do the output */
	fd = Accounting_fd;
	if(fd >= 0 ||
		(accntfile && (fd = open(accntfile, O_WRONLY|O_APPEND )) >= 0 )) {
		i = strlen( list );
		list[i] = '\n';
		writecn(fd,list,i+1);
		if( fd != Accounting_fd ) (void)close(fd);
		list[i] = 0;
	}
	if( !start && sh && *sh ){
		if( stat( sh, &statb ) < 0 ){
			logerr(3,"Accounting script '%s' cannot stat- %s",
				sh, Errormsg(errno) );
			sh = 0;
		} else if( ! ( (statb.st_mode & 0001)
			|| ((statb.st_mode & 0010 ) && (statb.st_gid == getegid() ))
			|| ((statb.st_mode & 0100 ) && (statb.st_uid == geteuid() )) ) ){
			logerr(3,"Accounting script '%s' has bad exec perms or ownership",
				sh );
			sh = 0;
		}
		if( sh == 0 ) return;
		inv( 0, accntfile );
		log(4,"accounting '%s %s'", parms[0], list );
		/* fix up parm list */
		for( i = 1; i < argc; ++i ){
			parms[i][-1] = 0;
		}
		parms[argc] = 0;
		monitpid = fork();
		if( monitpid == -1 ) {
			errorcode = FILTABORT;
			logerr(1,"Cannot fork to update accounting");
		} else if( monitpid == 0 ){
			/* child - Exec the script to update pages */
			dup2(2,1);
			execve(sh,parms,Envp);
			errorcode = FILTABORT;
			logerr_die(1,"Cannot exec %s to update pages field",sh);
		} else {
			/* father - Wait for the update
			 * Child exits with 0 for Ok, 2 if signal
			 * terminated and 1 otherwise.
			 */
			do{
				status = 0;
				pid = waitpid( monitpid, &status, 0 );
				err = errno;
				log(4,"doaccnt: pid  %d, status 0x%x", pid, status );
			} while( pid == -1 && err != ECHILD );
			log(1,"Accounting process exited, status 0x%x", status);
			if( status ){
				errorcode = FILTABORT;
				fatal("Accounting process died, status 0x%x", status);
			}
		}
	}
}