~ubuntu-branches/ubuntu/precise/torque/precise-updates

« back to all changes in this revision

Viewing changes to src/scheduler.cc/pbs_sched.c

  • Committer: Bazaar Package Importer
  • Author(s): Morten Kjeldgaard
  • Date: 2009-02-16 17:32:28 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090216173228-repbgujfj52115mg
Tags: 2.3.6+dfsg-0ubuntu1
* New upstream release (LP: #235385).
* Torque-server init script fails during installation and 
  removal (LP: #223649) fixed, "set -e" removed from init script.
* Package torque-scheduler 2.1.8+dfsg-0ubuntu1.1 failed to install
  (LP: #244440 LP: #270574) fixed, "set -e" removed from init script.
* /etc/init.d/torque-mom not idempotent, and stop doesn't work
  (LP: #256998) Fixed, "set -e" removed from init script.
* Torque-scheduler prints errors during package configuration
  (LP: #270653). Reason: missing dir sched_config. Fixed,
  package installs FIFO scheduler config file in
  /var/lib/torque/sched_priv/.
* Package torque-mom 2.1.8+dfsg-0ubuntu1 failed to 
  install/upgrade (LP: #276575 LP: #291674). Reason: missing directories.
  Fixed, /var/lib/torque/server_priv/jobs/, 
  /var/lib/torque/server_priv/queues/, 
  /var/lib/torque/server_priv/accounting/ and
  /var/lib/torque/mom_priv/jobs/ are now installed in their respective
  packages.
* Package torque-gui missing most of the files it needs to run!
  (LP: #281360). Reason: missing *.tk etc. files from src/gui
  Fixed: /usr/lib/xpbs now shipped in package
* changed patch system to quilt.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
*         OpenPBS (Portable Batch System) v2.3 Software License
3
 
 
3
*
4
4
* Copyright (c) 1999-2000 Veridian Information Solutions, Inc.
5
5
* All rights reserved.
6
 
 
6
*
7
7
* ---------------------------------------------------------------------------
8
8
* For a license to use or redistribute the OpenPBS software under conditions
9
9
* other than those described below, or to purchase support for this software,
10
10
* please contact Veridian Systems, PBS Products Department ("Licensor") at:
11
 
 
11
*
12
12
*    www.OpenPBS.org  +1 650 967-4675                  sales@OpenPBS.org
13
13
*                        877 902-4PBS (US toll-free)
14
14
* ---------------------------------------------------------------------------
15
 
 
15
*
16
16
* This license covers use of the OpenPBS v2.3 software (the "Software") at
17
17
* your site or location, and, for certain users, redistribution of the
18
18
* Software to other sites and locations.  Use and redistribution of
19
19
* OpenPBS v2.3 in source and binary forms, with or without modification,
20
20
* are permitted provided that all of the following conditions are met.
21
21
* After December 31, 2001, only conditions 3-6 must be met:
22
 
 
22
*
23
23
* 1. Commercial and/or non-commercial use of the Software is permitted
24
24
*    provided a current software registration is on file at www.OpenPBS.org.
25
25
*    If use of this software contributes to a publication, product, or
26
26
*    service, proper attribution must be given; see www.OpenPBS.org/credit.html
27
 
 
27
*
28
28
* 2. Redistribution in any form is only permitted for non-commercial,
29
29
*    non-profit purposes.  There can be no charge for the Software or any
30
30
*    software incorporating the Software.  Further, there can be no
31
31
*    expectation of revenue generated as a consequence of redistributing
32
32
*    the Software.
33
 
 
33
*
34
34
* 3. Any Redistribution of source code must retain the above copyright notice
35
35
*    and the acknowledgment contained in paragraph 6, this list of conditions
36
36
*    and the disclaimer contained in paragraph 7.
37
 
 
37
*
38
38
* 4. Any Redistribution in binary form must reproduce the above copyright
39
39
*    notice and the acknowledgment contained in paragraph 6, this list of
40
40
*    conditions and the disclaimer contained in paragraph 7 in the
41
41
*    documentation and/or other materials provided with the distribution.
42
 
 
42
*
43
43
* 5. Redistributions in any form must be accompanied by information on how to
44
44
*    obtain complete source code for the OpenPBS software and any
45
45
*    modifications and/or additions to the OpenPBS software.  The source code
47
47
*    than the cost of distribution plus a nominal fee, and all modifications
48
48
*    and additions to the Software must be freely redistributable by any party
49
49
*    (including Licensor) without restriction.
50
 
 
50
*
51
51
* 6. All advertising materials mentioning features or use of the Software must
52
52
*    display the following acknowledgment:
53
 
 
53
*
54
54
*     "This product includes software developed by NASA Ames Research Center,
55
 
*     Lawrence Livermore National Laboratory, and Veridian Information 
 
55
*     Lawrence Livermore National Laboratory, and Veridian Information
56
56
*     Solutions, Inc.
57
57
*     Visit www.OpenPBS.org for OpenPBS software support,
58
58
*     products, and information."
59
 
 
59
*
60
60
* 7. DISCLAIMER OF WARRANTY
61
 
 
61
*
62
62
* THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. ANY EXPRESS
63
63
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
64
64
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT
65
65
* ARE EXPRESSLY DISCLAIMED.
66
 
 
66
*
67
67
* IN NO EVENT SHALL VERIDIAN CORPORATION, ITS AFFILIATED COMPANIES, OR THE
68
68
* U.S. GOVERNMENT OR ANY OF ITS AGENCIES BE LIABLE FOR ANY DIRECT OR INDIRECT,
69
69
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
72
72
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
73
73
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
74
74
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
75
 
 
75
*
76
76
* This license will be governed by the laws of the Commonwealth of Virginia,
77
77
* without reference to its choice of law rules.
78
78
*/
79
79
#include <pbs_config.h>   /* the master config generated by configure */
80
80
 
81
81
#include        <unistd.h>
82
 
#include        <stdio.h>
 
82
#include <stdio.h>
83
83
#include        <stdlib.h>
84
 
#include        <sys/time.h>
 
84
#include <sys/time.h>
85
85
#if (PLOCK_DAEMONS & 2)
86
 
#include        <sys/lock.h>
 
86
#include <sys/lock.h>
87
87
#endif /* PLOCK_DAEMONS */
88
 
#include        "libpbs.h"
 
88
#include "libpbs.h"
89
89
#include        <string.h>
90
90
#include        <signal.h>
91
 
#include        <time.h>
 
91
#include <time.h>
92
92
#include        <ctype.h>
93
93
#include        <limits.h>
94
94
#include        <fcntl.h>
95
 
#include        <errno.h>
96
 
#include        <netdb.h>
97
 
#include        <grp.h>
 
95
#include <errno.h>
 
96
#include <netdb.h>
 
97
#include <grp.h>
98
98
#include        <sys/types.h>
99
99
#include        <sys/wait.h>
100
100
#include        <sys/stat.h>
108
108
 
109
109
#ifdef _CRAY
110
110
#include <sys/category.h>
111
 
#endif  /* _CRAY */
 
111
#endif /* _CRAY */
112
112
#include <sys/resource.h>
113
113
 
114
 
#include        "portability.h"
115
 
#include        "pbs_error.h"
116
 
#include        "pbs_ifl.h"
117
 
#include        "log.h"
118
 
#include        "resmon.h"
119
 
#include        "sched_cmds.h"
120
 
#include        "server_limits.h"
121
 
#include        "net_connect.h"
122
 
#include        "rpp.h"
123
 
#include        "rm.h"
124
 
#include        "libpbs.h"
125
 
 
126
 
extern int      pbs_errno;
127
 
int             connector;
128
 
int             server_sock;
129
 
 
130
 
#define         START_CLIENTS   2       /* minimum number of clients */
131
 
pbs_net_t       *okclients = NULL;      /* accept connections from */
132
 
int             numclients = 0;         /* the number of clients */
 
114
#include "portability.h"
 
115
#include "pbs_error.h"
 
116
#include "pbs_ifl.h"
 
117
#include "log.h"
 
118
#include "resmon.h"
 
119
#include "sched_cmds.h"
 
120
#include "server_limits.h"
 
121
#include "net_connect.h"
 
122
#include "rpp.h"
 
123
#include "rm.h"
 
124
#include "libpbs.h"
 
125
 
 
126
extern int pbs_errno;
 
127
int  connector;
 
128
int  server_sock;
 
129
 
 
130
#define  START_CLIENTS 2 /* minimum number of clients */
 
131
pbs_net_t *okclients = NULL; /* accept connections from */
 
132
int  numclients = 0;  /* the number of clients */
133
133
pbs_net_t      *maskclient = NULL;      /* wildcard connections */
134
134
int             mask_num = 0;
135
 
char            *configfile = NULL;     /* name of config file */
136
 
 
137
 
char            *oldpath;
138
 
extern char     *msg_daemonname;
139
 
char            **glob_argv;
140
 
char            usage[] = "[-S port][-d home][-p output][-c config][-a alarm]";
141
 
struct  sockaddr_in     saddr;
142
 
sigset_t        allsigs;
143
 
int             alarm_time;
 
135
char  *configfile = NULL; /* name of config file */
 
136
 
 
137
char  *oldpath;
 
138
extern char *msg_daemonname;
 
139
char  **glob_argv;
 
140
char  usage[] = "[-S port][-d home][-p output][-c config][-a alarm]";
 
141
 
 
142
struct sockaddr_in saddr;
 
143
sigset_t allsigs;
 
144
int  alarm_time;
144
145
static char    *logfile = (char *)0;
145
 
static char     path_log[_POSIX_PATH_MAX];
146
 
int             pbs_rm_port;
147
 
 
148
 
int     schedreq();
149
 
 
150
 
extern int get_4byte(int,int *);
 
146
static char path_log[_POSIX_PATH_MAX];
 
147
char path_acct[_POSIX_PATH_MAX];
 
148
int   pbs_rm_port;
 
149
 
 
150
int             schedreq();
 
151
 
 
152
extern int get_4byte(int, unsigned int *);
151
153
 
152
154
 
153
155
/*
161
163
  {
162
164
  char *id = "die";
163
165
 
164
 
  if (sig > 0) 
 
166
  if (sig > 0)
165
167
    {
166
168
    sprintf(log_buffer, "caught signal %d", sig);
167
169
 
168
170
    log_record(PBSEVENT_SYSTEM, PBS_EVENTCLASS_SERVER,
169
 
      id, log_buffer);
 
171
               id, log_buffer);
170
172
    }
171
 
  else 
 
173
  else
172
174
    {
173
175
    log_record(PBSEVENT_SYSTEM, PBS_EVENTCLASS_SERVER,
174
 
      id, "abnormal termination");
 
176
               id, "abnormal termination");
175
177
    }
176
178
 
177
179
  log_close(1);
182
184
 
183
185
 
184
186
 
185
 
  /*
186
 
   * Catch core dump signals - set core size so we can see what happened!
187
 
   *
188
 
   * Chris Samuel - VPAC
189
 
   * csamuel@vpac.org - 29th July 2003
190
 
   */
 
187
/*
 
188
 * Catch core dump signals - set core size so we can see what happened!
 
189
 *
 
190
 * Chris Samuel - VPAC
 
191
 * csamuel@vpac.org - 29th July 2003
 
192
 */
191
193
 
192
194
static void catch_abort(
193
195
 
194
196
  int sig)
195
197
 
196
198
  {
 
199
 
197
200
  struct rlimit rlimit;
 
201
 
198
202
  struct sigaction act;
199
203
 
200
204
  /*
213
217
  sigaction(SIGTRAP, &act, NULL);
214
218
  sigaction(SIGSYS, &act, NULL);
215
219
 
216
 
  log_err (sig,"pbs_sched","Caught fatal core signal");
 
220
  log_err(sig, "pbs_sched", "Caught fatal core signal");
217
221
  rlimit.rlim_cur = RLIM_INFINITY;
218
222
  rlimit.rlim_max = RLIM_INFINITY;
219
223
 
235
239
  {
236
240
  close(connection[connect].ch_socket);
237
241
 
238
 
  if (connection[connect].ch_errtxt != (char *)NULL) 
 
242
  if (connection[connect].ch_errtxt != (char *)NULL)
239
243
    free(connection[connect].ch_errtxt);
240
244
 
241
245
  connection[connect].ch_errno = 0;
 
246
 
242
247
  connection[connect].ch_inuse = 0;
243
248
 
244
249
  return(0);
249
254
 
250
255
 
251
256
/*
252
 
**      Got an alarm call.
 
257
** Got an alarm call.
253
258
*/
254
 
void
255
 
toolong(sig)
256
 
int     sig;
257
 
{
258
 
        char    *id = "toolong";
259
 
        struct  stat    sb;
260
 
        pid_t   cpid;
261
 
 
262
 
        log_record(PBSEVENT_SYSTEM, PBS_EVENTCLASS_SERVER, id, "alarm call");
263
 
        DBPRT(("alarm call\n"))
264
 
 
265
 
        if (connector >= 0 && server_disconnect(connector))
266
 
                log_err(errno, id, "server_disconnect");
267
 
        if (close(server_sock))
268
 
                log_err(errno, id, "close");
269
 
 
270
 
        if ((cpid = fork()) > 0) {      /* parent re-execs itself */
271
 
                rpp_terminate();
 
259
 
 
260
void toolong(
 
261
 
 
262
  int sig)
 
263
 
 
264
  {
 
265
  char *id = "toolong";
 
266
 
 
267
  struct stat sb;
 
268
  pid_t cpid;
 
269
 
 
270
  log_record(PBSEVENT_SYSTEM, PBS_EVENTCLASS_SERVER, id,
 
271
             "scheduling iteration took too long");
 
272
  DBPRT(("scheduling iteration too long\n"))
 
273
 
 
274
  if (connector >= 0 && server_disconnect(connector))
 
275
    log_err(errno, id, "server_disconnect");
 
276
 
 
277
  if (close(server_sock))
 
278
    log_err(errno, id, "close");
 
279
 
 
280
  if ((cpid = fork()) > 0)   /* parent re-execs itself */
 
281
    {
 
282
    rpp_terminate();
272
283
#ifndef linux
273
 
                sleep(5);
 
284
    sleep(5);
274
285
#endif
275
286
 
276
 
                /* hopefully, that gave the child enough */
277
 
                /*   time to do its business. anyhow:    */
278
 
                (void)waitpid(cpid, NULL, 0);
279
 
 
280
 
                if (chdir(oldpath) == -1) {
281
 
                        sprintf(log_buffer, "chdir to %s", oldpath);
282
 
                        log_err(errno, id, log_buffer);
283
 
                }
284
 
 
285
 
                sprintf(log_buffer, "restart dir %s object %s",
286
 
                        oldpath, glob_argv[0]);
287
 
                log_record(PBSEVENT_SYSTEM, PBS_EVENTCLASS_SERVER,
288
 
                        id, log_buffer);
289
 
 
290
 
                execv(glob_argv[0], glob_argv);
291
 
                log_err(errno, id, "execv");
292
 
                exit(3);
293
 
        }
294
 
 
295
 
        /*
296
 
        **      Child (or error on fork) gets here and tried
297
 
        **      to dump core.
298
 
        */
299
 
        if (stat("core", &sb) == -1) {
300
 
                if (errno == ENOENT) {
301
 
                        log_close(1);
302
 
                        abort();
303
 
                        rpp_terminate();
304
 
                        exit(2);        /* not reached (hopefully) */
305
 
                }
306
 
                log_err(errno, id, "stat");
307
 
        }
308
 
        log_record(PBSEVENT_SYSTEM, PBS_EVENTCLASS_SERVER,
309
 
                id, "exiting without core dump");
310
 
        log_close(1);
311
 
        rpp_terminate();
312
 
        exit(0);
313
 
}
314
 
 
 
287
    /* hopefully, that gave the child enough */
 
288
    /*   time to do its business. anyhow:    */
 
289
    (void)waitpid(cpid, NULL, 0);
 
290
 
 
291
    if (chdir(oldpath) == -1)
 
292
      {
 
293
      sprintf(log_buffer, "chdir to %s", oldpath);
 
294
      log_err(errno, id, log_buffer);
 
295
      }
 
296
 
 
297
    sprintf(log_buffer, "restart dir %s object %s",
 
298
 
 
299
            oldpath, glob_argv[0]);
 
300
    log_record(PBSEVENT_SYSTEM, PBS_EVENTCLASS_SERVER,
 
301
               id, log_buffer);
 
302
 
 
303
    execv(glob_argv[0], glob_argv);
 
304
    log_err(errno, id, "execv");
 
305
    exit(3);
 
306
    }
 
307
 
 
308
  /*
 
309
  ** Child (or error on fork) gets here and tried
 
310
  ** to dump core.
 
311
  */
 
312
  if (stat("core", &sb) == -1)
 
313
    {
 
314
    if (errno == ENOENT)
 
315
      {
 
316
      log_close(1);
 
317
      abort();
 
318
      rpp_terminate();
 
319
      exit(2); /* not reached (hopefully) */
 
320
      }
 
321
 
 
322
    log_err(errno, id, "stat");
 
323
    }
 
324
 
 
325
  log_record(PBSEVENT_SYSTEM, PBS_EVENTCLASS_SERVER,
 
326
 
 
327
             id, "exiting without core dump");
 
328
  log_close(1);
 
329
  rpp_terminate();
 
330
  exit(0);
 
331
  }
 
332
 
 
333
 
 
334
/* sock refers to an opened socket */
315
335
int
316
 
socket_to_conn(sock)
317
 
    int sock;           /* opened socket */
318
 
{
319
 
        int     i;
320
 
 
321
 
        for (i=0; i<NCONNECTS; i++) {
322
 
                if (connection[i].ch_inuse == 0) {
323
 
 
324
 
                        connection[i].ch_inuse = 1;
325
 
                        connection[i].ch_errno = 0;
326
 
                        connection[i].ch_socket= sock;
327
 
                        connection[i].ch_errtxt = NULL;
328
 
                        return (i);
329
 
                }
330
 
        }
331
 
        pbs_errno = PBSE_NOCONNECTS;
332
 
        return (-1);
333
 
}
 
336
socket_to_conn(int sock)
 
337
  {
 
338
  int     i;
 
339
 
 
340
  for (i = 0; i < NCONNECTS; i++)
 
341
    {
 
342
    if (connection[i].ch_inuse == 0)
 
343
      {
 
344
 
 
345
      connection[i].ch_inuse = 1;
 
346
      connection[i].ch_errno = 0;
 
347
      connection[i].ch_socket = sock;
 
348
      connection[i].ch_errtxt = NULL;
 
349
      return (i);
 
350
      }
 
351
    }
 
352
 
 
353
  pbs_errno = PBSE_NOCONNECTS;
 
354
 
 
355
  return (-1);
 
356
  }
334
357
 
335
358
 
336
359
 
343
366
  static  char    id[] = "restricted";
344
367
 
345
368
  struct  hostent         *host;
 
369
 
346
370
  struct  in_addr saddr;
347
371
  pbs_net_t       *newclients;
348
 
 
349
 
  if ((host = gethostbyname(name)) == NULL) 
 
372
 
 
373
  if ((host = gethostbyname(name)) == NULL)
350
374
    {
351
 
    sprintf(log_buffer,"host %s not found", 
352
 
      name);
 
375
    sprintf(log_buffer, "host %s not found",
 
376
            name);
353
377
 
354
 
    log_err(-1,id,log_buffer);
 
378
    log_err(-1, id, log_buffer);
355
379
 
356
380
    return(-1);
357
381
    }
358
382
 
359
 
  if (mask_num > 0) 
 
383
  if (mask_num > 0)
360
384
    {
361
 
    newclients = realloc(maskclient,sizeof(pbs_net_t) * (mask_num + 1));
 
385
    newclients = realloc(maskclient, sizeof(pbs_net_t) * (mask_num + 1));
362
386
    }
363
 
  else 
 
387
  else
364
388
    {
365
389
    newclients = malloc(sizeof(pbs_net_t));
366
390
    }
372
396
 
373
397
  maskclient = newclients;
374
398
 
375
 
  log_record(PBSEVENT_SYSTEM,PBS_EVENTCLASS_SERVER,id,name);
 
399
  log_record(PBSEVENT_SYSTEM, PBS_EVENTCLASS_SERVER, id, name);
376
400
 
377
 
  memcpy((char *)&saddr,host->h_addr,host->h_length);
 
401
  memcpy((char *)&saddr, host->h_addr, host->h_length);
378
402
 
379
403
  maskclient[mask_num++] = saddr.s_addr;
380
404
 
381
405
  return(0);
382
406
  }
383
 
 
 
407
 
384
408
 
385
409
 
386
410
int addclient(
389
413
 
390
414
  {
391
415
  static char     id[] = "addclient";
 
416
 
392
417
  struct hostent *host;
 
418
 
393
419
  struct in_addr  saddr;
394
420
 
395
 
  if ((host = gethostbyname(name)) == NULL) 
 
421
  if ((host = gethostbyname(name)) == NULL)
396
422
    {
397
 
    sprintf(log_buffer,"host %s not found", 
398
 
      name);
 
423
    sprintf(log_buffer, "host %s not found",
 
424
            name);
399
425
 
400
 
    log_err(-1,id,log_buffer);
 
426
    log_err(-1, id, log_buffer);
401
427
 
402
428
    return(-1);
403
429
    }
404
430
 
405
 
        if (numclients >= START_CLIENTS) {
406
 
                pbs_net_t       *newclients;
407
 
 
408
 
                newclients = realloc(okclients,
409
 
                                sizeof(pbs_net_t)*(numclients+1));
410
 
                if (newclients == NULL)
411
 
                        return -1;
412
 
                okclients = newclients;
413
 
        }
414
 
        memcpy((char *)&saddr, host->h_addr, host->h_length);
415
 
        okclients[numclients++] = saddr.s_addr;
416
 
        return 0;
417
 
}
 
431
  if (numclients >= START_CLIENTS)
 
432
    {
 
433
    pbs_net_t *newclients;
 
434
 
 
435
    newclients = realloc(okclients,
 
436
                         sizeof(pbs_net_t) * (numclients + 1));
 
437
 
 
438
    if (newclients == NULL)
 
439
      return -1;
 
440
 
 
441
    okclients = newclients;
 
442
    }
 
443
 
 
444
  memcpy((char *)&saddr, host->h_addr, host->h_length);
 
445
 
 
446
  okclients[numclients++] = saddr.s_addr;
 
447
  return 0;
 
448
  }
418
449
 
419
450
 
420
451
 
425
456
/*
426
457
 * read_config - read and process the configuration file (see -c option)
427
458
 *
428
 
 *  Currently, the parameters are 
 
459
 *  Currently, the parameters are
429
460
 
430
461
      $clienthost - specify which systems can contact the scheduler
431
462
      $restricted - specify which systems can provide rm services on non-privileged ports
440
471
  {
441
472
  static char *id = "read_config";
442
473
 
443
 
  FILE  *conf;
444
 
  int   i;
445
 
  char  line[CONF_LINE_LEN];
446
 
  char  *token;
447
 
  struct        specialconfig {
448
 
  char  *name;
449
 
  int   (*handler)();
450
 
  } special[] = {
451
 
    {"clienthost", addclient },
 
474
  FILE *conf;
 
475
  int i;
 
476
  char line[CONF_LINE_LEN];
 
477
  char *token;
 
478
 
 
479
  struct specialconfig
 
480
    {
 
481
    char *name;
 
482
    int (*handler)();
 
483
    } special[] =
 
484
 
 
485
    {
 
486
      {"clienthost", addclient },
452
487
    {"restricted", restricted },
453
 
    { NULL,        NULL } };
 
488
    { NULL,    NULL }
 
489
    };
454
490
 
455
491
 
456
492
#if !defined(DEBUG) && !defined(NO_SECURITY_CHECK)
457
 
  if (chk_file_sec(file,0,0,S_IWGRP|S_IWOTH,1))
 
493
 
 
494
  if (chk_file_sec(file, 0, 0, S_IWGRP | S_IWOTH, 1, NULL))
458
495
    {
459
496
    return(-1);
460
497
    }
 
498
 
461
499
#endif
462
500
 
463
501
  free(maskclient);
464
 
 
 
502
 
465
503
  mask_num = 0;
466
504
 
467
 
        if ((conf = fopen(file, "r")) == NULL) {
468
 
                log_err(errno, id, "cannot open config file");
469
 
                return (-1);
470
 
        }
471
 
        while (fgets(line, CONF_LINE_LEN, conf)) {
472
 
 
473
 
                if ((line[0] == '#') || (line[0] == '\n'))
474
 
                        continue;               /* ignore comment & null line */
475
 
                else if (line[0] == '$') {      /* special */
476
 
 
477
 
                        if ((token = strtok(line, " \t")) == NULL)
478
 
                                token = "";
479
 
                        for (i=0; special[i].name; i++) {
480
 
                                if (strcmp(token+1, special[i].name) == 0)
481
 
                                        break;
482
 
                        }
483
 
                        if (special[i].name == NULL) {
484
 
                                sprintf(log_buffer,"config name %s not known",
485
 
                                        token);
486
 
                                log_record(PBSEVENT_ERROR,
487
 
                                        PBS_EVENTCLASS_SERVER,
488
 
                                        msg_daemonname, log_buffer);
489
 
                                continue;
490
 
                        }
491
 
                        token = strtok(NULL, " \t");
492
 
                        if (*(token+strlen(token)-1) == '\n')
493
 
                                *(token+strlen(token)-1) = '\0';
494
 
                        if (special[i].handler(token)) {
495
 
                                fclose(conf);
496
 
                                return (-1);
497
 
                        }
498
 
 
499
 
                } else {
500
 
                        log_record(PBSEVENT_ERROR, PBS_EVENTCLASS_SERVER,
501
 
                                  msg_daemonname,
502
 
                                  "invalid line in config file");
503
 
                        fclose(conf);
504
 
                        return (-1);
505
 
                }
506
 
        }
507
 
        fclose(conf);
508
 
        return (0);
509
 
}
510
 
 
511
 
void
512
 
restart(sig)
513
 
    int sig;
514
 
{
515
 
        char    *id = "restart";
516
 
 
517
 
        if (sig) {
518
 
                log_close(1);
519
 
                log_open(logfile, path_log);
520
 
                sprintf(log_buffer, "restart on signal %d", sig);
521
 
        } else {
522
 
                sprintf(log_buffer, "restart command");
523
 
        }
524
 
        log_record(PBSEVENT_SYSTEM, PBS_EVENTCLASS_SERVER, id, log_buffer);
525
 
        if (configfile) {
526
 
                if (read_config(configfile) != 0)
527
 
                        die(0);
528
 
        }
529
 
}
530
 
 
531
 
void
532
 
badconn(msg)
533
 
    char        *msg;
534
 
{
535
 
        static  char    id[] = "badconn";
536
 
        struct  in_addr addr;
537
 
        char            buf[5*sizeof(addr) + 100];
538
 
        struct  hostent *phe;
539
 
 
540
 
        addr = saddr.sin_addr;
541
 
        phe = gethostbyaddr((void *)&addr, sizeof(addr), AF_INET);
542
 
        if (phe == NULL) {
543
 
                char    hold[6];
544
 
                int     i;
545
 
                union {
546
 
                        struct  in_addr aa;
547
 
                        u_char          bb[sizeof(addr)];
548
 
                } uu;
549
 
 
550
 
                uu.aa = addr;
551
 
                sprintf(buf, "%u", uu.bb[0]);
552
 
                for(i=1; i<(int)sizeof(addr); i++) {
553
 
                        sprintf(hold, ".%u", uu.bb[i]);
554
 
                        strcat(buf, hold);
555
 
                }
556
 
        }
557
 
        else {
558
 
                strncpy(buf, phe->h_name, sizeof(buf));
559
 
                buf[sizeof(buf)-1] = '\0';
560
 
        }
561
 
 
562
 
        sprintf(log_buffer, "%s on port %u %s",
563
 
                        buf, ntohs(saddr.sin_port), msg);
564
 
        log_err(-1, id, log_buffer);
 
505
  if ((conf = fopen(file, "r")) == NULL)
 
506
    {
 
507
    log_err(errno, id, "cannot open config file");
 
508
 
 
509
    return(-1);
 
510
    }
 
511
 
 
512
  while (fgets(line, CONF_LINE_LEN, conf))
 
513
    {
 
514
 
 
515
    if ((line[0] == '#') || (line[0] == '\n'))
 
516
      continue;  /* ignore comment & null line */
 
517
    else if (line[0] == '$')   /* special */
 
518
      {
 
519
 
 
520
      if ((token = strtok(line, " \t")) == NULL)
 
521
        token = "";
 
522
 
 
523
      for (i = 0; special[i].name; i++)
 
524
        {
 
525
        if (strcmp(token + 1, special[i].name) == 0)
 
526
          break;
 
527
        }
 
528
 
 
529
      if (special[i].name == NULL)
 
530
        {
 
531
        sprintf(log_buffer, "config name %s not known",
 
532
                token);
 
533
        log_record(PBSEVENT_ERROR,
 
534
                   PBS_EVENTCLASS_SERVER,
 
535
                   msg_daemonname, log_buffer);
 
536
        continue;
 
537
        }
 
538
 
 
539
      token = strtok(NULL, " \t");
 
540
 
 
541
      if (*(token + strlen(token) - 1) == '\n')
 
542
        *(token + strlen(token) - 1) = '\0';
 
543
 
 
544
      if (special[i].handler(token))
 
545
        {
 
546
        fclose(conf);
 
547
        return (-1);
 
548
        }
 
549
 
 
550
      }
 
551
    else
 
552
      {
 
553
      log_record(PBSEVENT_ERROR, PBS_EVENTCLASS_SERVER,
 
554
                 msg_daemonname,
 
555
                 "invalid line in config file");
 
556
      fclose(conf);
 
557
      return (-1);
 
558
      }
 
559
    }
 
560
 
 
561
  fclose(conf);
 
562
 
 
563
  return (0);
 
564
  }
 
565
 
 
566
void
 
567
restart(int sig)
 
568
  {
 
569
  char    *id = "restart";
 
570
 
 
571
  if (sig)
 
572
    {
 
573
    log_close(1);
 
574
    log_open(logfile, path_log);
 
575
    sprintf(log_buffer, "restart on signal %d", sig);
 
576
    }
 
577
  else
 
578
    {
 
579
    sprintf(log_buffer, "restart command");
 
580
    }
 
581
 
 
582
  log_record(PBSEVENT_SYSTEM, PBS_EVENTCLASS_SERVER, id, log_buffer);
 
583
 
 
584
  if (configfile)
 
585
    {
 
586
    if (read_config(configfile) != 0)
 
587
      die(0);
 
588
    }
 
589
  }
 
590
 
 
591
void
 
592
badconn(char *msg)
 
593
  {
 
594
  static char id[] = "badconn";
 
595
 
 
596
  struct in_addr addr;
 
597
  char  buf[5*sizeof(addr) + 100];
 
598
 
 
599
  struct hostent *phe;
 
600
 
 
601
  addr = saddr.sin_addr;
 
602
  phe = gethostbyaddr((void *) & addr, sizeof(addr), AF_INET);
 
603
 
 
604
  if (phe == NULL)
 
605
    {
 
606
    char hold[6];
 
607
    int i;
 
608
    union
 
609
      {
 
610
 
 
611
      struct in_addr aa;
 
612
      u_char  bb[sizeof(addr)];
 
613
      } uu;
 
614
 
 
615
    uu.aa = addr;
 
616
    sprintf(buf, "%u", uu.bb[0]);
 
617
 
 
618
    for (i = 1; i < (int)sizeof(addr); i++)
 
619
      {
 
620
      sprintf(hold, ".%u", uu.bb[i]);
 
621
      strcat(buf, hold);
 
622
      }
 
623
    }
 
624
  else
 
625
    {
 
626
    strncpy(buf, phe->h_name, sizeof(buf));
 
627
    buf[sizeof(buf)-1] = '\0';
 
628
    }
 
629
 
 
630
  sprintf(log_buffer, "%s on port %u %s",
 
631
 
 
632
          buf, ntohs(saddr.sin_port), msg);
 
633
  log_err(-1, id, log_buffer);
565
634
 
566
635
  return;
567
636
  }
570
639
 
571
640
 
572
641
 
573
 
int server_command()
 
642
int
 
643
server_command(void)
574
644
 
575
645
  {
576
 
  char  *id = "server_command";
 
646
  char *id = "server_command";
577
647
 
578
648
  int           new_socket;
579
 
  torque_socklen_t      slen;
580
 
  int           i, cmd;
581
 
  pbs_net_t     addr;
 
649
  torque_socklen_t slen;
 
650
  int  i;
 
651
  unsigned int  cmd;
 
652
  pbs_net_t addr;
582
653
 
583
654
  slen = sizeof(saddr);
584
655
 
585
 
  new_socket = accept(server_sock,(struct sockaddr *)&saddr,&slen);
 
656
  new_socket = accept(server_sock, (struct sockaddr *) & saddr, &slen);
586
657
 
587
 
  if (new_socket == -1) 
 
658
  if (new_socket == -1)
588
659
    {
589
 
    log_err(errno,id,"accept");
 
660
    log_err(errno, id, "accept");
590
661
 
591
662
    return(SCH_ERROR);
592
663
    }
593
664
 
594
665
  addr = (pbs_net_t)saddr.sin_addr.s_addr;
595
 
 
596
 
  if (ntohs(saddr.sin_port) >= IPPORT_RESERVED) 
 
666
 
 
667
  if (ntohs(saddr.sin_port) >= IPPORT_RESERVED)
597
668
    {
598
 
    for (i = 0;i < mask_num;i++) 
 
669
    for (i = 0;i < mask_num;i++)
599
670
      {
600
671
      if (addr == maskclient[i])
601
672
        break;
602
673
      }
603
674
 
604
 
    if (i == mask_num) 
 
675
    if (i == mask_num)
605
676
      {
606
677
      badconn("non-reserved port");
607
678
 
610
681
      return(SCH_ERROR);
611
682
      }
612
683
    }
613
 
  else 
 
684
  else
614
685
    {
615
 
    for (i = 0;i < numclients;i++) 
 
686
    for (i = 0;i < numclients;i++)
616
687
      {
617
688
      if (addr == okclients[i])
618
689
        break;
619
690
      }
620
691
 
621
 
    if (i == numclients) 
 
692
    if (i == numclients)
622
693
      {
623
694
      badconn("unauthorized host");
624
695
 
628
699
      }
629
700
    }
630
701
 
631
 
  if ((connector = socket_to_conn(new_socket)) < 0) 
632
 
    {
633
 
    log_err(errno,id,"socket_to_conn");
634
 
 
635
 
    return(SCH_ERROR);
636
 
    }
637
 
 
638
 
  if (get_4byte(new_socket,&cmd) != 1) 
639
 
    {
640
 
    log_err(errno,id,"get4bytes");
641
 
 
642
 
    return(SCH_ERROR);
643
 
    }
644
 
 
645
 
  return(cmd);
 
702
  if ((connector = socket_to_conn(new_socket)) < 0)
 
703
    {
 
704
    log_err(errno, id, "socket_to_conn");
 
705
 
 
706
    return(SCH_ERROR);
 
707
    }
 
708
 
 
709
  if (get_4byte(new_socket, &cmd) != 1)
 
710
    {
 
711
    log_err(errno, id, "get4bytes");
 
712
 
 
713
    return(SCH_ERROR);
 
714
    }
 
715
 
 
716
  return((int)cmd);
646
717
  }
647
718
 
648
719
 
651
722
 
652
723
/*
653
724
 * lock_out - lock out other daemons from this directory.
 
725
 *
 
726
 * op is either F_WRLCK or F_UNLCK
654
727
 */
655
728
 
656
 
static void lock_out(fds, op)
657
 
        int fds;
658
 
        int op;         /* F_WRLCK  or  F_UNLCK */
659
 
{
660
 
        struct flock flock;
661
 
 
662
 
        flock.l_type   = op;
663
 
        flock.l_whence = SEEK_SET;
664
 
        flock.l_start  = 0;
665
 
        flock.l_len    = 0;     /* whole file */
666
 
        if (fcntl(fds, F_SETLK, &flock) < 0) {
667
 
            (void)strcpy(log_buffer, "pbs_sched: another scheduler running\n");
668
 
            log_err(errno, msg_daemonname, log_buffer);
669
 
            fprintf(stderr, log_buffer);
670
 
            exit (1);
671
 
        }
672
 
}
 
729
static void lock_out(int fds, int op)
 
730
  {
 
731
 
 
732
  struct flock flock;
 
733
 
 
734
  flock.l_type   = op;
 
735
  flock.l_whence = SEEK_SET;
 
736
  flock.l_start  = 0;
 
737
  flock.l_len    = 0; /* whole file */
 
738
 
 
739
  if (fcntl(fds, F_SETLK, &flock) < 0)
 
740
    {
 
741
    (void)strcpy(log_buffer, "pbs_sched: another scheduler running\n");
 
742
    log_err(errno, msg_daemonname, log_buffer);
 
743
    fprintf(stderr, "%s", log_buffer);
 
744
    exit(1);
 
745
    }
 
746
  }
673
747
 
674
748
 
675
749
 
680
754
  char *argv[])
681
755
 
682
756
  {
683
 
        char            *id = "main";
684
 
        struct  hostent *hp;
685
 
        int             go, c, errflg = 0;
686
 
        int             lockfds;
687
 
        int             t = 1;
688
 
        pid_t           pid;
689
 
        char            host[100];
690
 
        char            *homedir = PBS_SERVER_HOME;
691
 
        unsigned int    port;
692
 
        char            *dbfile = "sched_out";
693
 
        struct  sigaction       act;
694
 
        sigset_t        oldsigs;
695
 
        caddr_t         curr_brk = 0;
696
 
        caddr_t         next_brk;
697
 
        extern  char    *optarg;
698
 
        extern  int     optind, opterr;
699
 
        extern  int     rpp_fd;
700
 
        fd_set          fdset;
701
 
        int             schedinit A_((int argc, char **argv));
702
 
        int             schedule A_((int com, int connector));
703
 
 
704
 
        glob_argv = argv;
705
 
        alarm_time = 180;
706
 
 
707
 
        port = get_svrport(PBS_SCHEDULER_SERVICE_NAME, "tcp", 
708
 
                           PBS_SCHEDULER_SERVICE_PORT);
709
 
        pbs_rm_port = get_svrport(PBS_MANAGER_SERVICE_NAME, "tcp",
710
 
                           PBS_MANAGER_SERVICE_PORT);
711
 
 
712
 
        strcpy(pbs_current_user,"Scheduler");
713
 
        msg_daemonname=strdup("pbs_sched");
714
 
 
715
 
        opterr = 0;
716
 
 
717
 
        while ((c = getopt(argc, argv, "L:S:R:d:p:c:a:-:")) != EOF) {
718
 
                switch (c) {
719
 
                case '-':
720
 
                        if ((optarg == NULL) || (optarg[0] == '\0')) {
721
 
                                errflg = 1;
722
 
                        }
723
 
 
724
 
                        if (!strcmp(optarg,"version")) {
725
 
                                fprintf(stderr,"version: %s\n", PACKAGE_VERSION);
726
 
                                exit(0);
727
 
                        } else {
728
 
                                errflg = 1;
729
 
                        }
730
 
                        break;
731
 
                case 'L':
732
 
                        logfile = optarg;
733
 
                        break;
734
 
                case 'S':
735
 
                        port = atoi(optarg);
736
 
                        if (port == 0) {
737
 
                                fprintf(stderr,
738
 
                                        "%s: illegal port\n", optarg);
739
 
                                errflg = 1;
740
 
                        }
741
 
                        break;
742
 
                case 'R':
743
 
                        if ((pbs_rm_port = atoi(optarg)) == 0) {
744
 
                            (void)fprintf(stderr, "%s: bad -R %s\n",
745
 
                                argv[0], optarg);
746
 
                        return 1;
747
 
                        }
748
 
                        break;
749
 
                case 'd':
750
 
                        homedir = optarg;
751
 
                        break;
752
 
                case 'p':
753
 
                        dbfile = optarg;
754
 
                        break;
755
 
                case 'c':
756
 
                        configfile = optarg;
757
 
                        break;
758
 
                case 'a':
759
 
                        alarm_time = atoi(optarg);
760
 
                        if (alarm_time == 0) {
761
 
                                fprintf(stderr,
762
 
                                        "%s: bad alarm time\n", optarg);
763
 
                                errflg = 1;
764
 
                        }
765
 
                        break;
766
 
                case '?':
767
 
                        errflg = 1;
768
 
                        break;
769
 
                }
770
 
        }
771
 
        if (errflg) {
772
 
                fprintf(stderr, "usage: %s %s\n", argv[0], usage);
773
 
                exit(1);
774
 
        }
 
757
  char  *id = "main";
 
758
 
 
759
  struct hostent *hp;
 
760
  int  go, c, errflg = 0;
 
761
  int  lockfds;
 
762
  int  t = 1;
 
763
  pid_t  pid;
 
764
  char  host[100];
 
765
  char  *homedir = PBS_SERVER_HOME;
 
766
  unsigned int port;
 
767
  char  *dbfile = "sched_out";
 
768
 
 
769
  struct sigaction act;
 
770
  sigset_t oldsigs;
 
771
  caddr_t curr_brk = 0;
 
772
  caddr_t next_brk;
 
773
  extern char *optarg;
 
774
  extern int optind, opterr;
 
775
  extern int rpp_fd;
 
776
  fd_set fdset;
 
777
 
 
778
  int  schedinit A_((int argc, char **argv));
 
779
  int  schedule A_((int com, int connector));
 
780
 
 
781
  glob_argv = argv;
 
782
  alarm_time = 180;
 
783
 
 
784
  /* The following is code to reduce security risks                */
 
785
  /* move this to a place where nss_ldap doesn't hold a socket yet */
 
786
 
 
787
  c = sysconf(_SC_OPEN_MAX);
 
788
 
 
789
  while (--c > 2)
 
790
    (void)close(c); /* close any file desc left open by parent */
 
791
 
 
792
  port = get_svrport(PBS_SCHEDULER_SERVICE_NAME, "tcp",
 
793
                     PBS_SCHEDULER_SERVICE_PORT);
 
794
 
 
795
  pbs_rm_port = get_svrport(PBS_MANAGER_SERVICE_NAME, "tcp",
 
796
                            PBS_MANAGER_SERVICE_PORT);
 
797
 
 
798
  strcpy(pbs_current_user, "Scheduler");
 
799
 
 
800
  msg_daemonname = strdup("pbs_sched");
 
801
 
 
802
  opterr = 0;
 
803
 
 
804
  while ((c = getopt(argc, argv, "L:S:R:d:p:c:a:-:")) != EOF)
 
805
    {
 
806
    switch (c)
 
807
      {
 
808
 
 
809
      case '-':
 
810
 
 
811
        if ((optarg == NULL) || (optarg[0] == '\0'))
 
812
          {
 
813
          errflg = 1;
 
814
          }
 
815
 
 
816
        if (!strcmp(optarg, "version"))
 
817
          {
 
818
          fprintf(stderr, "version: %s\n", PACKAGE_VERSION);
 
819
          exit(0);
 
820
          }
 
821
        else
 
822
          {
 
823
          errflg = 1;
 
824
          }
 
825
 
 
826
        break;
 
827
 
 
828
      case 'L':
 
829
        logfile = optarg;
 
830
        break;
 
831
 
 
832
      case 'S':
 
833
        port = atoi(optarg);
 
834
 
 
835
        if (port == 0)
 
836
          {
 
837
          fprintf(stderr,
 
838
                  "%s: illegal port\n", optarg);
 
839
          errflg = 1;
 
840
          }
 
841
 
 
842
        break;
 
843
 
 
844
      case 'R':
 
845
 
 
846
        if ((pbs_rm_port = atoi(optarg)) == 0)
 
847
          {
 
848
          (void)fprintf(stderr, "%s: bad -R %s\n",
 
849
                        argv[0], optarg);
 
850
          return 1;
 
851
          }
 
852
 
 
853
        break;
 
854
 
 
855
      case 'd':
 
856
        homedir = optarg;
 
857
        break;
 
858
 
 
859
      case 'p':
 
860
        dbfile = optarg;
 
861
        break;
 
862
 
 
863
      case 'c':
 
864
        configfile = optarg;
 
865
        break;
 
866
 
 
867
      case 'a':
 
868
        alarm_time = atoi(optarg);
 
869
 
 
870
        if (alarm_time == 0)
 
871
          {
 
872
          fprintf(stderr,
 
873
                  "%s: bad alarm time\n", optarg);
 
874
          errflg = 1;
 
875
          }
 
876
 
 
877
        break;
 
878
 
 
879
      case '?':
 
880
        errflg = 1;
 
881
        break;
 
882
      }
 
883
    }
 
884
 
 
885
  if (errflg)
 
886
    {
 
887
    fprintf(stderr, "usage: %s %s\n", argv[0], usage);
 
888
    exit(1);
 
889
    }
775
890
 
776
891
#ifndef DEBUG
777
 
        if ((geteuid() != 0) || (getuid() != 0)) {
778
 
                fprintf(stderr, "%s: Must be run by root\n", argv[0]);
779
 
                return (1);
780
 
        }
 
892
  if ((geteuid() != 0) || (getuid() != 0))
 
893
    {
 
894
    fprintf(stderr, "%s: Must be run by root\n", argv[0]);
 
895
    return (1);
 
896
    }
 
897
 
781
898
#endif        /* DEBUG */
782
899
 
783
 
        /* Save the original working directory for "restart" */
784
 
        if ((oldpath = getcwd((char *)NULL, MAXPATHLEN)) == NULL) {
785
 
                fprintf(stderr, "cannot get current working directory\n");
786
 
                exit(1);
787
 
        }
 
900
  /* Save the original working directory for "restart" */
 
901
  if ((oldpath = getcwd((char *)NULL, MAXPATHLEN)) == NULL)
 
902
    {
 
903
    fprintf(stderr, "cannot get current working directory\n");
 
904
    exit(1);
 
905
    }
788
906
 
789
 
        (void)sprintf(log_buffer, "%s/sched_priv", homedir);
 
907
  (void)sprintf(log_buffer, "%s/sched_priv", homedir);
790
908
#if !defined(DEBUG) && !defined(NO_SECURITY_CHECK)
791
 
        c  = chk_file_sec(log_buffer, 1, 0, S_IWGRP|S_IWOTH, 1);
792
 
        c |= chk_file_sec(PBS_ENVIRON, 0, 0, S_IWGRP|S_IWOTH, 0);
793
 
        if (c != 0) exit(1);
 
909
  c  = chk_file_sec(log_buffer, 1, 0, S_IWGRP | S_IWOTH, 1, NULL);
 
910
  c |= chk_file_sec(PBS_ENVIRON, 0, 0, S_IWGRP | S_IWOTH, 0, NULL);
 
911
 
 
912
  if (c != 0) exit(1);
 
913
 
794
914
#endif  /* not DEBUG and not NO_SECURITY_CHECK */
795
 
        if (chdir(log_buffer) == -1) {
796
 
                perror("chdir");
797
 
                exit(1);
798
 
        }
799
 
        (void)sprintf(path_log,   "%s/sched_logs", homedir);
800
 
 
801
 
 
802
 
        /* The following is code to reduce security risks                */
803
 
        /* start out with standard umask, system resource limit infinite */
804
 
 
805
 
        umask(022);
806
 
        if (setup_env(PBS_ENVIRON)==-1)
807
 
                exit(1);
808
 
        c = getgid();
809
 
        (void)setgroups(1, (gid_t *)&c);        /* secure suppl. groups */
810
 
        c = sysconf(_SC_OPEN_MAX);
811
 
        while (--c > 2)
812
 
                (void)close(c); /* close any file desc left open by parent */
 
915
  if (chdir(log_buffer) == -1)
 
916
    {
 
917
    perror("chdir");
 
918
    exit(1);
 
919
    }
 
920
 
 
921
  (void)sprintf(path_log,   "%s/sched_logs", homedir);
 
922
  (void)sprintf(path_acct,   "%s/%s", log_buffer, PBS_ACCT);
 
923
 
 
924
 
 
925
  /* The following is code to reduce security risks                */
 
926
  /* start out with standard umask, system resource limit infinite */
 
927
 
 
928
  umask(022);
 
929
 
 
930
  if (setup_env(PBS_ENVIRON) == -1)
 
931
    exit(1);
 
932
 
 
933
  c = getgid();
 
934
 
 
935
  (void)setgroups(1, (gid_t *)&c); /* secure suppl. groups */
813
936
 
814
937
#ifndef DEBUG
815
938
#ifdef _CRAY
816
 
        (void)limit(C_JOB,      0, L_CPROC, 0);
817
 
        (void)limit(C_JOB,      0, L_CPU,   0);
818
 
        (void)limit(C_JOBPROCS, 0, L_CPU,   0);
819
 
        (void)limit(C_PROC,     0, L_FD,  255);
820
 
        (void)limit(C_JOB,      0, L_FSBLK, 0);
821
 
        (void)limit(C_JOBPROCS, 0, L_FSBLK, 0);
822
 
        (void)limit(C_JOB,      0, L_MEM  , 0);
823
 
        (void)limit(C_JOBPROCS, 0, L_MEM  , 0);
824
 
#else   /* not  _CRAY */
825
 
        {
826
 
        struct rlimit rlimit;
827
 
 
828
 
        rlimit.rlim_cur = RLIM_INFINITY;
829
 
        rlimit.rlim_max = RLIM_INFINITY;
830
 
        (void)setrlimit(RLIMIT_CPU,   &rlimit);
831
 
        (void)setrlimit(RLIMIT_FSIZE, &rlimit);
832
 
        (void)setrlimit(RLIMIT_DATA,  &rlimit);
833
 
        (void)setrlimit(RLIMIT_STACK, &rlimit);
 
939
  (void)limit(C_JOB,      0, L_CPROC, 0);
 
940
 
 
941
  (void)limit(C_JOB,      0, L_CPU,   0);
 
942
 
 
943
  (void)limit(C_JOBPROCS, 0, L_CPU,   0);
 
944
 
 
945
  (void)limit(C_PROC,     0, L_FD,  255);
 
946
 
 
947
  (void)limit(C_JOB,      0, L_FSBLK, 0);
 
948
 
 
949
  (void)limit(C_JOBPROCS, 0, L_FSBLK, 0);
 
950
 
 
951
  (void)limit(C_JOB,      0, L_MEM  , 0);
 
952
 
 
953
  (void)limit(C_JOBPROCS, 0, L_MEM  , 0);
 
954
 
 
955
#else /* not  _CRAY */
 
956
    {
 
957
 
 
958
    struct rlimit rlimit;
 
959
 
 
960
    rlimit.rlim_cur = RLIM_INFINITY;
 
961
    rlimit.rlim_max = RLIM_INFINITY;
 
962
    (void)setrlimit(RLIMIT_CPU,   &rlimit);
 
963
    (void)setrlimit(RLIMIT_FSIZE, &rlimit);
 
964
    (void)setrlimit(RLIMIT_DATA,  &rlimit);
 
965
    (void)setrlimit(RLIMIT_STACK, &rlimit);
834
966
#ifdef  RLIMIT_RSS
835
 
        (void)setrlimit(RLIMIT_RSS  , &rlimit);
 
967
    (void)setrlimit(RLIMIT_RSS  , &rlimit);
836
968
#endif  /* RLIMIT_RSS */
837
969
#ifdef  RLIMIT_VMEM
838
 
        (void)setrlimit(RLIMIT_VMEM  , &rlimit);
 
970
    (void)setrlimit(RLIMIT_VMEM  , &rlimit);
839
971
#endif  /* RLIMIT_VMEM */
840
 
        }
841
 
#endif  /* not _CRAY */
842
 
#endif  /* DEBUG */
843
 
 
844
 
        if (log_open(logfile, path_log) == -1) {
845
 
                fprintf(stderr, "%s: logfile could not be opened\n", argv[0]);
846
 
                exit(1);
847
 
        }
848
 
 
849
 
        if (gethostname(host, sizeof(host)) == -1) {
850
 
                log_err(errno, id, "gethostname");
851
 
                die(0);
852
 
        }
853
 
        if ((hp =gethostbyname(host)) == NULL) {
854
 
                log_err(errno, id, "gethostbyname");
855
 
                die(0);
856
 
        }
857
 
        if ((server_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
858
 
                log_err(errno, id, "socket");
859
 
                die(0);
860
 
        }
861
 
        if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR,
862
 
                        (char *)&t, sizeof(t)) == -1) {
863
 
                log_err(errno, id, "setsockopt");
864
 
                die(0);
865
 
        }
866
 
        saddr.sin_family = AF_INET;
867
 
        saddr.sin_port = htons(port);
868
 
        memcpy (&saddr.sin_addr, hp->h_addr, hp->h_length);
869
 
        if (bind (server_sock, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
870
 
                log_err(errno, id, "bind");
871
 
                die(0);
872
 
        }
873
 
        if (listen (server_sock, 5) < 0) {
874
 
                log_err(errno, id, "listen");
875
 
                die(0);
876
 
        }
877
 
 
878
 
        okclients = (pbs_net_t *)calloc(START_CLIENTS, sizeof(pbs_net_t));
879
 
        addclient("localhost");   /* who has permission to call MOM */
880
 
        addclient(host);
881
 
        if (configfile) {
882
 
                if (read_config(configfile) != 0)
883
 
                        die(0);
884
 
        }
885
 
 
886
 
        lockfds = open("sched.lock", O_CREAT | O_TRUNC | O_WRONLY, 0644);
887
 
        if (lockfds < 0) {
888
 
                log_err(errno, id, "open lock file");
889
 
                exit(1);
890
 
        }
891
 
        lock_out(lockfds, F_WRLCK);
892
 
 
893
 
        fullresp(0);
894
 
        if (sigemptyset(&allsigs) == -1) {
895
 
                perror("sigemptyset");
896
 
                exit(1);
897
 
        }
898
 
        if (sigprocmask(SIG_SETMASK, &allsigs, NULL) == -1) {   /* unblock */
899
 
                perror("sigprocmask");
900
 
                exit(1);
901
 
        }
902
 
        act.sa_flags = 0;
903
 
        sigaddset(&allsigs, SIGHUP);    /* remember to block these */
904
 
        sigaddset(&allsigs, SIGINT);    /* during critical sections */
905
 
        sigaddset(&allsigs, SIGTERM);   /* so we don't get confused */
906
 
        act.sa_mask = allsigs;
907
 
 
908
 
        act.sa_handler = restart;       /* do a restart on SIGHUP */
909
 
        sigaction(SIGHUP, &act, NULL);
910
 
 
911
 
        act.sa_handler = toolong;       /* handle an alarm call */
912
 
        sigaction(SIGALRM, &act, NULL);
913
 
 
914
 
        act.sa_handler = die;           /* bite the biscuit for all following */
915
 
        sigaction(SIGINT, &act, NULL);
916
 
        sigaction(SIGTERM, &act, NULL);
917
 
 
918
 
        /*
919
 
         * Catch these signals to ensure we core dump even if
920
 
         * our rlimit for core dumps is set to 0 initially.
921
 
         * 
922
 
         * Chris Samuel - VPAC
923
 
         * csamuel@vpac.org - 29th July 2003
924
 
         * 
925
 
         * Now conditional on the PBSCOREDUMP environment variable
926
 
         */
927
 
 
928
 
        if (getenv("PBSCOREDUMP"))
929
 
          {
930
 
          act.sa_handler = catch_abort;   /* make sure we core dump */
931
 
 
932
 
          sigaction(SIGSEGV,&act, NULL);
933
 
          sigaction(SIGBUS, &act, NULL);
934
 
          sigaction(SIGFPE, &act, NULL);
935
 
          sigaction(SIGILL, &act, NULL);
936
 
          sigaction(SIGTRAP,&act, NULL);
937
 
          sigaction(SIGSYS, &act, NULL);
938
 
          }
939
 
 
940
 
        /*
941
 
         *  Local initialization stuff
942
 
         */
943
 
 
944
 
        if (schedinit(argc, argv)) {
945
 
                (void) sprintf(log_buffer,
946
 
                        "local initialization failed, terminating");
947
 
                log_record(PBSEVENT_SYSTEM,PBS_EVENTCLASS_SERVER,id,log_buffer);
948
 
                exit(1);
949
 
        }
950
 
 
951
 
        if (getenv("PBSDEBUG") == NULL)
952
 
          {
953
 
          lock_out(lockfds,F_UNLCK);
954
 
 
955
 
          if ((pid = fork()) == -1) 
956
 
            {     /* error on fork */
957
 
            perror("fork");
958
 
 
959
 
            exit(1);
960
 
            }
961
 
          else if (pid > 0)               /* parent exits */
962
 
            {
963
 
            exit(0);
964
 
            }
965
 
 
966
 
          if ((pid = setsid()) == -1) 
967
 
            {
968
 
            perror("setsid");
969
 
 
970
 
            exit(1);
971
 
            }
972
 
 
973
 
          lock_out(lockfds, F_WRLCK);
974
 
 
975
 
          freopen(dbfile, "a", stdout);
976
 
 
977
 
          setvbuf(stdout, NULL, _IOLBF, 0);
978
 
 
979
 
          dup2(fileno(stdout), fileno(stderr));
980
 
          }
981
 
        else
982
 
          {
983
 
          setvbuf(stdout, NULL, _IOLBF, 0);
984
 
          setvbuf(stderr, NULL, _IOLBF, 0);
985
 
 
986
 
          pid = getpid();
987
 
          }
988
 
 
989
 
        freopen("/dev/null", "r", stdin);
990
 
 
991
 
        /* write scheduler's pid into lockfile */
992
 
 
993
 
        (void)sprintf(log_buffer, "%ld\n", (long)pid);
994
 
 
995
 
        if (write(lockfds, log_buffer, strlen(log_buffer)+1) != (ssize_t)(strlen(log_buffer)+1))
996
 
          {
997
 
          perror("writing to lockfile");
998
 
 
999
 
          exit(1);
1000
 
          }
 
972
    }
 
973
#endif /* not _CRAY */
 
974
#endif /* DEBUG */
 
975
 
 
976
  if (log_open(logfile, path_log) == -1)
 
977
    {
 
978
    fprintf(stderr, "%s: logfile could not be opened\n", argv[0]);
 
979
    exit(1);
 
980
    }
 
981
 
 
982
  if (gethostname(host, sizeof(host)) == -1)
 
983
    {
 
984
    log_err(errno, id, "gethostname");
 
985
    die(0);
 
986
    }
 
987
 
 
988
  if ((hp = gethostbyname(host)) == NULL)
 
989
    {
 
990
    log_err(errno, id, "gethostbyname");
 
991
    die(0);
 
992
    }
 
993
 
 
994
  if ((server_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
 
995
    {
 
996
    log_err(errno, id, "socket");
 
997
    die(0);
 
998
    }
 
999
 
 
1000
  if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR,
 
1001
                 (char *)&t, sizeof(t)) == -1)
 
1002
    {
 
1003
    log_err(errno, id, "setsockopt");
 
1004
    die(0);
 
1005
    }
 
1006
 
 
1007
  saddr.sin_family = AF_INET;
 
1008
 
 
1009
  saddr.sin_port = htons(port);
 
1010
  memcpy(&saddr.sin_addr, hp->h_addr, hp->h_length);
 
1011
 
 
1012
  if (bind(server_sock, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
 
1013
    {
 
1014
    log_err(errno, id, "bind");
 
1015
    die(0);
 
1016
    }
 
1017
 
 
1018
  if (listen(server_sock, 5) < 0)
 
1019
    {
 
1020
    log_err(errno, id, "listen");
 
1021
    die(0);
 
1022
    }
 
1023
 
 
1024
  okclients = (pbs_net_t *)calloc(START_CLIENTS, sizeof(pbs_net_t));
 
1025
 
 
1026
  addclient("localhost");   /* who has permission to call MOM */
 
1027
  addclient(host);
 
1028
 
 
1029
  if (configfile)
 
1030
    {
 
1031
    if (read_config(configfile) != 0)
 
1032
      die(0);
 
1033
    }
 
1034
 
 
1035
  lockfds = open("sched.lock", O_CREAT | O_TRUNC | O_WRONLY, 0644);
 
1036
 
 
1037
  if (lockfds < 0)
 
1038
    {
 
1039
    log_err(errno, id, "open lock file");
 
1040
    exit(1);
 
1041
    }
 
1042
 
 
1043
  lock_out(lockfds, F_WRLCK);
 
1044
 
 
1045
  fullresp(0);
 
1046
 
 
1047
  if (sigemptyset(&allsigs) == -1)
 
1048
    {
 
1049
    perror("sigemptyset");
 
1050
    exit(1);
 
1051
    }
 
1052
 
 
1053
  if (sigprocmask(SIG_SETMASK, &allsigs, NULL) == -1)   /* unblock */
 
1054
    {
 
1055
    perror("sigprocmask");
 
1056
    exit(1);
 
1057
    }
 
1058
 
 
1059
  act.sa_flags = 0;
 
1060
 
 
1061
  sigaddset(&allsigs, SIGHUP);    /* remember to block these */
 
1062
  sigaddset(&allsigs, SIGINT);    /* during critical sections */
 
1063
  sigaddset(&allsigs, SIGTERM);   /* so we don't get confused */
 
1064
  act.sa_mask = allsigs;
 
1065
 
 
1066
  act.sa_handler = restart;       /* do a restart on SIGHUP */
 
1067
  sigaction(SIGHUP, &act, NULL);
 
1068
 
 
1069
  act.sa_handler = toolong; /* handle an alarm call */
 
1070
  sigaction(SIGALRM, &act, NULL);
 
1071
 
 
1072
  act.sa_handler = die;           /* bite the biscuit for all following */
 
1073
  sigaction(SIGINT, &act, NULL);
 
1074
  sigaction(SIGTERM, &act, NULL);
 
1075
 
 
1076
  /*
 
1077
   * Catch these signals to ensure we core dump even if
 
1078
   * our rlimit for core dumps is set to 0 initially.
 
1079
   *
 
1080
   * Chris Samuel - VPAC
 
1081
   * csamuel@vpac.org - 29th July 2003
 
1082
   *
 
1083
   * Now conditional on the PBSCOREDUMP environment variable
 
1084
   */
 
1085
 
 
1086
  if (getenv("PBSCOREDUMP"))
 
1087
    {
 
1088
    act.sa_handler = catch_abort;   /* make sure we core dump */
 
1089
 
 
1090
    sigaction(SIGSEGV, &act, NULL);
 
1091
    sigaction(SIGBUS, &act, NULL);
 
1092
    sigaction(SIGFPE, &act, NULL);
 
1093
    sigaction(SIGILL, &act, NULL);
 
1094
    sigaction(SIGTRAP, &act, NULL);
 
1095
    sigaction(SIGSYS, &act, NULL);
 
1096
    }
 
1097
 
 
1098
  /*
 
1099
   *  Local initialization stuff
 
1100
   */
 
1101
 
 
1102
  if (schedinit(argc, argv))
 
1103
    {
 
1104
    (void) sprintf(log_buffer,
 
1105
                   "local initialization failed, terminating");
 
1106
    log_record(PBSEVENT_SYSTEM, PBS_EVENTCLASS_SERVER, id, log_buffer);
 
1107
    exit(1);
 
1108
    }
 
1109
 
 
1110
  if (getenv("PBSDEBUG") == NULL)
 
1111
    {
 
1112
    lock_out(lockfds, F_UNLCK);
 
1113
 
 
1114
    if ((pid = fork()) == -1)
 
1115
      {
 
1116
      /* error on fork */
 
1117
      perror("fork");
 
1118
 
 
1119
      exit(1);
 
1120
      }
 
1121
    else if (pid > 0)               /* parent exits */
 
1122
      {
 
1123
      exit(0);
 
1124
      }
 
1125
 
 
1126
    if ((pid = setsid()) == -1)
 
1127
      {
 
1128
      perror("setsid");
 
1129
 
 
1130
      exit(1);
 
1131
      }
 
1132
 
 
1133
    lock_out(lockfds, F_WRLCK);
 
1134
 
 
1135
    if (!freopen(dbfile, "a", stdout)) {
 
1136
      perror("lockout, attempting to freopen dbfile");
 
1137
      exit(1);
 
1138
    }
 
1139
 
 
1140
    setvbuf(stdout, NULL, _IOLBF, 0);
 
1141
 
 
1142
    dup2(fileno(stdout), fileno(stderr));
 
1143
    }
 
1144
  else
 
1145
    {
 
1146
    setvbuf(stdout, NULL, _IOLBF, 0);
 
1147
    setvbuf(stderr, NULL, _IOLBF, 0);
 
1148
 
 
1149
    pid = getpid();
 
1150
    }
 
1151
 
 
1152
  if (!freopen("/dev/null", "r", stdin)) {
 
1153
    perror("lockout, attempting to freopen /dev/null");
 
1154
    exit(1);
 
1155
  }
 
1156
 
 
1157
  /* write scheduler's pid into lockfile */
 
1158
 
 
1159
  (void)sprintf(log_buffer, "%ld\n", (long)pid);
 
1160
 
 
1161
  if (write(lockfds, log_buffer, strlen(log_buffer) + 1) != (ssize_t)(strlen(log_buffer) + 1))
 
1162
    {
 
1163
    perror("writing to lockfile");
 
1164
 
 
1165
    exit(1);
 
1166
    }
1001
1167
 
1002
1168
#if (PLOCK_DAEMONS & 2)
1003
 
        (void)plock(PROCLOCK);  /* lock daemon into memory */
 
1169
  (void)plock(PROCLOCK); /* lock daemon into memory */
 
1170
 
1004
1171
#endif
1005
1172
 
1006
 
        sprintf(log_buffer, "%s startup pid %ld", argv[0], (long)pid);
1007
 
        log_record(PBSEVENT_SYSTEM, PBS_EVENTCLASS_SERVER, id, log_buffer);
1008
 
 
1009
 
        FD_ZERO(&fdset);
1010
 
        for (go=1; go;) {
1011
 
                int     cmd;
1012
 
 
1013
 
                if (rpp_fd != -1)
1014
 
                        FD_SET(rpp_fd, &fdset);
1015
 
                FD_SET(server_sock, &fdset);
1016
 
                if (select(FD_SETSIZE, &fdset, NULL, NULL, NULL) == -1) {
1017
 
                        if (errno != EINTR) {
1018
 
                                log_err(errno, id, "select");
1019
 
                                die(0);
1020
 
                        }
1021
 
                        continue;
1022
 
                }
1023
 
 
1024
 
                if (rpp_fd != -1 && FD_ISSET(rpp_fd, &fdset)) {
1025
 
                        if (rpp_io() == -1)
1026
 
                                log_err(errno, id, "rpp_io");
1027
 
                }
1028
 
                if (!FD_ISSET(server_sock, &fdset))
1029
 
                        continue;
1030
 
 
1031
 
                cmd = server_command();
1032
 
                if (sigprocmask(SIG_BLOCK, &allsigs, &oldsigs) == -1)
1033
 
                        log_err(errno, id, "sigprocmaskSIG_BLOCK)");
1034
 
 
1035
 
                alarm(alarm_time);
1036
 
                if (schedule(cmd, connector))   /* magic happens here */
1037
 
                        go = 0;
1038
 
                alarm(0);
1039
 
 
1040
 
                if (connector >= 0 && server_disconnect(connector)) {
1041
 
                        log_err(errno, id, "server_disconnect");
1042
 
                        die(0);
1043
 
                }
1044
 
                next_brk = (caddr_t)sbrk(0);
1045
 
                if (next_brk > curr_brk) {
1046
 
                        sprintf(log_buffer, "brk point %ld", (long)next_brk);
1047
 
                        log_record(PBSEVENT_DEBUG, PBS_EVENTCLASS_SERVER,
1048
 
                                id, log_buffer);
1049
 
                        curr_brk = next_brk;
1050
 
                }
1051
 
                if (sigprocmask(SIG_SETMASK, &oldsigs, NULL) == -1)
1052
 
                        log_err(errno, id, "sigprocmask(SIG_SETMASK)");
1053
 
        }
1054
 
 
1055
 
        sprintf(log_buffer, "%s normal finish pid %ld", argv[0], (long)pid);
1056
 
        log_record(PBSEVENT_SYSTEM, PBS_EVENTCLASS_SERVER, id, log_buffer);
1057
 
 
1058
 
        (void)close(server_sock);
1059
 
        exit(0);
1060
 
}
 
1173
  sprintf(log_buffer, "%s startup pid %ld", argv[0], (long)pid);
 
1174
 
 
1175
  log_record(PBSEVENT_SYSTEM, PBS_EVENTCLASS_SERVER, id, log_buffer);
 
1176
 
 
1177
  FD_ZERO(&fdset);
 
1178
 
 
1179
  for (go = 1;go;)
 
1180
    {
 
1181
    int cmd;
 
1182
 
 
1183
    if (rpp_fd != -1)
 
1184
      FD_SET(rpp_fd, &fdset);
 
1185
 
 
1186
    FD_SET(server_sock, &fdset);
 
1187
 
 
1188
    if (select(FD_SETSIZE, &fdset, NULL, NULL, NULL) == -1)
 
1189
      {
 
1190
      if (errno != EINTR)
 
1191
        {
 
1192
        log_err(errno, id, "select");
 
1193
        die(0);
 
1194
        }
 
1195
 
 
1196
      continue;
 
1197
      }
 
1198
 
 
1199
    if (rpp_fd != -1 && FD_ISSET(rpp_fd, &fdset))
 
1200
      {
 
1201
      if (rpp_io() == -1)
 
1202
        log_err(errno, id, "rpp_io");
 
1203
      }
 
1204
 
 
1205
    if (!FD_ISSET(server_sock, &fdset))
 
1206
      continue;
 
1207
 
 
1208
    cmd = server_command();
 
1209
 
 
1210
    if (sigprocmask(SIG_BLOCK, &allsigs, &oldsigs) == -1)
 
1211
      log_err(errno, id, "sigprocmaskSIG_BLOCK)");
 
1212
 
 
1213
    alarm(alarm_time);
 
1214
 
 
1215
    if (schedule(cmd, connector)) /* magic happens here */
 
1216
      go = 0;
 
1217
 
 
1218
    alarm(0);
 
1219
 
 
1220
    if (connector >= 0 && server_disconnect(connector))
 
1221
      {
 
1222
      log_err(errno, id, "server_disconnect");
 
1223
      die(0);
 
1224
      }
 
1225
 
 
1226
    next_brk = (caddr_t)sbrk(0);
 
1227
 
 
1228
    if (next_brk > curr_brk)
 
1229
      {
 
1230
      sprintf(log_buffer, "brk point %ld", (long)next_brk);
 
1231
      log_record(PBSEVENT_DEBUG, PBS_EVENTCLASS_SERVER,
 
1232
                 id, log_buffer);
 
1233
      curr_brk = next_brk;
 
1234
      }
 
1235
 
 
1236
    if (sigprocmask(SIG_SETMASK, &oldsigs, NULL) == -1)
 
1237
      log_err(errno, id, "sigprocmask(SIG_SETMASK)");
 
1238
    }
 
1239
 
 
1240
  sprintf(log_buffer, "%s normal finish pid %ld",
 
1241
 
 
1242
          argv[0],
 
1243
          (long)pid);
 
1244
 
 
1245
  log_record(PBSEVENT_SYSTEM, PBS_EVENTCLASS_SERVER, id, log_buffer);
 
1246
 
 
1247
  close(server_sock);
 
1248
 
 
1249
  exit(0);
 
1250
  }  /* END main() */