[cups] Custom backend with fork()

Justin Killen jkillen at allamericanasphalt.com
Wed Apr 29 11:43:46 PDT 2015


Michael,

I tried calling setsid() on the child process and it creates a new session successfully, but I still get the same behavior (I tried with setpgid(0,0) as well).  Following is a condensed version of the code that illustrates the problem.  If you set a custom backend to use this code and queue up several jobs, if you tail the message log you'll notice that the intro log ("progbase is") only happens after the child finishes spinning down.

--BEGIN CODE--

 #include <libgen.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <syslog.h>

int main(int argc, char **argv)
{
  char * progbase = basename(argv[0]);

  setlogmask (LOG_UPTO (LOG_DEBUG));
  openlog ("", LOG_PID | LOG_NDELAY, LOG_LOCAL2);
  syslog (LOG_INFO, "progbase is %s", progbase);

  if(argc == 1)
  {
    printf("direct %s \"Unknown\" \"%s\"\n", progbase, progbase);
    exit(0);
  }

 if(argc < 5 || argc > 6){
    fprintf(stderr, "ERROR: Usage: %s job-id user title copies options [file]\n", progbase);
    fprintf(stderr, "argc: %d\n", argc);
    exit(1);
  }

  setuid(0);

  syslog (LOG_INFO, "forking...");
  pid_t pid = fork();

  syslog (LOG_INFO, "PID is: %d", pid);

  if(pid == 0)
  {
    // dis-accosiate the child from the session and process group...
    pid_t setSid = setsid();
    syslog(LOG_INFO, "setSid: %d", setSid);
    /*
    pid_t pgid = setpgid(0, 0);
    syslog(LOG_INFO, "pgid: %d", pgid);
    */

    int i;
    for(i=10; i>=0; i--)
    {
      syslog(LOG_INFO, "spinning %d", i);
      sleep(10);
    }
    //execvp("/usr/local/bin/4glprintqueue", argv);
  }
  else
  {
    //sleep(2);
    syslog(LOG_INFO, "parent is exiting...");
    exit(0);
  }
  closelog ();
}

--END CODE--

-Justin

-----Original Message-----
From: cups-bounces at cups.org [mailto:cups-bounces at cups.org] On Behalf Of Justin Killen
Sent: Wednesday, April 29, 2015 10:04 AM
To: The CUPS user discussion list.
Subject: Re: [cups] Custom backend with fork()

Thanks Michael - I think that is exactly what I'm looking for.

To address some of the other responses, I feel I should clarify - in this situation, there is no physical printer device.  Lpr is simply used as a transport to get a file to the server and run a program against that file - more like an inetd type service.

-Justin


-----Original Message-----
From: cups-bounces at cups.org [mailto:cups-bounces at cups.org] On Behalf Of Michael Sweet
Sent: Wednesday, April 29, 2015 5:52 AM
To: The CUPS user discussion list.
Subject: Re: [cups] Custom backend with fork()

Justin,

You'll need to separate the child from the process group, otherwise the child will hold up the parent from exiting.

"man setpgid" and "man setsid"...


> On Apr 28, 2015, at 4:01 PM, Justin Killen <jkillen at allamericanasphalt.com> wrote:
> 
> Hi,
> 
> I'm trying to emulate a system that passes job control via lpr/lpd.  I've got the basics worked out, but I'm getting held up by jobs only running one at a time.  Ideally, what should happen is that if 10 jobs are queued, they should all run in parallel.  In the legacy system (an AIX box), it was accomplished by the backend script fork()ing, and the parent returning, which caused the print queue entry to be 'complete' and it would go to the next one.  I tried this strategy with CUPS, but it doesn't work -the backend script forks and the parent returns, but CUPS still waits for the child PID to complete before moving on to the next item on the queue.  Perhaps CUPS is monitoring the entire process tree?  Anyway, any ideas or thoughts would be welcomed.
> 
> Thanks,
> -Justin
> 
> _______________________________________________
> cups mailing list
> cups at cups.org
> https://www.cups.org/mailman/listinfo/cups

_________________________________________________________
Michael Sweet, Senior Printing System Engineer, PWG Chair

_______________________________________________
cups mailing list
cups at cups.org
https://www.cups.org/mailman/listinfo/cups
_______________________________________________
cups mailing list
cups at cups.org
https://www.cups.org/mailman/listinfo/cups



More information about the cups mailing list