aboutsummaryrefslogtreecommitdiffstats
path: root/src/rename_device.c
blob: 968bd0158f443d5def709c735bb4070edc6f2002 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396

/* 
 * rename_device.c: udev helper to rename ethernet devices.
 *
 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions of the
 * GNU General Public License v.2.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */


#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/ioctl.h>
#include <sys/types.h>

#include <linux/sockios.h>

#include <net/if.h>

#include <glib.h>

#define LOCKFILE "/dev/.rename_device.lock"

void sighandler(int dummy) {
	unlink(LOCKFILE);
	_exit(1);
}

struct netdev {
	char *hwaddr;
	char *dev;
	struct netdev *next;
};

struct netdev *configs = NULL;
struct netdev *devs = NULL;

#if defined(__s390__) || defined(__s390x__)
static int is_cdev(const struct dirent *dent) {
        char *end = NULL;
        
        if (strncmp(dent->d_name,"cdev",4))
                return 0;
        strtoul(dent->d_name+4,&end, 10);
        if (*end)
                return 0;
        return 1;
}

static inline char *getdev(char *path, char *ent) {
        char *a, *b, *ret;
        
        asprintf(&a,"%s/%s",path,ent);
        b = canonicalize_file_name(a);
        ret = strdup(basename(b));
        free(b);
        free(a);
        return ret;
}

char *read_subchannels(char *device) {
	char *tmp, *path, *ret;
	int n, x;
	struct dirent **cdevs;
		
	if (asprintf(&path,"/sys/class/net/%s/device",device) == -1)
		return NULL;
	if ((n = scandir(path, &cdevs, is_cdev, alphasort)) <= 0)
		return NULL;
		
	ret = getdev(path,cdevs[0]->d_name);
	for (x = 1 ; x < n ; x++ ) { 
		if (asprintf(&tmp, "%s,%s", ret, getdev(path, cdevs[x]->d_name)) == -1) 
			return NULL;
		free(ret);
		ret = tmp;
	}
	free(path);
	return ret;
}

#endif

struct netdev *get_devs() {
	DIR *dir;
	struct dirent *entry;
	struct netdev *ret = NULL, *tmpdev;
	
	dir = opendir("/sys/class/net");
	if (!dir)
		return NULL;
	while ((entry = readdir(dir))) {
		char *path;
		gchar *contents;
		
		contents = NULL;
		
		if (!strcmp(entry->d_name,".") || !strcmp(entry->d_name,"..")) {
			continue;
		}
		if (asprintf(&path,"/sys/class/net/%s/type",entry->d_name) == -1)
			continue;
		g_file_get_contents(path, &contents, NULL, NULL);
		if (!contents) continue;
		if (atoi(contents) >= 256) {
			g_free(contents);
			continue;
		}
		g_free(contents);
		contents = NULL;
#if defined(__s390__) || defined(__s390x__)
		contents = read_subchannels(entry->d_name);
#else
		if (asprintf(&path,"/sys/class/net/%s/address",entry->d_name) == -1)
			continue;
		g_file_get_contents(path, &contents, NULL, NULL);
#endif /* mainframe */
		if (!contents) continue;
		contents = g_strstrip(contents);
		tmpdev = calloc(1, sizeof(struct netdev));
		tmpdev->dev = g_strstrip(g_strdup(entry->d_name));
		tmpdev->hwaddr = g_strstrip(g_strdup(contents));
		if (ret)
			tmpdev->next = ret;
		ret = tmpdev;
		g_free(contents);
	}
	return ret;
}

int isCfg(const struct dirent *dent) {
	int len = strlen(dent->d_name);
	
	if (strncmp(dent->d_name,"ifcfg-",6))
		return 0;
	if (strstr(dent->d_name,"rpmnew") ||
	    strstr(dent->d_name,"rpmsave") ||
	    strstr(dent->d_name,"rpmorig"))
		return 0;
	if (dent->d_name[len-1] == '~')
		return 0;
	if (!strncmp(dent->d_name+len-5,".bak",4))
		return 0;
	return 1;
}

struct netdev *get_configs() {
	int ncfgs = 0;
	struct netdev *ret, *tmpdev;
	struct dirent **cfgs;
	int x;
	
	ret = NULL;
	
	if ((ncfgs = scandir("/etc/sysconfig/network-scripts",&cfgs,
			     isCfg, alphasort)) == -1) {
		return ret;
	}
	for (x = 0; x < ncfgs; x++ ) {
		char *path;
		char *devname, *hwaddr;
		gchar *contents, **lines;
		int i;
		
		devname = hwaddr = contents = NULL;
		lines = NULL;
		if (asprintf(&path,"/etc/sysconfig/network-scripts/%s",
			     cfgs[x]->d_name) == -1)
			continue;
		g_file_get_contents(path, &contents, NULL, NULL);
		if (!contents)
			continue;
		lines = g_strsplit(contents,"\n", 0);
		for (i = 0; lines[i]; i++) {
			if (g_str_has_prefix(lines[i],"DEVICE=")) {
				devname = lines[i] + 7;
				/* ignore alias devices */
				if (strchr(devname,':'))
					devname = NULL;
			}
#if defined(__s390__) || defined(__s390x__)
			if (g_str_has_prefix(lines[i],"SUBCHANNELS=")) {
				hwaddr = lines[i] + 12;
			}
#else
			if (g_str_has_prefix(lines[i],"HWADDR=")) {
				hwaddr = lines[i] + 7;
			}
#endif
		}
		if (!devname || !hwaddr) {
			g_free(contents);
			g_strfreev(lines);
			continue;
		}
		tmpdev = calloc(1, sizeof(struct netdev));
		tmpdev->dev = g_strstrip(g_strdup(devname));
		tmpdev->hwaddr = g_strstrip(g_strdup(hwaddr));
		if (ret)
			tmpdev->next = ret;
		ret = tmpdev;
		g_free(contents);
		g_strfreev(lines);
	}
	free(cfgs);
	return ret;
}
		
