This source file includes following definitions.
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 #include <config.h>
19
20 #include <unistd.h>
21
22
23 #include <limits.h>
24
25 #include <string.h>
26
27 #include <errno.h>
28 #include <stdio.h>
29
30 #include "root-uid.h"
31
32 #include "macros.h"
33
34 #define TESTHOSTNAME "gnulib-hostname"
35
36
37
38
39 #if !HAVE_GETEUID || defined __CYGWIN__
40 # define geteuid() ROOT_UID
41 #endif
42
43 int
44 main (int argc, _GL_UNUSED char *argv[])
45 {
46 char origname[HOST_NAME_MAX];
47 char newname[HOST_NAME_MAX];
48 char longname[HOST_NAME_MAX + 2];
49 int rcs, i;
50
51
52
53
54
55 if (geteuid () != ROOT_UID)
56 {
57 fprintf (stderr, "Skipping test: insufficient permissions.\n");
58 return 77;
59 }
60
61
62
63
64 ASSERT (gethostname (origname, sizeof (origname)) == 0);
65
66
67
68
69 rcs = sethostname (TESTHOSTNAME, strlen (TESTHOSTNAME));
70
71 if (rcs != 0)
72 {
73 if (rcs == -1 && errno == ENOSYS)
74 {
75 fprintf (stderr,
76 "Skipping test: sethostname is not really implemented.\n");
77 return 77;
78 }
79 else if (rcs == -1
80 && (errno == EPERM
81 || errno == EACCES))
82 {
83 fprintf (stderr, "Skipping test: insufficient permissions.\n");
84 return 77;
85 }
86 else
87 {
88 fprintf (stderr, "error setting valid hostname.\n");
89 return 1;
90 }
91 }
92 else
93 {
94 ASSERT (gethostname (newname, sizeof (newname)) == 0);
95
96
97
98 #if !(defined _WIN32 || defined __CYGWIN__)
99
100
101
102
103 if (strcmp (newname, TESTHOSTNAME) != 0)
104 {
105 fprintf (stderr, "set/get comparison failed.\n");
106 return 1;
107 }
108 #endif
109 }
110
111
112
113
114 for (i = 0; i < (HOST_NAME_MAX + 1); i++)
115 longname[i] = 'a';
116
117 longname[i] = '\0';
118
119 rcs = sethostname (longname, (HOST_NAME_MAX + 1));
120
121 if (rcs != -1)
122 {
123
124 ASSERT (sethostname (origname, strlen (origname)) == 0);
125 fprintf (stderr, "setting a too long hostname succeeded.\n");
126 return 1;
127 }
128
129
130 ASSERT (sethostname (origname, strlen (origname)) == 0);
131
132 return 0;
133 }