msg.c
author viric <viriketo@gmail.com>
Mon, 18 Jul 2011 22:48:54 +0200
branchqueuelimit
changeset 293 bb87d5e7c466
parent 286 f648473fd056
permissions -rw-r--r--
Closing this branch, where I wrote bad code about adding the proper handling of queue limits by blocking clients.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49
09bb8a5583e9 Added the license.
lbatlle@npdl268.bpo.hp.com
parents: 44
diff changeset
     1
/*
09bb8a5583e9 Added the license.
lbatlle@npdl268.bpo.hp.com
parents: 44
diff changeset
     2
    Task Spooler - a task queue system for the unix user
267
11631dd11ff8 Updating copyright years in the source.
viric@mandarina
parents: 250
diff changeset
     3
    Copyright (C) 2007-2009  LluĂ­s Batlle i Rossell
49
09bb8a5583e9 Added the license.
lbatlle@npdl268.bpo.hp.com
parents: 44
diff changeset
     4
09bb8a5583e9 Added the license.
lbatlle@npdl268.bpo.hp.com
parents: 44
diff changeset
     5
    Please find the license in the provided COPYING file.
09bb8a5583e9 Added the license.
lbatlle@npdl268.bpo.hp.com
parents: 44
diff changeset
     6
*/
44
4dcf05746ece Better include files.
viric@llimona
parents: 32
diff changeset
     7
#include <sys/types.h>
4dcf05746ece Better include files.
viric@llimona
parents: 32
diff changeset
     8
#include <sys/socket.h>
4dcf05746ece Better include files.
viric@llimona
parents: 32
diff changeset
     9
#include <stdio.h>
146
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents: 117
diff changeset
    10
#include <sys/time.h>
44
4dcf05746ece Better include files.
viric@llimona
parents: 32
diff changeset
    11
#include <stdlib.h>
92
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents: 49
diff changeset
    12
#include "main.h"
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
diff changeset
    13
235
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    14
void send_bytes(const int fd, const char *data, int bytes)
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
diff changeset
    15
{
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
diff changeset
    16
    int res;
235
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    17
    int offset = 0;
236
cb9c3b46c874 Removing msgdump calls.
viric@mandarina
parents: 235
diff changeset
    18
235
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    19
    while(1)
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    20
    {
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    21
        res = send(fd, data + offset, bytes, 0);
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    22
        if(res == -1)
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    23
        {
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    24
            warning("Sending %i bytes to %i.", bytes, fd);
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    25
            break;
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    26
        }
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    27
        if(res == bytes)
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    28
            break;
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    29
        offset += res;
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    30
        bytes -= res;
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    31
    }
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
diff changeset
    32
}
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
diff changeset
    33
235
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    34
int recv_bytes(const int fd, char *data, int bytes)
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
diff changeset
    35
{
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
diff changeset
    36
    int res;
235
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    37
    int offset = 0;
236
cb9c3b46c874 Removing msgdump calls.
viric@mandarina
parents: 235
diff changeset
    38
235
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    39
    while(1)
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    40
    {
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    41
        res = recv(fd, data + offset, bytes, 0);
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    42
        if(res == -1)
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    43
        {
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    44
            warning("Receiving %i bytes from %i.", bytes, fd);
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    45
            break;
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    46
        }
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    47
        if(res == bytes)
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    48
            break;
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    49
        offset += res;
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    50
        bytes -= res;
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    51
    }
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
diff changeset
    52
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
diff changeset
    53
    return res;
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
diff changeset
    54
}
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
diff changeset
    55
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
diff changeset
    56
void send_msg(const int fd, const struct msg *m)
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
diff changeset
    57
{
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
diff changeset
    58
    int res;
236
cb9c3b46c874 Removing msgdump calls.
viric@mandarina
parents: 235
diff changeset
    59
285
0a105f193446 Working on the queue limit. It still does not work but I don't know why. I've
viric <viriketo@gmail.com>
parents: 273
diff changeset
    60
    if (1)
286
f648473fd056 Enabling a heavier msgdump. I still don't get what goes wrong.
viric <viriketo@gmail.com>
parents: 285
diff changeset
    61
        msgdump(stderr, "send", m);
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
diff changeset
    62
    res = send(fd, m, sizeof(*m), 0);
235
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    63
    if(res == -1 || res != sizeof(*m))
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    64
        warning_msg(m, "Sending a message to %i, sent %i bytes, should "
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    65
                "send %i.", fd,
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    66
                res, sizeof(*m));
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
diff changeset
    67
}
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
diff changeset
    68
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
diff changeset
    69
int recv_msg(const int fd, struct msg *m)
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
diff changeset
    70
{
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
diff changeset
    71
    int res;
236
cb9c3b46c874 Removing msgdump calls.
viric@mandarina
parents: 235
diff changeset
    72
21
a797f96a022f Lines for list doesn't have limit.
viric@llimona
parents: 19
diff changeset
    73
    res = recv(fd, m, sizeof(*m), 0);
92
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents: 49
diff changeset
    74
    if(res == -1)
111
d6bc62904b5a Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents: 92
diff changeset
    75
        warning_msg(m, "Receiving a message from %i.", fd);
285
0a105f193446 Working on the queue limit. It still does not work but I don't know why. I've
viric <viriketo@gmail.com>
parents: 273
diff changeset
    76
    if (res == sizeof(*m) || 1)
286
f648473fd056 Enabling a heavier msgdump. I still don't get what goes wrong.
viric <viriketo@gmail.com>
parents: 285
diff changeset
    77
        msgdump(stderr, "recv", m);
271
11e4c2ac9607 Fixing a problem in msg.c, which resulted in Warnings very often in ts.error.
viric@llimona
parents: 236
diff changeset
    78
    if (res != sizeof(*m) && res > 0)
235
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    79
        warning_msg(m, "Receiving a message from %i, received %i bytes, "
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    80
                "should have received %i.", fd,
97064e7d2969 Assuring the send/recv loops, which didn't pay attention to data sent in
llbatlle@taga
parents: 231
diff changeset
    81
                res, sizeof(*m));
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
diff changeset
    82
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
diff changeset
    83
    return res;
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents:
diff changeset
    84
}