~ubuntu-branches/ubuntu/trusty/erlang/trusty

« back to all changes in this revision

Viewing changes to lib/erl_interface/src/prog/erl_call.c

  • Committer: Bazaar Package Importer
  • Author(s): Clint Byrum
  • Date: 2011-05-05 15:48:43 UTC
  • mfrom: (3.5.13 sid)
  • Revision ID: james.westby@ubuntu.com-20110505154843-0om6ekzg6m7ugj27
Tags: 1:14.b.2-dfsg-3ubuntu1
* Merge from debian unstable.  Remaining changes:
  - Drop libwxgtk2.8-dev build dependency. Wx isn't in main, and not
    supposed to.
  - Drop erlang-wx binary.
  - Drop erlang-wx dependency from -megaco, -common-test, and -reltool, they
    do not really need wx. Also drop it from -debugger; the GUI needs wx,
    but it apparently has CLI bits as well, and is also needed by -megaco,
    so let's keep the package for now.
  - debian/patches/series: Do what I meant, and enable build-options.patch
    instead.
* Additional changes:
  - Drop erlang-wx from -et
* Dropped Changes:
  - patches/pcre-crash.patch: CVE-2008-2371: outer level option with
    alternatives caused crash. (Applied Upstream)
  - fix for ssl certificate verification in newSSL: 
    ssl_cacertfile_fix.patch (Applied Upstream)
  - debian/patches/series: Enable native.patch again, to get stripped beam
    files and reduce the package size again. (build-options is what
    actually accomplished this)
  - Remove build-options.patch on advice from upstream and because it caused
    odd build failures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * %CopyrightBegin%
3
3
 * 
4
 
 * Copyright Ericsson AB 1996-2009. All Rights Reserved.
 
4
 * Copyright Ericsson AB 1996-2011. All Rights Reserved.
5
5
 * 
6
6
 * The contents of this file are subject to the Erlang Public License,
7
7
 * Version 1.1, (the "License"); you may not use this file except in
118
118
static void usage_error(const char *progname, const char *switchname);
119
119
static void usage(const char *progname);
120
120
static int get_module(char **mbuf, char **mname);
121
 
static struct hostent* get_hostent(char *host);
122
121
static int do_connect(ei_cnode *ec, char *nodename, struct call_flags *flags);
123
122
static int read_stdin(char **buf);
124
123
static void split_apply_string(char *str, char **mod, 
125
124
                               char **fun, char **args);
 
125
static void* ei_chk_malloc(size_t size);
 
126
static void* ei_chk_calloc(size_t nmemb, size_t size);
 
127
static void* ei_chk_realloc(void *old, size_t size);
 
128
static char* ei_chk_strdup(char *s);
126
129
 
127
130
 
128
131
/***************************************************************************
132
135
 ***************************************************************************/
133
136
 
134
137
/* FIXME isn't VxWorks to handle arguments differently? */
135
 
/* FIXME check errors from malloc */
136
138
 
137
139
#if !defined(VXWORKS)
138
140
int main(int argc, char *argv[])
165
167
                usage_arg(progname, "-sname ");
166
168
            }
167
169
 
168
 
            flags.node = (char *) malloc(strlen(argv[i+1]) + 1);
169
 
            strcpy(flags.node, argv[i+1]);
 
170
            flags.node = ei_chk_strdup(argv[i+1]);
170
171
            i++;
171
172
            flags.use_long_name = 0;
172
173
        } else if (strcmp(argv[i], "-name") == 0) {  /* -name NAME */
174
175
                usage_arg(progname, "-name ");
175
176
            }
176
177
 
177
 
            flags.node = (char *) malloc(strlen(argv[i+1]) + 1);
178
 
            strcpy(flags.node, argv[i+1]);
 
178
            flags.node = ei_chk_strdup(argv[i+1]);
179
179
            i++;
180
180
            flags.use_long_name = 1;
181
181
        } else {
210
210
                    usage_arg(progname, "-c ");
211
211
                }
212
212
                flags.cookiep = 1;
213
 
                flags.cookie = (char *) malloc(strlen(argv[i+1]) + 1);
214
 
                strcpy(flags.cookie, argv[i+1]);
 
213
                flags.cookie = ei_chk_strdup(argv[i+1]);
215
214
                i++;
216
215
                break;
217
216
            case 'n':
