~ubuntu-branches/ubuntu/vivid/slurm-llnl/vivid

« back to all changes in this revision

Viewing changes to src/common/write_labelled_message.c

  • Committer: Bazaar Package Importer
  • Author(s): Gennaro Oliva
  • Date: 2009-09-24 23:28:15 UTC
  • mfrom: (1.1.11 upstream) (3.2.4 sid)
  • Revision ID: james.westby@ubuntu.com-20090924232815-enh65jn32q1ebg07
Tags: 2.0.5-1
* New upstream release 
* Changed dependecy from lib-mysqlclient15 to lib-mysqlclient 
* Added Default-Start for runlevel 2 and 4 and $remote_fs requirement in
  init.d scripts (Closes: #541252)
* Postinst checks for wrong runlevels 2 and 4 links
* Upgraded to standard version 3.8.3
* Add lintian overrides for missing slurm-llnl-configurator.html in doc
  base registration
* modified postrm scripts to ignore pkill return value in order to avoid
  postrm failure when no slurm process is running
* Checking for slurmctld.pid before cancelling running and pending
  jobs during package removal 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************\
 
2
 *  write_labelled_message.c - write a message with an optional label
 
3
 *****************************************************************************
 
4
 *  Copyright (C) 2009 Lawrence Livermore National Security.
 
5
 *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
 
6
 *  Written by Christopher J. Morrone <morrone2@llnl.gov> and 
 
7
 *  David Bremer <dbremer@llnl.gov>
 
8
 *  CODE-OCEC-09-009. All rights reserved.
 
9
 *  
 
10
 *  This file is part of SLURM, a resource management program.
 
11
 *  For details, see <https://computing.llnl.gov/linux/slurm/>.
 
12
 *  Please also read the included file: DISCLAIMER.
 
13
 *  
 
14
 *  SLURM is free software; you can redistribute it and/or modify it under
 
15
 *  the terms of the GNU General Public License as published by the Free
 
16
 *  Software Foundation; either version 2 of the License, or (at your option)
 
17
 *  any later version.
 
18
 *
 
19
 *  In addition, as a special exception, the copyright holders give permission 
 
20
 *  to link the code of portions of this program with the OpenSSL library under
 
21
 *  certain conditions as described in each individual source file, and 
 
22
 *  distribute linked combinations including the two. You must obey the GNU 
 
23
 *  General Public License in all respects for all of the code used other than 
 
24
 *  OpenSSL. If you modify file(s) with this exception, you may extend this 
 
25
 *  exception to your version of the file(s), but you are not obligated to do 
 
26
 *  so. If you do not wish to do so, delete this exception statement from your
 
27
 *  version.  If you delete this exception statement from all source files in 
 
28
 *  the program, then also delete it here.
 
29
 *  
 
30
 *  SLURM is distributed in the hope that it will be useful, but WITHOUT ANY
 
31
 *  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
32
 *  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
33
 *  details.
 
34
 *  
 
35
 *  You should have received a copy of the GNU General Public License along
 
36
 *  with SLURM; if not, write to the Free Software Foundation, Inc.,
 
37
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
 
38
\*****************************************************************************/
 
39
 
 
40
#include <signal.h>
 
41
#include <stdio.h>
 
42
#include <errno.h>
 
43
#include <string.h>
 
44
 
 
45
#include "src/common/write_labelled_message.h"
 
46
#include "slurm/slurm_errno.h"
 
47
#include "src/common/log.h"
 
48
 
 
49
static int _write_label(int fd, int taskid, int label_width);
 
50
static int _write_line(int fd, void *buf, int len);
 
51
static int _write_newline(int fd);
 
52
 
 
53
 
 
54
 
 
55
int write_labelled_message(int fd, void *buf, int len, int taskid,
 
56
                           bool label, int label_width)
 
57
{
 
58
        void *start;
 
59
        void *end;
 
60
        int remaining = len;
 
61
        int written = 0;
 
62
        int line_len;
 
63
        int rc = -1;
 
64
 
 
65
        while (remaining > 0) {
 
66
                start = buf + written;
 
67
                end = memchr(start, '\n', remaining);
 
68
                if (label)
 
69
                        if (_write_label(fd, taskid, label_width)
 
70
                            != SLURM_SUCCESS)
 
71
                                goto done;
 
72
                if (end == NULL) { /* no newline found */
 
73
                        rc = _write_line(fd, start, remaining);
 
74
                        if (rc <= 0) {
 
75
                                goto done;
 
76
                        } else {
 
77
                                remaining -= rc;
 
78
                                written += rc;
 
79
                        }
 
80
                        if (label)
 
81
                                if (_write_newline(fd) != SLURM_SUCCESS)
 
82
                                        goto done;
 
83
                } else {
 
84
                        line_len = (int)(end - start) + 1;
 
85
                        rc = _write_line(fd, start, line_len);
 
86
                        if (rc <= 0) {
 
87
                                goto done;
 
88
                        } else {
 
89
                                remaining -= rc;
 
90
                                written += rc;
 
91
                        }
 
92
                }
 
93
 
 
94
        }
 
95
done:
 
96
        if (written > 0)
 
97
                return written;
 
98
        else
 
99
                return rc;
 
100
}
 
101
 
 
102
 
 
103
static int _write_label(int fd, int taskid, int label_width)
 
104
{
 
105
        int n;
 
106
        int left = label_width + 2;
 
107
        char buf[16];
 
108
        void *ptr = buf;
 
109
 
 
110
        snprintf(buf, 16, "%0*d: ", label_width, taskid);
 
111
        while (left > 0) {
 
112
        again:
 
113
                if ((n = write(fd, ptr, left)) < 0) {
 
114
                        if (errno == EINTR)
 
115
                                goto again;
 
116
                        if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
 
117
                                debug3("  got EAGAIN in _write_label");
 
118
                                goto again;
 
119
                        }
 
120
                        error("In _write_label: %m");
 
121
                        return SLURM_ERROR;
 
122
                }
 
123
                left -= n;
 
124
                ptr += n;
 
125
        }
 
126
 
 
127
        return SLURM_SUCCESS;
 
128
}
 
129
 
 
130
 
 
131
static int _write_newline(int fd)
 
132
{
 
133
        int n;
 
134
 
 
135
        debug2("Called _write_newline");
 
136
again:
 
137
        if ((n = write(fd, "\n", 1)) < 0) {
 
138
                if (errno == EINTR
 
139
                    || errno == EAGAIN
 
140
                    || errno == EWOULDBLOCK) {
 
141
                        goto again;
 
142
                }
 
143
                error("In _write_newline: %m");
 
144
                return SLURM_ERROR;
 
145
        }
 
146
        return SLURM_SUCCESS;
 
147
}
 
148
 
 
149
 
 
150
/*
 
151
 * Blocks until write is complete, regardless of the file
 
152
 * descriptor being in non-blocking mode.
 
153
 */
 
154
static int _write_line(int fd, void *buf, int len)
 
155
{
 
156
        int n;
 
157
        int left = len;
 
158
        void *ptr = buf;
 
159
 
 
160
        debug2("Called _write_line");
 
161
        while (left > 0) {
 
162
        again:
 
163
                if ((n = write(fd, ptr, left)) < 0) {
 
164
                        if (errno == EINTR)
 
165
                                goto again;
 
166
                        if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
 
167
                                debug3("  got EAGAIN in _write_line");
 
168
                                goto again;
 
169
                        }
 
170
                        return -1;
 
171
                }
 
172
                left -= n;
 
173
                ptr += n;
 
174
        }
 
175
        
 
176
        return len;
 
177
}