char *get_hwaddr(char *device) {
	struct netdev *dev;
	
	for (dev = devs; dev; dev = dev->next)
		if (!strcmp(dev->dev, device))
			return dev->hwaddr;
	return NULL;
}
		
char *get_config_by_hwaddr(char *hwaddr, char *current) {
	struct netdev *config;
	char *first = NULL;
	
	if (!hwaddr) return NULL;
	
	for (config = configs; config; config = config->next) {
		if (strcasecmp(config->hwaddr, hwaddr) != 0)
			continue;
		if (!current || !strcasecmp(config->dev, current))
			return config->dev;
		if (!first)
			first = config->dev;
	}
	return first;
}

char *get_device_by_hwaddr(char *hwaddr) {
	struct netdev *dev;
	
	if (!hwaddr) return NULL;
	
	for (dev = devs; dev; dev = dev->next)
		if (!strcasecmp(dev->hwaddr, hwaddr))
			return dev->dev;
	return NULL;
}

int do_rename(char *src, char *target) {
	int sock;
	struct ifreq ifr;
	int ret;
	
	sock = socket(PF_INET, SOCK_DGRAM, 0);
	if (sock == -1)
		return 1;
	
	memset(&ifr,'\0', sizeof(struct ifreq));
	g_strlcpy(ifr.ifr_name, src, IFNAMSIZ);
	g_strlcpy(ifr.ifr_newname, target, IFNAMSIZ);
	ret = ioctl(sock, SIOCSIFNAME, &ifr);
	close(sock);
	return ret;
}
	

void rename_device(char *src, char *target, struct netdev *current) {
	int rc;
	
	if (!current) {
		current = calloc(1, sizeof(struct netdev));
		current->dev = src;
	}
	
	rc = do_rename(src, target);
	if (rc && errno != ENODEV) {
		char *hw;
		char *nconfig;
		char *curdev;
		char *dev = NULL;
		struct netdev *i, *tmpdev;
		
		hw = get_hwaddr(target);
		if (!hw) {
			devs = get_devs();
			hw = get_hwaddr(target);
			if (!hw)
				return;
		}
		
		nconfig = get_config_by_hwaddr(hw, NULL);
		curdev = get_device_by_hwaddr(hw);

		if (nconfig) {
			dev = nconfig;
			for (i = current; i; i = i->next) {
				if (!strcmp(i->dev,dev))
					dev = NULL;
			}
		}
		if (!dev)
			asprintf(&dev,"__tmp%d",rand());
		if (!dev)
			return;
		tmpdev = calloc(1,sizeof(struct netdev));
		tmpdev->dev = curdev;
		if (current)
			tmpdev->next = current;
		current = tmpdev;
		rename_device(curdev, dev, current);
		do_rename(src,target);
	}
}

void take_lock() {
	int count = 0;
	int lockfd;
	
	while (1) {
		lockfd = open(LOCKFILE, O_RDWR|O_CREAT|O_EXCL);
		if (lockfd != -1) {
			write(lockfd,"%d\n",getpid());
			close(lockfd);
			break;
		}
		count++;
		/* If we've slept for 20 seconds, break the lock. */
		if (count >= 200) {
			int fd;
			char buf[32];
			int pid;
				
			fd = open(LOCKFILE, O_RDONLY);
			if (fd == -1)
				break;
			read(fd,buf,32);
			close(fd);
			pid = atoi(buf);
			if (pid && pid != 1) {
				kill(pid,SIGKILL);
			}
		}
		usleep(100000);
		continue;

	}
	return;
}

int main(int argc, char **argv) {
	char *src, *target, *hw;
		
	take_lock();
	
	signal(SIGSEGV,sighandler);
	signal(SIGKILL,sighandler);
	signal(SIGTERM,sighandler);
	signal(SIGSEGV,sighandler);
	signal(SIGALRM,sighandler);
	alarm(10);
	
	configs = get_configs();
	devs = get_devs();
	
	src = getenv("INTERFACE");
	if (!src)
		goto out_unlock;
	hw = get_hwaddr(src);
	if (!hw)
		goto out_unlock;
	target = get_config_by_hwaddr(hw, src);
	if (!target || !strcmp(src,target))
		goto out_unlock;
	
	rename_device(src, target, NULL);
	printf("INTERFACE=%s\n",target);
	printf("DEVPATH=/class/net/%s\n", target);
out_unlock:
	unlink(LOCKFILE);
	exit(0);
}