error.c
author viric@llimona
Fri, 13 Apr 2007 20:52:28 +0200
changeset 127 c620eacd7630
parent 117 db479ef293d3
child 146 5e689cb593aa
permissions -rw-r--r--
Fixed the versions in the code.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
102
27b0f0e44de8 Added copyright headers to the new files. I always forget that.
viric@llimona
parents: 92
diff changeset
     1
/*
27b0f0e44de8 Added copyright headers to the new files. I always forget that.
viric@llimona
parents: 92
diff changeset
     2
    Task Spooler - a task queue system for the unix user
27b0f0e44de8 Added copyright headers to the new files. I always forget that.
viric@llimona
parents: 92
diff changeset
     3
    Copyright (C) 2007  LluĂ­s Batlle i Rossell
27b0f0e44de8 Added copyright headers to the new files. I always forget that.
viric@llimona
parents: 92
diff changeset
     4
27b0f0e44de8 Added copyright headers to the new files. I always forget that.
viric@llimona
parents: 92
diff changeset
     5
    Please find the license in the provided COPYING file.
27b0f0e44de8 Added copyright headers to the new files. I always forget that.
viric@llimona
parents: 92
diff changeset
     6
*/
92
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
     7
#include <unistd.h>
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
     8
#include <stdio.h>
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
     9
#include <time.h>
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    10
#include <sys/types.h>
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    11
#include <errno.h>
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    12
#include <string.h>
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    13
#include <fcntl.h>
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    14
#include <sys/stat.h>
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    15
#include <stdarg.h>
105
98b6955472dd Fixed warning in error.c
viric@llimona
parents: 104
diff changeset
    16
#include <stdlib.h>
92
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    17
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    18
#include "main.h"
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    19
111
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
    20
enum Etype
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
    21
{
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
    22
    WARNING,
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
    23
    ERROR
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
    24
};
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
    25
92
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    26
/* Declared in main.h as extern */
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    27
enum Process_type process_type;
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    28
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    29
static int real_errno;
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    30
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    31
/* Local protos */
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    32
static void dump_structs(FILE *out);
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    33
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    34
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    35
static void print_date(FILE *out)
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    36
{
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    37
    time_t t;
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    38
    const char *tstr;
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    39
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    40
    t = time(NULL);
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    41
    tstr = ctime(&t);
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    42
104
dc4dd9939238 Fixed error reporting.
viric@llimona
parents: 102
diff changeset
    43
    fprintf(out, "date %s", tstr);
92
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    44
}
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    45
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    46
static void dump_proc_info(FILE *out)
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    47
{
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    48
    print_date(out);
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    49
    fprintf(out, "pid %i\n", getpid());
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    50
    if (process_type == SERVER)
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    51
        fprintf(out, "type SERVER\n");
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    52
    else if (process_type == CLIENT)
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    53
        fprintf(out, "type CLIENT\n");
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    54
    else
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    55
        fprintf(out, "type UNKNOWN\n");
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    56
}
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    57
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    58
static FILE * open_error_file()
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    59
{
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    60
    int fd;
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    61
    FILE* out;
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    62
104
dc4dd9939238 Fixed error reporting.
viric@llimona
parents: 102
diff changeset
    63
    fd = open("/tmp/ts.error", O_CREAT | O_APPEND | O_WRONLY, 0600);
92
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    64
    if (fd == -1)
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    65
        return 0;
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    66
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    67
    out = fdopen(fd, "a");
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    68
    if (out == NULL)
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    69
    {
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    70
        close(fd);
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    71
        return 0;
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    72
    }
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    73
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    74
    return out;
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    75
}
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    76
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    77
static void print_error(FILE *out, enum Etype type, const char *str, va_list ap)
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    78
{
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    79
    if (type == ERROR)
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    80
        fprintf(out, "Error\n");
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    81
    else if (type == WARNING)
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    82
        fprintf(out, "Warning\n");
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    83
    else
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    84
        fprintf(out, "Unknown kind of error\n");
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    85
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    86
    fprintf(out, " Msg: ");
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    87
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    88
    vfprintf(out, str, ap);
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    89
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    90
    fprintf(out, "\n");
104
dc4dd9939238 Fixed error reporting.
viric@llimona
parents: 102
diff changeset
    91
    fprintf(out, " errno %i, \"%s\"\n", real_errno, strerror(real_errno));
92
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    92
}
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    93
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    94
static void problem(enum Etype type, const char *str, va_list ap)
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    95
{
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    96
    FILE *out;
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    97
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    98
    out = open_error_file();
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
    99
    if (out == 0)
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   100
        return;
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   101
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   102
    /* out is ready */
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   103
    print_error(out, type, str, ap);
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   104
    dump_structs(out);
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   105
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   106
    /* this will close the fd also */
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   107
    fclose(out);
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   108
}
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   109
111
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   110
static void problem_msg(enum Etype type, const struct msg *m, const char *str, va_list ap)
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   111
{
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   112
    FILE *out;
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   113
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   114
    out = open_error_file();
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   115
    if (out == 0)
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   116
        return;
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   117
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   118
    /* out is ready */
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   119
    print_error(out, type, str, ap);
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   120
    msgdump(out, m);
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   121
    dump_structs(out);
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   122
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   123
    /* this will close the fd also */
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   124
    fclose(out);
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   125
}
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   126
92
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   127
void error(const char *str, ...)
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   128
{
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   129
    va_list ap;
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   130
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   131
    va_start(ap, str);
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   132
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   133
    real_errno = errno;
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   134
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   135
    problem(ERROR, str, ap);
104
dc4dd9939238 Fixed error reporting.
viric@llimona
parents: 102
diff changeset
   136
    exit(-1);
92
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   137
}
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   138
111
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   139
void error_msg(const struct msg *m, const char *str, ...)
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   140
{
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   141
    va_list ap;
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   142
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   143
    va_start(ap, str);
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   144
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   145
    real_errno = errno;
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   146
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   147
    problem_msg(ERROR, m, str, ap);
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   148
    exit(-1);
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   149
}
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   150
92
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   151
void warning(const char *str, ...)
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   152
{
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   153
    va_list ap;
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   154
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   155
    va_start(ap, str);
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   156
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   157
    real_errno = errno;
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   158
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   159
    problem(WARNING, str, ap);
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   160
}
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   161
111
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   162
void warning_msg(const struct msg *m, const char *str, ...)
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   163
{
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   164
    va_list ap;
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   165
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   166
    va_start(ap, str);
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   167
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   168
    real_errno = errno;
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   169
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   170
    problem_msg(WARNING, m, str, ap);
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   171
}
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 105
diff changeset
   172
92
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   173
static void dump_structs(FILE *out)
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   174
{
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   175
    dump_proc_info(out);
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   176
    if (process_type == SERVER)
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   177
    {
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   178
        dump_jobs_struct(out);
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   179
        dump_conns_struct(out);
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   180
    }
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents:
diff changeset
   181
}