218
217
                if (i+1 >= argc) {
219
218
                    usage_arg(progname, "-n ");
220
219
                }
221
 
                flags.node = (char *) malloc(strlen(argv[i+1]) + 1);
222
 
                strcpy(flags.node, argv[i+1]);
 
220
                flags.node = ei_chk_strdup(argv[i+1]);
223
221
                flags.use_long_name = 1;
224
222
                i++;
225
223
                break;
227
225
                if (i+1 >= argc) {
228
226
                    usage_arg(progname, "-h ");
229
227
                }
230
 
                flags.hidden = (char *) malloc(strlen(argv[i+1]) + 1);
231
 
                strcpy(flags.hidden, argv[i+1]);
 
228
                flags.hidden = ei_chk_strdup(argv[i+1]);
232
229
                i++;
233
230
                break;
234
231
            case 'x':
235
232
                if (i+1 >= argc) {
236
233
                    usage_arg(progname, "-x ");
237
234
                }
238
 
                flags.script = (char *) malloc(strlen(argv[i+1]) + 1);
239
 
                strcpy(flags.script, argv[i+1]);
 
235
                flags.script = ei_chk_strdup(argv[i+1]);
240
236
                i++;
241
237
                break;
242
238
            case 'a':
243
239
                if (i+1 >= argc) {
244
240
                    usage_arg(progname, "-a ");
245
241
                }
246
 
                flags.apply = (char *) malloc(strlen(argv[i+1]) + 1);
247
 
                strcpy(flags.apply, argv[i+1]);
 
242
                flags.apply = ei_chk_strdup(argv[i+1]);
248
243
                i++;
249
244
                break;
250
245
            case '?':
