Blame scheduler/server.c

Packit 2fc92b
/*
Packit 2fc92b
 * Server start/stop routines for the CUPS scheduler.
Packit 2fc92b
 *
Packit 2fc92b
 * Copyright 2007-2017 by Apple Inc.
Packit 2fc92b
 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
Packit 2fc92b
 *
Packit 2fc92b
 * These coded instructions, statements, and computer programs are the
Packit 2fc92b
 * property of Apple Inc. and are protected by Federal copyright
Packit 2fc92b
 * law.  Distribution and use rights are outlined in the file "LICENSE.txt"
Packit 2fc92b
 * which should have been included with this file.  If this file is
Packit 2fc92b
 * missing or damaged, see the license at "http://www.cups.org/".
Packit 2fc92b
 */
Packit 2fc92b
Packit 2fc92b
/*
Packit 2fc92b
 * Include necessary headers...
Packit 2fc92b
 */
Packit 2fc92b
Packit 2fc92b
#include <cups/http-private.h>
Packit 2fc92b
#include "cupsd.h"
Packit 2fc92b
#include <grp.h>
Packit 2fc92b
#ifdef HAVE_NOTIFY_H
Packit 2fc92b
#  include <notify.h>
Packit 2fc92b
#endif /* HAVE_NOTIFY_H */
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
/*
Packit 2fc92b
 * Local globals...
Packit 2fc92b
 */
Packit 2fc92b
Packit 2fc92b
static int		started = 0;	/* Did we start the server already? */
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
/*
Packit 2fc92b
 * 'cupsdStartServer()' - Start the server.
Packit 2fc92b
 */
Packit 2fc92b
Packit 2fc92b
void
Packit 2fc92b
cupsdStartServer(void)
Packit 2fc92b
{
Packit 2fc92b
 /*
Packit Service 70a099
  * Create the default security profile...
Packit 2fc92b
  */
Packit 2fc92b
Packit Service 70a099
  DefaultProfile = cupsdCreateProfile(0, 1);
Packit Service 70a099
Packit Service 70a099
#ifdef HAVE_SANDBOX_H
Packit Service 70a099
  if (!DefaultProfile && UseSandboxing && Sandboxing != CUPSD_SANDBOXING_OFF)
Packit Service 70a099
  {
Packit Service 70a099
   /*
Packit Service 70a099
    * Failure to create the sandbox profile means something really bad has
Packit Service 70a099
    * happened and we need to shutdown immediately.
Packit Service 70a099
    */
Packit Service 70a099
Packit Service 70a099
    return;
Packit Service 70a099
  }
Packit Service 70a099
#endif /* HAVE_SANDBOX_H */
Packit 2fc92b
Packit 2fc92b
 /*
Packit Service 70a099
  * Start color management (as needed)...
Packit 2fc92b
  */
Packit 2fc92b
Packit Service 70a099
  cupsdStartColor();
Packit 2fc92b
Packit 2fc92b
 /*
Packit 2fc92b
  * Startup all the networking stuff...
Packit 2fc92b
  */
Packit 2fc92b
Packit 2fc92b
  cupsdStartListening();
Packit 2fc92b
  cupsdStartBrowsing();
Packit 2fc92b
Packit 2fc92b
 /*
Packit 2fc92b
  * Create a pipe for CGI processes...
Packit 2fc92b
  */
Packit 2fc92b
Packit 2fc92b
  if (cupsdOpenPipe(CGIPipes))
Packit 2fc92b
    cupsdLogMessage(CUPSD_LOG_ERROR,
Packit 2fc92b
                    "cupsdStartServer: Unable to create pipes for CGI status!");
Packit 2fc92b
  else
Packit 2fc92b
  {
Packit 2fc92b
    CGIStatusBuffer = cupsdStatBufNew(CGIPipes[0], "[CGI]");
Packit 2fc92b
Packit 2fc92b
    cupsdAddSelect(CGIPipes[0], (cupsd_selfunc_t)cupsdUpdateCGI, NULL, NULL);
Packit 2fc92b
  }
Packit 2fc92b
Packit 2fc92b
 /*
Packit 2fc92b
  * Mark that the server has started and printers and jobs may be changed...
Packit 2fc92b
  */
Packit 2fc92b
Packit 2fc92b
  LastEvent = CUPSD_EVENT_PRINTER_CHANGED | CUPSD_EVENT_JOB_STATE_CHANGED |
Packit 2fc92b
              CUPSD_EVENT_SERVER_STARTED;
Packit 2fc92b
  started   = 1;
Packit 2fc92b
Packit 2fc92b
  cupsdSetBusyState(0);
Packit 2fc92b
}
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
/*
Packit 2fc92b
 * 'cupsdStopServer()' - Stop the server.
Packit 2fc92b
 */
