~ubuntu-branches/ubuntu/trusty/net-snmp/trusty

« back to all changes in this revision

Viewing changes to agent/mibgroup/util_funcs.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2007-12-08 14:59:50 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20071208145950-u1tykhpw56nyzqik
Tags: 5.4.1~dfsg-4ubuntu1
* Merge from debian unstable.
* Remaining Ubuntu changes:
  - Remove stop links from rc0 and rc6
  - Munge Maintainer field as per spec.
* Ubuntu changes dropped:
  - Symlink common files between the packages, CDBS ought to handle that
    for us automatically.
* The latest Debian changes has dropped history from the changelog. Slot in
  the Ubuntu changes as best I can. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
104
104
#endif
105
105
 
106
106
 
107
 
#ifdef EXCACHETIME
 
107
#ifdef NETSNMP_EXCACHETIME
108
108
static long     cachetime;
109
109
#endif
110
110
 
117
117
    exit(var);
118
118
}
119
119
 
 
120
/** deprecated, use netsnmp_mktemp instead */
120
121
const char *
121
122
make_tempfile(void)
122
123
{
221
222
void
222
223
wait_on_exec(struct extensible *ex)
223
224
{
224
 
#ifndef EXCACHETIME
 
225
#if defined(WIN32) && !defined (mingw32)
 
226
  int rc;
 
227
  if (ex->tid != 0 && ex->pid != 0) {
 
228
    HANDLE hThread = (HANDLE) ex->tid;
 
229
    HANDLE hProcess = (HANDLE) ex->pid;
 
230
    rc = WaitForSingleObject(hProcess, NETSNMP_TIMEOUT_WAITFORSINGLEOBJECT);
 
231
    DEBUGMSGT(("exec:wait_on_exec","WaitForSingleObject rc=(%d)\n",rc ));
 
232
    rc = CloseHandle( hThread );
 
233
    DEBUGMSGT(("exec:wait_on_exec","CloseHandle hThread=(%d)\n",rc ));
 
234
    rc = CloseHandle( hProcess );
 
235
    DEBUGMSGT(("exec:wait_on_exec","CloseHandle hProcess=(%d)\n",rc ));
 
236
    ex->pid = 0;
 
237
    ex->tid = 0;
 
238
  }
 
239
#else
 
240
#ifndef NETSNMP_EXCACHETIME
225
241
    if (ex->pid && waitpid(ex->pid, &ex->result, 0) < 0) {
226
242
        setPerrorstatus("waitpid");
227
243
    }
228
244
    ex->pid = 0;
229
 
#endif
 
245
#endif  /* NETSNMP_EXCACHETIME */
 
246
#endif  /* WIN32 */
230
247
}
231
248
 
232
249
#define MAXARGS 30
236
253
{
237
254
#if HAVE_EXECV
238
255
    char            cachefile[STRMAX];
239
 
    char            cache[MAXCACHESIZE];
 
256
    char            cache[NETSNMP_MAXCACHESIZE];
240
257
    ssize_t         cachebytes;
241
258
    int             cfd;
242
 
#ifdef EXCACHETIME
 
259
#ifdef NETSNMP_EXCACHETIME
243
260
    long            curtime;
244
261
    static char     lastcmd[STRMAX];
245
262
    static int      lastresult;
247
264
 
248
265
    DEBUGMSGTL(("exec:get_exec_output","calling %s\n", ex->command));
249
266
 
250
 
    sprintf(cachefile, "%s/%s", get_persistent_directory(), CACHEFILE);
251
 
#ifdef EXCACHETIME
 
267
    sprintf(cachefile, "%s/%s", get_persistent_directory(), NETSNMP_CACHEFILE);
 
268
#ifdef NETSNMP_EXCACHETIME
252
269
    curtime = time(NULL);
253
 
    if (curtime > (cachetime + EXCACHETIME) ||
 
270
    if (curtime > (cachetime + NETSNMP_EXCACHETIME) ||
254
271
        strcmp(ex->command, lastcmd) != 0) {
255
272
        strcpy(lastcmd, ex->command);
256
273
        cachetime = curtime;
257
274
#endif
258
275
 
259
 
        cachebytes = MAXCACHESIZE;
 
276
        cachebytes = NETSNMP_MAXCACHESIZE;
260
277
        ex->result = run_exec_command( ex->command, NULL, cache, &cachebytes );
261
278
 
262
279
        unlink(cachefile);
272
289
        if (cachebytes > 0)
273
290
            write(cfd, (void *) cache, cachebytes);
274
291
        close(cfd);
275
 
#ifdef EXCACHETIME
 
292
#ifdef NETSNMP_EXCACHETIME
276
293
        lastresult = ex->result;
277
294
    } else {
278
295
        ex->result = lastresult;
361
378
    
362
379
    /* Set global child process handle */
363
380
    ex->pid = (int)pi.hProcess;
 
381
    ex->tid = (int)pi.hThread;
364
382
 
365
383
    /* Close pipe handles to make sure that no handles to the write end of the
366
384
     * output pipe are maintained in this process or else the pipe will
381
399
    return -1;
382
400
#endif
383
401
}
384
 
 
385
402
int
386
403
get_exec_pipes(char *cmd, int *fdIn, int *fdOut, int *pid)
387
404
{
 
405
/*      Alexander Prömel, alexander@proemel.de 08/24/2006
 
406
        The following code, is tested on picotux rev. 1.01.
 
407
        I think, it will be better to put the named pipes, into /var/run or make it selectable via CONFIG file.
 
408
        If the pipe file already exist, the creation will fail.
 
409
        I put the pipes into /flash, the pipepath has to change in ucd-snmp/pass_persist.c too, if you change it here.
 
410
*/
388
411
#if HAVE_EXECV
 
412
#ifdef __uClinux__ /* HAVE uClinux */
 
413
        int in,out;
 
414
        char fifo_in_path[256];
 
415
        char fifo_out_path[256];
 
416
        pid_t tpid;
 
417
        
 
418
    if ((tpid = vfork()) == 0) { /*temp child*/
 
419
        execve(cmd, NULL,NULL);
 
420
        perror(cmd);
 
421
        exit(1);
 
422
    } else { 
 
423
                if(tpid > 0) {
 
424
                        /*initialize workspace*/
 
425
                        snprintf(fifo_in_path, 256, "/flash/cp_%d", tpid);
 
426
                        snprintf(fifo_out_path, 256, "/flash/pc_%d", tpid);
 
427
 
 
428
                        in = mkfifo(fifo_in_path, S_IRWXU);     /*Create Input Pipe, 700*/
 
429
                        if ( in ) {
 
430
                                perror("parent: inpipe");
 
431
                                exit(0);
 
432
                        }
 
433
                        out = mkfifo(fifo_out_path, S_IRWXU);   /*Create Output Pipe, 700*/
 
434
                        if ( out ) {
 
435
                                perror("parent: outpipe");
 
436
                                exit(0);
 
437
                        }
 
438
                                                
 
439
                        in = open(fifo_in_path,O_RDONLY);       /*open the Input Pipe read Only*/
 
440
                        if(in < 0) {
 
441
                                perror("parent: input");
 
442
                                exit(0);
 
443
                        }
 
444
                        out = open(fifo_out_path,O_WRONLY);     /*open the Output Pipe write Only*/
 
445
                        if(out < 0) {
 
446
                                perror("parent: output");
 
447
                                exit(0);
 
448
                        }
 
449
                        
 
450
                        *fdIn = in;     /*read*/
 
451
                        *fdOut = out;   /*write*/
 
452
                        *pid = tpid;    
 
453
                        return (1);     /* We are returning 0 for error... */
 
454
                } else { /*pid < 0*/
 
455
                        setPerrorstatus("vfork");
 
456
                        return 0;
 
457
                }
 
458
 
 
459
    }
 
460
#else /*HAVE x86*/
389
461
    int             fd[2][2], i, cnt;
390
462
    char            ctmp[STRMAX], *cptr1, *cptr2, argvs[STRMAX], **argv,
391
463
        **aptr;
462
534
        *fdOut = fd[0][1];
463
535
        return (1);             /* We are returning 0 for error... */
464
536
    }
 
537
#endif                          /* uClinux or x86 */
465
538
#endif                          /* !HAVE_EXECV */
466
539
#if defined(WIN32) && !defined (mingw32) && !defined(HAVE_EXECV)
467
540
/* MSVC (MinGW not working but should use this code).  Cygwin already works as it has execv and fork */
598
671
    }
599
672
    tmp = *((long *) var_val);
600
673
    if (tmp == 1 && action == COMMIT) {
601
 
#ifdef EXCACHETIME
 
674
#ifdef NETSNMP_EXCACHETIME
602
675
        cachetime = 0;          /* reset the cache next read */
603
676
#endif
604
677
    }
614
687
                                        NETSNMP_DS_LIB_APPTYPE);
615
688
    snmp_shutdown(name);
616
689
 
 
690
    /*  This signal handler may run with SIGALARM blocked.
 
691
     *  Since the signal mask is preserved accross execv(), we must 
 
692
     *  make sure that SIGALARM is unblocked prior of execv'ing.
 
693
     *  Otherwise SIGALARM will be ignored in the next incarnation
 
694
     *  of snmpd, because the signal is blocked. And thus, the 
 
695
     *  restart doesn't work anymore. 
 
696
     */ 
 
697
#if HAVE_SIGBLOCK 
 
698
    sigsetmask(0);
 
699
#endif 
 
700
 
617
701
    /*
618
702
     * do the exec 
619
703
     */
642
726
#ifdef SIGALRM
643
727
        signal(SIGALRM, restart_doit);
644
728
#endif
645
 
        alarm(RESTARTSLEEP);
 
729
        alarm(NETSNMP_RESTARTSLEEP);
646
730
    }
647
731
    return SNMP_ERR_NOERROR;
648
732
}
856
940
    int             i;
857
941
    char           *init = ptr;
858
942
 
859
 
    if (field == LASTFIELD) {
 
943
    if (field == NETSNMP_LASTFIELD) {
860
944
        /*
861
945
         * skip to end 
862
946
         */