304
299
    if (flags.hidden == NULL) {
305
300
      /* As default we are c17@gethostname */
306
301
      i = flags.randomp ? (time(NULL) % 997) : 17;
307
 
      /* FIXME allocates to small !!! */
308
 
      flags.hidden = (char *) malloc(3 + 2 ); /* c17 or cXYZ */
 
302
      flags.hidden = (char *) ei_chk_malloc(10 + 2 ); /* c17 or cXYZ */
309
303
#if defined(VXWORKS)
310
304
      sprintf(flags.hidden, "c%d",
311
305
          i < 0 ?  (int) taskIdSelf() : i);
330
324
      initWinSock();
331
325
#endif
332
326
 
333
 
      gethostname(h_hostname, EI_MAXHOSTNAMELEN);
 
327
      if (gethostname(h_hostname, EI_MAXHOSTNAMELEN) < 0) {
 
328
          fprintf(stderr,"erl_call: failed to get host name: %d\n", errno);
 
329
          exit(1);
 
330
      }
334
331
      if ((hp = ei_gethostbyname(h_hostname)) == 0) {
335
332
          fprintf(stderr,"erl_call: can't resolve hostname %s\n", h_hostname);
336
333
          exit(1);
337
334
      }
338
 
      /* If shortnames cut of the name at first '.' */
 
335
      /* If shortnames, cut off the name at first '.' */
339
336
      if (flags.use_long_name == 0 && (ct = strchr(hp->h_name, '.')) != NULL) {
340
337
          *ct = '\0';
341
338
      }
342
 
      strcpy(h_hostname, hp->h_name);
 
339
      strncpy(h_hostname, hp->h_name, EI_MAXHOSTNAMELEN);
 
340
      h_hostname[EI_MAXHOSTNAMELEN] = '\0';
343
341
      memcpy(&h_ipadr.s_addr, *hp->h_addr_list, sizeof(struct in_addr));
 
342
      if (strlen(h_alivename) + strlen(h_hostname) + 2 > sizeof(h_nodename)) {
 
343
          fprintf(stderr,"erl_call: hostname too long: %s\n", h_hostname);
 
344
          exit(1);
 
345
      }
344
346
      sprintf(h_nodename, "%s@%s", h_alivename, h_hostname);
345
347
      
346
348
      if (ei_connect_xinit(&ec, h_hostname, h_alivename, h_nodename,
364
366
     * Expand name to a real name (may be ip-address) 
365
367
     */
366
368
    /* FIXME better error string */
367
 
    if ((hp = get_hostent(host)) == 0) {
368
 
        fprintf(stderr,"erl_call: can't get_hostent(%s)\n", host);
 
369
    if ((hp = ei_gethostbyname(host)) == 0) {
 
370
        fprintf(stderr,"erl_call: can't ei_gethostbyname(%s)\n", host);
369
371
        exit(1);
370
372
    }
371
 
    /* If shortnames cut of the name at first '.' */
 
373
    /* If shortnames, cut off the name at first '.' */
372
374
    if (flags.use_long_name == 0 && (ct = strchr(hp->h_name, '.')) != NULL) {
373
375
        *ct = '\0';
374
376
    }
375
 
    strcpy(host_name, hp->h_name);
 
377
    strncpy(host_name, hp->h_name, EI_MAXHOSTNAMELEN);
 
378
    host_name[EI_MAXHOSTNAMELEN] = '\0';
 
379
    if (strlen(flags.node) + strlen(host_name) + 2 > sizeof(nodename)) {
 
380
        fprintf(stderr,"erl_call: nodename too long: %s\n", flags.node);
 
381
        exit(1);
 
382
    }
376
383
    sprintf(nodename, "%s@%s", flags.node, host_name);
377
384
 
378
385
    /* 
401
408
 
402
409
        ei_encode_empty_list(NULL, &i);
403
410
 
404
 
        p = (char *)malloc(i);
 
411
        p = (char *)ei_chk_malloc(i);
405
412
        i = 0;          /* Reset */
406
413
          
407
414
        ei_encode_empty_list(p, &i);
426
433
    if (flags.modp && (modname != NULL)) {
427
434
      char fname[256];
428
435
 
 
436
      if (strlen(modname) + 4 + 1 > sizeof(fname)) {
 
437
      fprintf(stderr,"erl_call: module name too long: %s\n", modname);
 
438
      exit(1);
 
439
      }
429
440
      strcpy(fname, modname);
430
441
      strcat(fname, ".erl");
431
442
 
443
454
          ei_encode_binary(NULL, &i, module, modsize);
444
455
          ei_encode_empty_list(NULL, &i);
445
456
 
446
 
          p = (char *)malloc(i);
 
457
          p = (char *)ei_chk_malloc(i);
447
458
          i = 0;                /* Reset */
448
459
          
449
460
          ei_encode_list_header(p, &i, 2);
476
487
          ei_encode_empty_list(NULL, &i);
477
488
          ei_encode_empty_list(NULL, &i);
478
489
 
479
 
          p = (char *)malloc(i);
 
490
          p = (char *)ei_chk_malloc(i);
480
491
          i = 0;                /* Reset */
481
492
          
482
493
          ei_encode_list_header(p, &i, 2);
521
532
          ei_encode_binary(NULL, &i, evalbuf, len);
522
533
          ei_encode_empty_list(NULL, &i);
523
534
 
524
 
          p = (char *)malloc(i);
 
535
          p = (char *)ei_chk_malloc(i);
525
536
          i = 0;                /* Reset */
526
537
          
527
538
          ei_encode_list_header(p, &i, 1);
592
603
 *
593
604
 ***************************************************************************/
594
605
 
595
 
/*
596
 
 * Get host entry (by address or name)
597
 
 */
598
 
/* FIXME: will fail on names like '2fun4you'.  */
599
 
static struct hostent* get_hostent(char *host)
600
 
{
601
 
  if (isdigit((int)*host)) {
602
 
    struct in_addr ip_addr;
603
 
    int b1, b2, b3, b4;
604
 
    long addr;
605
 
      
606
 
    /* FIXME: Use inet_aton() (or inet_pton() and get v6 for free). */
607
 
    if (sscanf(host, "%d.%d.%d.%d", &b1, &b2, &b3, &b4) != 4) {
608
 
        return NULL;
609
 
    }
610
 
    addr = inet_addr(host);
611
 
    ip_addr.s_addr = htonl(addr);
612
 
      
613
 
    return ei_gethostbyaddr((char *)&ip_addr,sizeof(struct in_addr), AF_INET);
614
 
  }
615
 
 
616
 
  return ei_gethostbyname(host);
617
 
} /* get_hostent */
618
 
 
619
 
 
620
 
 
621
606
 
622
607
/* 
623
608
 * This function does only return on success.
719
704
 
720
705
  EAT(str);
721
706
  len = str-begin;
722
 
  *mod = (char *) calloc(len + 1, sizeof(char));
 
707
  *mod = (char *) ei_chk_calloc(len + 1, sizeof(char));
723
708
  memcpy(*mod, begin, len);
724
709
 
725
710
  SKIP_SPACE(str);
726
711
  if (*str == '\0') {
727
 
    *fun = (char *) calloc(strlen(start)+1, sizeof(char));
728
 
    strcpy(*fun, start);
729
 
    *args = (char *) calloc(strlen(empty_list)+1, sizeof(char));
730
 
    strcpy(*args, empty_list);
 
712
    *fun = ei_chk_strdup(start);
 
713
    *args = ei_chk_strdup(empty_list);
731
714
    return;
732
715
  }
733
716
  begin = str;
734
717
  EAT(str);
735
718
  len = str-begin;
736
 
  *fun = (char *) calloc(len + 1, sizeof(char));
 
719
  *fun = (char *) ei_chk_calloc(len + 1, sizeof(char));
737
720
  memcpy(*fun, begin, len);
738
721
 
739
722
  SKIP_SPACE(str);
740
723
  if (*str == '\0') {
741
 
    *args = (char *) calloc(strlen(empty_list)+1, sizeof(char));
742
 
    strcpy(*args, empty_list);
 
724
    *args = ei_chk_strdup(empty_list);
743
725
    return;
744
726
  }
745
727
 
746
 
  *args = (char *) calloc(strlen(str) + 1, sizeof(char));
747
 
  strcpy(*args, str);
 
728
  *args = ei_chk_strdup(str);
748
729
  
749
730
  return;
750
731
 
760
741
    int bsize = BUFSIZ;
761
742
    int len = 0;
762
743
    int i;
763
 
    char *tmp = (char *) malloc(bsize);
 
744
    char *tmp = (char *) ei_chk_malloc(bsize);
764
745
 
765
746
    while (1) {
766
747
        if ((i = read(0, &tmp[len], bsize-len)) < 0) {
772
753
            len += i;
773
754
            if ((len+50) > bsize) {
774
755
                bsize = len * 2;
775
 
                tmp = (char *) realloc(tmp, bsize);
 
756
                tmp = (char *) ei_chk_realloc(tmp, bsize);
776
757
            } else {
777
758
                continue;
778
759
            }
809
790
      }
810
791
    } /* while */
811
792
    i = tmp - start;
812
 
    *mname = (char *) calloc(i+1, sizeof(char));
 
793
    *mname = (char *) ei_chk_calloc(i+1, sizeof(char));
813
794
    memcpy(*mname, start, i);
814
795
  }
815
 
  free(mbuf);                   /* Allocated in read_stdin() */
 
796
  if (*mbuf)
 
797
      free(*mbuf);                      /* Allocated in read_stdin() */
816
798
 
817
799
  return len;
818
800
 
904
886
    }
905
887
}
906
888
#endif
 
889
 
 
890
 
 
891
/***************************************************************************
 
892
 *
 
893
 *  Utility functions
 
894
 *
 
895
 ***************************************************************************/
 
896
 
 
897
static void* ei_chk_malloc(size_t size)
 
898
{
 
899
    void *p = malloc(size);
 
900
    if (p == NULL) {
 
901
        fprintf(stderr,"erl_call: insufficient memory\n");
 
902
        exit(1);
 
903
    }
 
904
    return p;
 
905
}
 
906
 
 
907
static void* ei_chk_calloc(size_t nmemb, size_t size)
 
908
{
 
909
    void *p = calloc(nmemb, size);
 
910
    if (p == NULL) {
 
911
        fprintf(stderr,"erl_call: insufficient memory\n");
 
912
        exit(1);
 
913
    }
 
914
    return p;
 
915
}
 
916
 
 
917
static void* ei_chk_realloc(void *old, size_t size)
 
918
{
 
919
    void *p = realloc(old, size);
 
920
    if (!p) {
 
921
        fprintf(stderr, "erl_call: cannot reallocate %u bytes of memory from %p\n",
 
922
                (unsigned) size, old);
 
923
        exit (1);
 
924
    }
 
925
    return p;
 
926
}
 
927
 
 
928
static char* ei_chk_strdup(char *s)
 
929
{
 
930
    char *p = strdup(s);
 
931
    if (p == NULL) {
 
932
        fprintf(stderr,"erl_call: insufficient memory\n");
 
933
        exit(1);
 
934
    }
 
935
    return p;
 
936
}