1*5e7646d2SAndroid Build Coastguard Worker /* 2*5e7646d2SAndroid Build Coastguard Worker * Server start/stop routines for the CUPS scheduler. 3*5e7646d2SAndroid Build Coastguard Worker * 4*5e7646d2SAndroid Build Coastguard Worker * Copyright 2007-2019 by Apple Inc. 5*5e7646d2SAndroid Build Coastguard Worker * Copyright 1997-2006 by Easy Software Products, all rights reserved. 6*5e7646d2SAndroid Build Coastguard Worker * 7*5e7646d2SAndroid Build Coastguard Worker * Licensed under Apache License v2.0. See the file "LICENSE" for more information. 8*5e7646d2SAndroid Build Coastguard Worker */ 9*5e7646d2SAndroid Build Coastguard Worker 10*5e7646d2SAndroid Build Coastguard Worker /* 11*5e7646d2SAndroid Build Coastguard Worker * Include necessary headers... 12*5e7646d2SAndroid Build Coastguard Worker */ 13*5e7646d2SAndroid Build Coastguard Worker 14*5e7646d2SAndroid Build Coastguard Worker #include <cups/http-private.h> 15*5e7646d2SAndroid Build Coastguard Worker #include "cupsd.h" 16*5e7646d2SAndroid Build Coastguard Worker #include <grp.h> 17*5e7646d2SAndroid Build Coastguard Worker #ifdef HAVE_NOTIFY_H 18*5e7646d2SAndroid Build Coastguard Worker # include <notify.h> 19*5e7646d2SAndroid Build Coastguard Worker #endif /* HAVE_NOTIFY_H */ 20*5e7646d2SAndroid Build Coastguard Worker 21*5e7646d2SAndroid Build Coastguard Worker 22*5e7646d2SAndroid Build Coastguard Worker /* 23*5e7646d2SAndroid Build Coastguard Worker * Local globals... 24*5e7646d2SAndroid Build Coastguard Worker */ 25*5e7646d2SAndroid Build Coastguard Worker 26*5e7646d2SAndroid Build Coastguard Worker static int started = 0; /* Did we start the server already? */ 27*5e7646d2SAndroid Build Coastguard Worker 28*5e7646d2SAndroid Build Coastguard Worker 29*5e7646d2SAndroid Build Coastguard Worker /* 30*5e7646d2SAndroid Build Coastguard Worker * 'cupsdStartServer()' - Start the server. 31*5e7646d2SAndroid Build Coastguard Worker */ 32*5e7646d2SAndroid Build Coastguard Worker 33*5e7646d2SAndroid Build Coastguard Worker void cupsdStartServer(void)34*5e7646d2SAndroid Build Coastguard WorkercupsdStartServer(void) 35*5e7646d2SAndroid Build Coastguard Worker { 36*5e7646d2SAndroid Build Coastguard Worker /* 37*5e7646d2SAndroid Build Coastguard Worker * Create the default security profile... 38*5e7646d2SAndroid Build Coastguard Worker */ 39*5e7646d2SAndroid Build Coastguard Worker 40*5e7646d2SAndroid Build Coastguard Worker DefaultProfile = cupsdCreateProfile(0, 1); 41*5e7646d2SAndroid Build Coastguard Worker 42*5e7646d2SAndroid Build Coastguard Worker #ifdef HAVE_SANDBOX_H 43*5e7646d2SAndroid Build Coastguard Worker if (!DefaultProfile && UseSandboxing && Sandboxing != CUPSD_SANDBOXING_OFF) 44*5e7646d2SAndroid Build Coastguard Worker { 45*5e7646d2SAndroid Build Coastguard Worker /* 46*5e7646d2SAndroid Build Coastguard Worker * Failure to create the sandbox profile means something really bad has 47*5e7646d2SAndroid Build Coastguard Worker * happened and we need to shutdown immediately. 48*5e7646d2SAndroid Build Coastguard Worker */ 49*5e7646d2SAndroid Build Coastguard Worker 50*5e7646d2SAndroid Build Coastguard Worker return; 51*5e7646d2SAndroid Build Coastguard Worker } 52*5e7646d2SAndroid Build Coastguard Worker #endif /* HAVE_SANDBOX_H */ 53*5e7646d2SAndroid Build Coastguard Worker 54*5e7646d2SAndroid Build Coastguard Worker /* 55*5e7646d2SAndroid Build Coastguard Worker * Start color management (as needed)... 56*5e7646d2SAndroid Build Coastguard Worker */ 57*5e7646d2SAndroid Build Coastguard Worker 58*5e7646d2SAndroid Build Coastguard Worker cupsdStartColor(); 59*5e7646d2SAndroid Build Coastguard Worker 60*5e7646d2SAndroid Build Coastguard Worker /* 61*5e7646d2SAndroid Build Coastguard Worker * Startup all the networking stuff... 62*5e7646d2SAndroid Build Coastguard Worker */ 63*5e7646d2SAndroid Build Coastguard Worker 64*5e7646d2SAndroid Build Coastguard Worker cupsdStartListening(); 65*5e7646d2SAndroid Build Coastguard Worker cupsdStartBrowsing(); 66*5e7646d2SAndroid Build Coastguard Worker 67*5e7646d2SAndroid Build Coastguard Worker /* 68*5e7646d2SAndroid Build Coastguard Worker * Create a pipe for CGI processes... 69*5e7646d2SAndroid Build Coastguard Worker */ 70*5e7646d2SAndroid Build Coastguard Worker 71*5e7646d2SAndroid Build Coastguard Worker if (cupsdOpenPipe(CGIPipes)) 72*5e7646d2SAndroid Build Coastguard Worker cupsdLogMessage(CUPSD_LOG_ERROR, 73*5e7646d2SAndroid Build Coastguard Worker "cupsdStartServer: Unable to create pipes for CGI status!"); 74*5e7646d2SAndroid Build Coastguard Worker else 75*5e7646d2SAndroid Build Coastguard Worker { 76*5e7646d2SAndroid Build Coastguard Worker CGIStatusBuffer = cupsdStatBufNew(CGIPipes[0], "[CGI]"); 77*5e7646d2SAndroid Build Coastguard Worker 78*5e7646d2SAndroid Build Coastguard Worker cupsdAddSelect(CGIPipes[0], (cupsd_selfunc_t)cupsdUpdateCGI, NULL, NULL); 79*5e7646d2SAndroid Build Coastguard Worker } 80*5e7646d2SAndroid Build Coastguard Worker 81*5e7646d2SAndroid Build Coastguard Worker /* 82*5e7646d2SAndroid Build Coastguard Worker * Mark that the server has started and printers and jobs may be changed... 83*5e7646d2SAndroid Build Coastguard Worker */ 84*5e7646d2SAndroid Build Coastguard Worker 85*5e7646d2SAndroid Build Coastguard Worker LastEvent = CUPSD_EVENT_PRINTER_CHANGED | CUPSD_EVENT_JOB_STATE_CHANGED | 86*5e7646d2SAndroid Build Coastguard Worker CUPSD_EVENT_SERVER_STARTED; 87*5e7646d2SAndroid Build Coastguard Worker started = 1; 88*5e7646d2SAndroid Build Coastguard Worker 89*5e7646d2SAndroid Build Coastguard Worker cupsdSetBusyState(0); 90*5e7646d2SAndroid Build Coastguard Worker } 91*5e7646d2SAndroid Build Coastguard Worker 92*5e7646d2SAndroid Build Coastguard Worker 93*5e7646d2SAndroid Build Coastguard Worker /* 94*5e7646d2SAndroid Build Coastguard Worker * 'cupsdStopServer()' - Stop the server. 95*5e7646d2SAndroid Build Coastguard Worker */ 96*5e7646d2SAndroid Build Coastguard Worker 97*5e7646d2SAndroid Build Coastguard Worker void cupsdStopServer(void)98*5e7646d2SAndroid Build Coastguard WorkercupsdStopServer(void) 99*5e7646d2SAndroid Build Coastguard Worker { 100*5e7646d2SAndroid Build Coastguard Worker if (!started) 101*5e7646d2SAndroid Build Coastguard Worker return; 102*5e7646d2SAndroid Build Coastguard Worker 103*5e7646d2SAndroid Build Coastguard Worker /* 104*5e7646d2SAndroid Build Coastguard Worker * Stop color management (as needed)... 105*5e7646d2SAndroid Build Coastguard Worker */ 106*5e7646d2SAndroid Build Coastguard Worker 107*5e7646d2SAndroid Build Coastguard Worker cupsdStopColor(); 108*5e7646d2SAndroid Build Coastguard Worker 109*5e7646d2SAndroid Build Coastguard Worker /* 110*5e7646d2SAndroid Build Coastguard Worker * Close all network clients... 111*5e7646d2SAndroid Build Coastguard Worker */ 112*5e7646d2SAndroid Build Coastguard Worker 113*5e7646d2SAndroid Build Coastguard Worker cupsdCloseAllClients(); 114*5e7646d2SAndroid Build Coastguard Worker cupsdStopListening(); 115*5e7646d2SAndroid Build Coastguard Worker cupsdStopBrowsing(); 116*5e7646d2SAndroid Build Coastguard Worker cupsdStopAllNotifiers(); 117*5e7646d2SAndroid Build Coastguard Worker cupsdDeleteAllCerts(); 118*5e7646d2SAndroid Build Coastguard Worker 119*5e7646d2SAndroid Build Coastguard Worker if (Clients) 120*5e7646d2SAndroid Build Coastguard Worker { 121*5e7646d2SAndroid Build Coastguard Worker cupsArrayDelete(Clients); 122*5e7646d2SAndroid Build Coastguard Worker Clients = NULL; 123*5e7646d2SAndroid Build Coastguard Worker } 124*5e7646d2SAndroid Build Coastguard Worker 125*5e7646d2SAndroid Build Coastguard Worker /* 126*5e7646d2SAndroid Build Coastguard Worker * Close the pipe for CGI processes... 127*5e7646d2SAndroid Build Coastguard Worker */ 128*5e7646d2SAndroid Build Coastguard Worker 129*5e7646d2SAndroid Build Coastguard Worker if (CGIPipes[0] >= 0) 130*5e7646d2SAndroid Build Coastguard Worker { 131*5e7646d2SAndroid Build Coastguard Worker cupsdRemoveSelect(CGIPipes[0]); 132*5e7646d2SAndroid Build Coastguard Worker 133*5e7646d2SAndroid Build Coastguard Worker cupsdStatBufDelete(CGIStatusBuffer); 134*5e7646d2SAndroid Build Coastguard Worker close(CGIPipes[1]); 135*5e7646d2SAndroid Build Coastguard Worker 136*5e7646d2SAndroid Build Coastguard Worker CGIPipes[0] = -1; 137*5e7646d2SAndroid Build Coastguard Worker CGIPipes[1] = -1; 138*5e7646d2SAndroid Build Coastguard Worker } 139*5e7646d2SAndroid Build Coastguard Worker 140*5e7646d2SAndroid Build Coastguard Worker /* 141*5e7646d2SAndroid Build Coastguard Worker * Close all log files... 142*5e7646d2SAndroid Build Coastguard Worker */ 143*5e7646d2SAndroid Build Coastguard Worker 144*5e7646d2SAndroid Build Coastguard Worker if (AccessFile != NULL) 145*5e7646d2SAndroid Build Coastguard Worker { 146*5e7646d2SAndroid Build Coastguard Worker if (AccessFile != LogStderr) 147*5e7646d2SAndroid Build Coastguard Worker cupsFileClose(AccessFile); 148*5e7646d2SAndroid Build Coastguard Worker 149*5e7646d2SAndroid Build Coastguard Worker AccessFile = NULL; 150*5e7646d2SAndroid Build Coastguard Worker } 151*5e7646d2SAndroid Build Coastguard Worker 152*5e7646d2SAndroid Build Coastguard Worker if (ErrorFile != NULL) 153*5e7646d2SAndroid Build Coastguard Worker { 154*5e7646d2SAndroid Build Coastguard Worker if (ErrorFile != LogStderr) 155*5e7646d2SAndroid Build Coastguard Worker cupsFileClose(ErrorFile); 156*5e7646d2SAndroid Build Coastguard Worker 157*5e7646d2SAndroid Build Coastguard Worker ErrorFile = NULL; 158*5e7646d2SAndroid Build Coastguard Worker } 159*5e7646d2SAndroid Build Coastguard Worker 160*5e7646d2SAndroid Build Coastguard Worker if (PageFile != NULL) 161*5e7646d2SAndroid Build Coastguard Worker { 162*5e7646d2SAndroid Build Coastguard Worker if (PageFile != LogStderr) 163*5e7646d2SAndroid Build Coastguard Worker cupsFileClose(PageFile); 164*5e7646d2SAndroid Build Coastguard Worker 165*5e7646d2SAndroid Build Coastguard Worker PageFile = NULL; 166*5e7646d2SAndroid Build Coastguard Worker } 167*5e7646d2SAndroid Build Coastguard Worker 168*5e7646d2SAndroid Build Coastguard Worker /* 169*5e7646d2SAndroid Build Coastguard Worker * Delete the default security profile... 170*5e7646d2SAndroid Build Coastguard Worker */ 171*5e7646d2SAndroid Build Coastguard Worker 172*5e7646d2SAndroid Build Coastguard Worker cupsdDestroyProfile(DefaultProfile); 173*5e7646d2SAndroid Build Coastguard Worker DefaultProfile = NULL; 174*5e7646d2SAndroid Build Coastguard Worker 175*5e7646d2SAndroid Build Coastguard Worker /* 176*5e7646d2SAndroid Build Coastguard Worker * Expire subscriptions and clean out old jobs... 177*5e7646d2SAndroid Build Coastguard Worker */ 178*5e7646d2SAndroid Build Coastguard Worker 179*5e7646d2SAndroid Build Coastguard Worker cupsdExpireSubscriptions(NULL, NULL); 180*5e7646d2SAndroid Build Coastguard Worker 181*5e7646d2SAndroid Build Coastguard Worker if (JobHistoryUpdate) 182*5e7646d2SAndroid Build Coastguard Worker cupsdCleanJobs(); 183*5e7646d2SAndroid Build Coastguard Worker 184*5e7646d2SAndroid Build Coastguard Worker /* 185*5e7646d2SAndroid Build Coastguard Worker * Write out any dirty files... 186*5e7646d2SAndroid Build Coastguard Worker */ 187*5e7646d2SAndroid Build Coastguard Worker 188*5e7646d2SAndroid Build Coastguard Worker if (DirtyFiles) 189*5e7646d2SAndroid Build Coastguard Worker cupsdCleanDirty(); 190*5e7646d2SAndroid Build Coastguard Worker 191*5e7646d2SAndroid Build Coastguard Worker started = 0; 192*5e7646d2SAndroid Build Coastguard Worker } 193