root/src/job.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. job_add
  2. job_runqueue

   1 /* Copyright 1988,1990,1993,1994 by Paul Vixie
   2  * All rights reserved
   3  */
   4 
   5 /*
   6  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
   7  * Copyright (c) 1997,2000 by Internet Software Consortium, Inc.
   8  *
   9  * Permission to use, copy, modify, and distribute this software for any
  10  * purpose with or without fee is hereby granted, provided that the above
  11  * copyright notice and this permission notice appear in all copies.
  12  *
  13  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
  14  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  15  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
  16  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  17  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  18  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  19  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  20  */
  21 
  22 #include "config.h"
  23 
  24 #include <stdlib.h>
  25 #include <pwd.h>
  26 #include <errno.h>
  27 #include <sys/types.h>
  28 #include <unistd.h>
  29 #include <string.h>
  30 
  31 #include "funcs.h"
  32 #include "globals.h"
  33 
  34 typedef struct _job {
  35         struct _job *next;
  36         entry *e;
  37         user *u;
  38 } job;
  39 
  40 static job *jhead = NULL, *jtail = NULL;
  41 
  42 void job_add(entry * e, user * u) {
     /* [previous][next][first][last][top][bottom][index][help]  */
  43         job *j;
  44         struct passwd *newpwd;
  45         struct passwd *temppwd;
  46         const char *uname;
  47 
  48         /* if already on queue, keep going */
  49         for (j = jhead; j != NULL; j = j->next)
  50                 if (j->e == e && j->u == u)
  51                         return;
  52 
  53         uname = e->pwd->pw_name;
  54         /* check if user exists in time of job is being run f.e. ldap */
  55         if ((temppwd = getpwnam(uname)) != NULL) {
  56                 char **tenvp;
  57 
  58                 Debug(DSCH | DEXT, ("user [%s:%ld:%ld:...] cmd=\"%s\"\n",
  59                                 e->pwd->pw_name, (long) temppwd->pw_uid,
  60                                 (long) temppwd->pw_gid, e->cmd));
  61                 if ((newpwd = pw_dup(temppwd)) == NULL) {
  62                         log_it(uname, getpid(), "ERROR", "memory allocation failed", errno);
  63                         return;
  64                 }
  65                 free(e->pwd);
  66                 e->pwd = newpwd;
  67 
  68                 if ((tenvp = env_update_home(e->envp, e->pwd->pw_dir)) == NULL) {
  69                         log_it(uname, getpid(), "ERROR", "memory allocation failed", errno);
  70                         return;
  71                 }
  72                 e->envp = tenvp;
  73         } else {
  74                 log_it(uname, getpid(), "ERROR", "getpwnam() failed - user unknown",errno);
  75                 Debug(DSCH | DEXT, ("%s:%d pid=%d time=%ld getpwnam(%s) failed errno=%d error=%s\n",
  76                         __FILE__,__LINE__,getpid(),time(NULL),uname,errno,strerror(errno)));
  77                 return;
  78         }
  79 
  80         /* build a job queue element */
  81         if ((j = (job *) malloc(sizeof (job))) == NULL)
  82                 return;
  83         j->next = NULL;
  84         j->e = e;
  85         j->u = u;
  86 
  87         /* add it to the tail */
  88         if (jhead == NULL)
  89                 jhead = j;
  90         else
  91                 jtail->next = j;
  92         jtail = j;
  93 }
  94 
  95 int job_runqueue(void) {
     /* [previous][next][first][last][top][bottom][index][help]  */
  96         job *j, *jn;
  97         int run = 0;
  98 
  99         for (j = jhead; j; j = jn) {
 100                 do_command(j->e, j->u);
 101                 jn = j->next;
 102                 free(j);
 103                 run++;
 104         }
 105         jhead = jtail = NULL;
 106         return (run);
 107 }

/* [previous][next][first][last][top][bottom][index][help]  */