Packit 2fc92b
Packit 2fc92b
void
Packit 2fc92b
cupsdStopServer(void)
Packit 2fc92b
{
Packit 2fc92b
  if (!started)
Packit 2fc92b
    return;
Packit 2fc92b
Packit 2fc92b
 /*
Packit 2fc92b
  * Stop color management (as needed)...
Packit 2fc92b
  */
Packit 2fc92b
Packit 2fc92b
  cupsdStopColor();
Packit 2fc92b
Packit 2fc92b
 /*
Packit 2fc92b
  * Close all network clients...
Packit 2fc92b
  */
Packit 2fc92b
Packit 2fc92b
  cupsdCloseAllClients();
Packit 2fc92b
  cupsdStopListening();
Packit 2fc92b
  cupsdStopBrowsing();
Packit 2fc92b
  cupsdStopAllNotifiers();
Packit 2fc92b
  cupsdDeleteAllCerts();
Packit 2fc92b
Packit 2fc92b
  if (Clients)
Packit 2fc92b
  {
Packit 2fc92b
    cupsArrayDelete(Clients);
Packit 2fc92b
    Clients = NULL;
Packit 2fc92b
  }
Packit 2fc92b
Packit 2fc92b
 /*
Packit 2fc92b
  * Close the pipe for CGI processes...
Packit 2fc92b
  */
Packit 2fc92b
Packit 2fc92b
  if (CGIPipes[0] >= 0)
Packit 2fc92b
  {
Packit 2fc92b
    cupsdRemoveSelect(CGIPipes[0]);
Packit 2fc92b
Packit 2fc92b
    cupsdStatBufDelete(CGIStatusBuffer);
Packit 2fc92b
    close(CGIPipes[1]);
Packit 2fc92b
Packit 2fc92b
    CGIPipes[0] = -1;
Packit 2fc92b
    CGIPipes[1] = -1;
Packit 2fc92b
  }
Packit 2fc92b
Packit 2fc92b
 /*
Packit 2fc92b
  * Close all log files...
Packit 2fc92b
  */
Packit 2fc92b
Packit 2fc92b
  if (AccessFile != NULL)
Packit 2fc92b
  {
Packit 2fc92b
    if (AccessFile != LogStderr)
Packit 2fc92b
      cupsFileClose(AccessFile);
Packit 2fc92b
Packit 2fc92b
    AccessFile = NULL;
Packit 2fc92b
  }
Packit 2fc92b
Packit 2fc92b
  if (ErrorFile != NULL)
Packit 2fc92b
  {
Packit 2fc92b
    if (ErrorFile != LogStderr)
Packit 2fc92b
      cupsFileClose(ErrorFile);
Packit 2fc92b
Packit 2fc92b
    ErrorFile = NULL;
Packit 2fc92b
  }
Packit 2fc92b
Packit 2fc92b
  if (PageFile != NULL)
Packit 2fc92b
  {
Packit 2fc92b
    if (PageFile != LogStderr)
Packit 2fc92b
      cupsFileClose(PageFile);
Packit 2fc92b
Packit 2fc92b
    PageFile = NULL;
Packit 2fc92b
  }
Packit 2fc92b
Packit 2fc92b
 /*
Packit 2fc92b
  * Delete the default security profile...
Packit 2fc92b
  */
Packit 2fc92b
Packit 2fc92b
  cupsdDestroyProfile(DefaultProfile);
Packit 2fc92b
  DefaultProfile = NULL;
Packit 2fc92b
Packit 2fc92b
 /*
Packit 2fc92b
  * Write out any dirty files...
Packit 2fc92b
  */
Packit 2fc92b
Packit 2fc92b
  if (DirtyFiles)
Packit 2fc92b
    cupsdCleanDirty();
Packit 2fc92b
Packit 2fc92b
  started = 0;
Packit 2fc92b
}