~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/utilbin/gethostname.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*___INFO__MARK_BEGIN__*/
 
2
/*************************************************************************
 
3
 * 
 
4
 *  The Contents of this file are made available subject to the terms of
 
5
 *  the Sun Industry Standards Source License Version 1.2
 
6
 * 
 
7
 *  Sun Microsystems Inc., March, 2001
 
8
 * 
 
9
 * 
 
10
 *  Sun Industry Standards Source License Version 1.2
 
11
 *  =================================================
 
12
 *  The contents of this file are subject to the Sun Industry Standards
 
13
 *  Source License Version 1.2 (the "License"); You may not use this file
 
14
 *  except in compliance with the License. You may obtain a copy of the
 
15
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 
16
 * 
 
17
 *  Software provided under this License is provided on an "AS IS" basis,
 
18
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 
19
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 
20
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 
21
 *  See the License for the specific provisions governing your rights and
 
22
 *  obligations concerning the Software.
 
23
 * 
 
24
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 
25
 * 
 
26
 *   Copyright: 2001 by Sun Microsystems, Inc.
 
27
 * 
 
28
 *   All Rights Reserved.
 
29
 * 
 
30
 ************************************************************************/
 
31
/*___INFO__MARK_END__*/
 
32
#include <sys/types.h>
 
33
#include <sys/socket.h>
 
34
#include <netinet/in.h>
 
35
#include <arpa/inet.h>
 
36
#include <netdb.h>
 
37
#include <stdio.h>
 
38
#include <unistd.h>
 
39
#include <stdlib.h>
 
40
#include <string.h>
 
41
#include "basis_types.h"
 
42
#include "msg_utilbin.h"
 
43
#include "sge_string.h"
 
44
#include "sge_arch.h"
 
45
#include "sge_hostname.h"
 
46
#include "cl_commlib.h"
 
47
#include "version.h"
 
48
 
 
49
int usage(void)
 
50
{
 
51
  fprintf(stderr, "Version: %s\n", GDI_VERSION);
 
52
  fprintf(stderr, "%s\n gethostname [-help|-name|-aname|-all]\n\n%s\n", MSG_UTILBIN_USAGE, MSG_COMMAND_USAGE_GETHOSTNAME );
 
53
  exit(1);
 
54
  return 0;
 
55
}
 
56
 
 
57
int main(int argc,char *argv[]) {
 
58
   struct hostent *he = NULL;
 
59
   char* resolved_name = NULL;
 
60
   int retval = CL_RETVAL_OK;
 
61
   char **tp,**tp2;
 
62
   int name_only = 0;
 
63
   int sge_aliasing = 0;
 
64
   int all_option = 0;
 
65
   int system_error = 0;
 
66
 
 
67
   if (argc < 1 ) {
 
68
      usage();
 
69
   } 
 
70
   if (argc >= 2) {
 
71
      if (!strcmp(argv[1], "-help")) {
 
72
         usage();
 
73
      }
 
74
      if (!strcmp(argv[1], "-name")) {
 
75
         if (argc != 2) {
 
76
            usage(); 
 
77
         }
 
78
         name_only = 1;
 
79
      }   
 
80
      if (!strcmp(argv[1], "-aname")) {
 
81
         if (argc != 2) {
 
82
            usage(); 
 
83
         }
 
84
         name_only = 1;
 
85
         sge_aliasing = 1;
 
86
      }   
 
87
      if (!strcmp(argv[1], "-all")) {
 
88
         if (argc != 2) {
 
89
            usage(); 
 
90
         }
 
91
         name_only = 0;
 
92
         sge_aliasing = 1;
 
93
         all_option = 1;
 
94
      }
 
95
   }
 
96
  
 
97
   if (name_only == 0 && argc != 1 && all_option == 0) {
 
98
      usage();
 
99
   }
 
100
     
 
101
  retval = cl_com_setup_commlib(CL_NO_THREAD ,CL_LOG_OFF, NULL);
 
102
  if (retval != CL_RETVAL_OK) {
 
103
     fprintf(stderr,"%s\n",cl_get_error_text(retval));
 
104
     exit(1);
 
105
  }
 
106
 
 
107
  if (sge_aliasing ) {
 
108
     const char *alias_path = sge_get_alias_path();
 
109
     cl_com_set_alias_file(alias_path);
 
110
     FREE(alias_path);
 
111
  }
 
112
 
 
113
  retval = cl_com_gethostname(&resolved_name, NULL, &he, &system_error);
 
114
  if (retval != CL_RETVAL_OK) {
 
115
     char* err_text = cl_com_get_h_error_string(system_error);
 
116
     if (err_text == NULL) {
 
117
        err_text = strdup(strerror(system_error));
 
118
        if (err_text == NULL) {
 
119
           err_text = strdup("unexpected error");
 
120
        }
 
121
     }
 
122
     fprintf(stderr,"error resolving local host: %s (%s)\n",cl_get_error_text(retval), err_text);
 
123
     free(err_text); 
 
124
     err_text = NULL;
 
125
     cl_com_cleanup_commlib();
 
126
     exit(1);
 
127
  }
 
128
 
 
129
 
 
130
  if (name_only) {
 
131
     if (sge_aliasing) {
 
132
        if (resolved_name != NULL) {
 
133
           printf("%s\n",resolved_name);
 
134
        } else {
 
135
           printf("%s\n","unexpected error");
 
136
        }
 
137
     } else {
 
138
        if (he != NULL) {
 
139
           printf("%s\n",he->h_name);
 
140
        } else {
 
141
           printf("%s\n","could not get hostent struct");
 
142
        }
 
143
     }
 
144
  } else {
 
145
     if (he != NULL) {
 
146
        printf(MSG_SYSTEM_HOSTNAMEIS_S , he->h_name);
 
147
        printf("\n");
 
148
        
 
149
        if (resolved_name != NULL && all_option) {
 
150
           printf("SGE name: %s\n",resolved_name);
 
151
        }
 
152
 
 
153
        printf(MSG_SYSTEM_ALIASES );
 
154
 
 
155
        for (tp = he->h_aliases; *tp; tp++)
 
156
           printf("%s ", *tp);
 
157
        printf("\n");
 
158
  
 
159
        printf(MSG_SYSTEM_ADDRESSES );
 
160
        for (tp2 = he->h_addr_list; *tp2; tp2++)
 
161
           printf("%s ", inet_ntoa(* (struct in_addr *) *tp2));  /* inet_ntoa() is not MT save */
 
162
        printf("\n");  
 
163
     } else {   
 
164
        fprintf(stderr,"%s\n","could not get hostent struct");
 
165
     }
 
166
  }
 
167
  free(resolved_name);
 
168
  sge_free_hostent(&he);
 
169
 
 
170
  retval = cl_com_cleanup_commlib();
 
171
  if (retval != CL_RETVAL_OK) {
 
172
     fprintf(stderr,"%s\n",cl_get_error_text(retval));
 
173
     exit(1);
 
174
  }
 
175
  return 0;  
 
176
}