Home | History | Annotate | Download | only in lpsched
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License, Version 1.0 only
      6  * (the "License").  You may not use this file except in compliance
      7  * with the License.
      8  *
      9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
     10  * or http://www.opensolaris.org/os/licensing.
     11  * See the License for the specific language governing permissions
     12  * and limitations under the License.
     13  *
     14  * When distributing Covered Code, include this CDDL HEADER in each
     15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     16  * If applicable, add the following below this CDDL HEADER, with the
     17  * fields enclosed by brackets "[]" replaced with your own identifying
     18  * information: Portions Copyright [yyyy] [name of copyright owner]
     19  *
     20  * CDDL HEADER END
     21  */
     22 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
     23 /*	  All Rights Reserved  	*/
     24 
     25 
     26 /*
     27  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
     28  * Use is subject to license terms.
     29  */
     30 
     31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
     32 
     33 # include	<stdarg.h>
     34 # include	"lpsched.h"
     35 
     36 typedef struct fault	FLT;
     37 
     38 struct fault
     39 {
     40     FLT *	next;
     41     int		type;
     42     int		i1;
     43     char *	s1;
     44     RSTATUS *	r1;
     45     MESG *	ident;
     46 };
     47 
     48 static void free_flt ( FLT * );
     49 static void do_flt_acts ( MESG * );
     50 
     51 static FLT	Fault_Head = { NULL, 0, 0, NULL, NULL, NULL };
     52 static FLT *	Fault_List = &Fault_Head;
     53 
     54 void
     55 add_flt_act(MESG * md, ...)
     56 {
     57     va_list	arg;
     58     FLT		*f;
     59 
     60     va_start (arg, md);
     61 
     62     f = (FLT *)Malloc(sizeof(FLT));
     63 
     64     (void) memset((char *)f, 0, sizeof(FLT));
     65 
     66     f->type = (int)va_arg(arg, int);
     67     f->ident = md;
     68 
     69     if (md->on_discon == NULL)
     70 	if (mon_discon(md, do_flt_acts))
     71 	    mallocfail();
     72 
     73     switch(f->type)
     74     {
     75 	case FLT_FILES:
     76 	f->s1 = Strdup((char *)va_arg(arg, char *));
     77 	f->i1 = (int)va_arg(arg, int);
     78 	break;
     79 
     80 	case FLT_CHANGE:
     81 	f->r1 = (RSTATUS *)va_arg(arg, RSTATUS *);
     82 	break;
     83     }
     84 
     85     va_end(arg);
     86 
     87     f->next = Fault_List->next;
     88     Fault_List->next = f;
     89 }
     90 
     91 
     92 void
     93 del_flt_act(MESG *md, ...)
     94 {
     95     va_list	arg;
     96     int		type;
     97     FLT		*fp;
     98     FLT		*f;
     99 
    100     va_start(arg, md);
    101 
    102     type = (int)va_arg(arg, int);
    103 
    104     for (f = Fault_List; f->next; f = f->next)
    105 	if (f->next->type == type && f->next->ident == md)
    106 	{
    107 	    fp = f->next;
    108 	    f->next = f->next->next;
    109 	    free_flt(fp);
    110 	    break;
    111 	}
    112 
    113     va_end(arg);
    114 }
    115 
    116 static void
    117 do_flt_acts(MESG *md)
    118 {
    119     FLT		*f;
    120     FLT		*fp;
    121     char	*file;
    122     char	id[15];
    123 #ifdef LP_USE_PAPI_ATTR
    124     struct stat	tmpBuf;
    125     char	attrFile[BUFSIZ];
    126 #endif
    127 
    128     for (f = Fault_List; f && f->next; f = f->next)
    129 	if (f->next->ident == md)
    130 	{
    131 	    fp = f->next;
    132 	    f->next = f->next->next;
    133 
    134 	    switch (fp->type)
    135 	    {
    136 		case FLT_FILES:
    137 		/* remove files created with alloc_files */
    138 
    139 		while(fp->i1--)
    140 		{
    141 		    (void) snprintf(id, sizeof (id), "%s-%d", fp->s1, fp->i1);
    142 		    file = makepath(Lp_Temp, id, (char *)0);
    143 		    (void) Unlink(file);
    144 		    Free(file);
    145 		}
    146 
    147 #ifdef LP_USE_PAPI_ATTR
    148 		/*
    149 		 * check if the PAPI attribute file exists, if it does delete it
    150 		 */
    151 		(void) snprintf(attrFile, sizeof (attrFile),
    152 				"%s-%s", fp->s1, LP_PAPIATTRNAME);
    153 		file = makepath(Lp_Temp, attrFile, (char *)0);
    154 		if ((file != NULL) && (stat(file, &tmpBuf) == 0))
    155 		{
    156 			(void) Unlink(file);
    157 		}
    158 		Free(file);
    159 #endif
    160 		break;
    161 
    162 
    163 		case FLT_CHANGE:
    164 		/* clear RS_CHANGE bit, write request file, and schedule */
    165 		fp->r1->request->outcome &= ~RS_CHANGING;
    166 		putrequest(fp->r1->req_file, fp->r1->request);
    167 		if (NEEDS_FILTERING(fp->r1))
    168 		    schedule(/* LP_FILTER */ EV_SLOWF, fp->r1);
    169 		else
    170 		    schedule(/* LP_PRINTER */ EV_INTERF, fp->r1->printer);
    171 		break;
    172 	    }
    173 	    free_flt(fp);
    174 	}
    175 }
    176 
    177 static void
    178 free_flt(FLT *f)
    179 {
    180     if (f->s1)
    181 	Free(f->s1);
    182     Free((char *)f);
    183 }
    184