Annotation of coherent/d/etc/SYSVcron/src/entry.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * entry.c
                      3:  *
                      4:  * Keep track about all children processes.
                      5:  *
                      6:  */
                      7: #include       <stdio.h>
                      8: #include       <string.h>
                      9: #include       "cron.h"
                     10: 
                     11: #define        HUGE    1024            /* Size of the hostname+domain, is it enough */
                     12: 
                     13: extern char    *mktemp();      /* Generate temporary name */
                     14: extern int     gethostname();  /* Get host name */
                     15: extern int     getdomain();    /* Get domain name */
                     16: 
                     17: child_id       *add_entry(),   /* Add an new entry to a link list */
                     18:                *find_entry(),  /* Find an entry in the link list */
                     19:                *del_entry();   /* Remove an entry from the link list */
                     20: void           mail_entry();   /* Send mail to user if command failed */
                     21: 
                     22: extern child_id        *current;       /* Pointer to the lasr allocated struct. */
                     23: 
                     24: /*
                     25:  * Add a new entry to the link list. Return pointer to the new entry or
                     26:  * NULL if entry cannot be allocated.
                     27:  */
                     28: child_id *add_entry(pid, u_name, c_time, cmd)
                     29: int    pid;            /* Process ID */
                     30: char   *u_name;        /* User name */
                     31: time_t c_time;         /* Time when command started */
                     32: char   *cmd;           /* Command */
                     33: {
                     34:        register child_id       *pst;
                     35: 
                     36:        /* Structure child_id has one extracharacter at the end */
                     37:        if ((pst = (child_id *) malloc(sizeof(child_id) + strlen(cmd))) 
                     38:                                                                == NULL) {
                     39:                fprintf(stderr, "crond: out of memory\n");
                     40:        } else {        /* Write information about child to a table */
                     41:                pst->prev = current;
                     42:                if (current != NULL)
                     43:                        current->next = pst;
                     44:                current = pst;
                     45:                current->next = NULL;
                     46:                current->pid = pid;
                     47:                strncpy(current->name, u_name, MAX_UNAME - 1);
                     48:                current->time = c_time;
                     49:                strcpy(current->command, cmd);
                     50:                Dprint("add_entry: added ID is %d, ", current->pid);
                     51:        }
                     52:        return current;
                     53: }
                     54: 
                     55: /*
                     56:  * Find an entry by process id. Return pointer to the entry or
                     57:  * NULL if entry not found.
                     58:  */
                     59: child_id *find_entry(pid)
                     60: int    pid;
                     61: {
                     62:        register child_id       *pst;
                     63: 
                     64:        Dprint("find_entry: request ID is %d. List of incore pids:\n", pid);
                     65:        for (pst = current; pst != NULL; pst = pst->prev) {
                     66:                Dprint(" %d ", pst->pid);
                     67:                if (pst->pid == pid)
                     68:                        break;
                     69:        }
                     70:        Dprint("find_entry: found ID is %d\n", pst->pid);
                     71:        return pst;
                     72: }
                     73: 
                     74: /*
                     75:  * Delete entry. Return pointer to the last allocated entry ("current").
                     76:  */
                     77: child_id *del_entry(corpse)
                     78: child_id       *corpse;        /* Entry that should be deleted */
                     79: {
                     80:        child_id        *pst;
                     81: 
                     82:        Dprint("del_entry: ID should be %d\t", corpse->pid);
                     83:        Dprint("next_id is %d\t", corpse->next->pid);
                     84:        Dprint("prev_id is %d\n", corpse->prev->pid);
                     85: 
                     86:        if (corpse == NULL)
                     87:                return current;
                     88: 
                     89:        if ((pst = corpse->prev) != NULL) {
                     90:                pst->next = corpse->next;
                     91:                Dprint("del_entry: prev_id is %d\n", pst->pid);
                     92:        }
                     93: 
                     94:        if ((pst = corpse->next) != NULL) {
                     95:                pst->prev = corpse->prev;
                     96:                Dprint("del_entry: next_id is %d\n", pst->pid);
                     97:        }
                     98: 
                     99:        if (corpse->next == NULL) {
                    100:                pst = corpse->prev;
                    101:                free(corpse);
                    102:                return pst;
                    103:        }
                    104:        free(corpse);
                    105:        return current;
                    106: }      
                    107: 
                    108: char   tmpFoo[] = "/tmp/cronXXXXXX";   /* Temporary file */
                    109: 
                    110: /*
                    111:  * Mail entry. Send mail to a user about failed command.
                    112:  */
                    113: void mail_entry(entry)
                    114: child_id       *entry;
                    115: {
                    116:        char    mailBuf[MAX_STR_LEN];   /* Message buffer */
                    117:        char    tmpName[20];            /* Name of the temporary file */
                    118:        char    tmpBuf[HUGE];           /* Huge buffer for hostname */
                    119:        FILE    *fp;                    /* Pointer to a temporary file */
                    120:        int     sz;                     /* Size of the Hostname + '.' */
                    121: 
                    122:        *tmpBuf = 0;    /* Just to have */
                    123: 
                    124:        strcpy(tmpName, mktemp(tmpFoo));
                    125:        if ((fp = fopen(tmpName, "w+")) == NULL) {
                    126:                fprintf(stderr, "crond: cannot open temporary file %s.\n",
                    127:                                                        tmpName);
                    128:                return;
                    129:        }
                    130: 
                    131:        if (gethostname(tmpBuf, HUGE))
                    132:                strcat(tmpBuf, "");
                    133:        else {
                    134:                strcat(tmpBuf, ".");
                    135:                sz = strlen(tmpBuf);
                    136:                if (getdomain(&tmpBuf[sz], HUGE - sz));
                    137:                        strcat(tmpBuf, "");
                    138:        }
                    139:        fprintf(fp, "From: CRON@%s\n", tmpBuf);
                    140:        fprintf(fp, "Subject: failed command\n\n");
                    141:        fprintf(fp, "Time was %sCommand was \"%s\".\n\n", 
                    142:                        ctime(&(entry->time)), entry->command);
                    143:        fprintf(fp, "Have a good one cron!\n");
                    144:        rewind(fp);
                    145:        sprintf(mailBuf, "smail %s < %s", entry->name, tmpName);
                    146:        system(mailBuf);
                    147:        fclose(fp);
                    148:        unlink(tmpName);
                    149: }      
                    150: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.