aboutsummaryrefslogtreecommitdiffstats
path: root/udev-block-notify.c
blob: 2952db3528fadc64d87bc893a9f4fc12184a9dbf (plain) (blame)
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
/*
 * (C) 2011-2013 by Christian Hesse <mail@eworm.de>
 *
 * This software may be used and distributed according to the terms
 * of the GNU General Public License, incorporated herein by reference.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <libnotify/notify.h>
#include <libudev.h>

#define PROGNAME	"udev-block-notify"

#define NOTIFICATION_TIMEOUT	10000
#ifndef DEBUG
#define DEBUG	0
#endif

#define ICON_ADD	"media-removable"
#define ICON_REMOVE	"media-removable"
#define ICON_MOVE	"media-removable"
#define ICON_CHANGE	"media-removable"
#define ICON_DEFAULT	"media-removable"

#define TEXT_TOPIC	"Udev Block Notification"
#define TEXT_ADD	"Device <b>%s</b> (%i:%i) <b>appeared</b>."
#define TEXT_REMOVE	"Device <b>%s</b> (%i:%i) <b>disappeared</b>."
#define TEXT_MOVE	"Device <b>%s</b> (%i:%i) was <b>renamed</b>."
#define TEXT_CHANGE	"Device <b>%s</b> (%i:%i) media <b>changed</b>."
#define TEXT_DEFAULT	"Anything happend to <b>%s</b> (%i:%i)... Don't know."
#define TEXT_TAG	"\n%s: <i>%s</i>"

int main (int argc, char ** argv) {
	char action;
	char *device = NULL, *icon = NULL, *notifystr = NULL;
	const char *value = NULL;
	fd_set readfds;
	GError *error = NULL;
	int devnum, errcount = 0;
        NotifyNotification *notification;
        NotifyNotification ***notificationref;
	struct udev_device *dev = NULL;
   	struct udev_monitor *mon = NULL;
	struct udev *udev = NULL;
	short int *maxminor;
	unsigned short int i, major, minor;

	printf("%s: %s v%s (compiled: " __DATE__ ", " __TIME__ ")\n", argv[0], PROGNAME, VERSION);

	if(!notify_init("Udev-Block-Notification")) {
		fprintf(stderr, "%s: Can't create notify.\n", argv[0]);
		exit(EXIT_FAILURE);
	}

	udev = udev_new();
	if(!udev) {
		fprintf(stderr, "%s: Can't create udev.\n", argv[0]);
		exit(EXIT_FAILURE);
	}

	mon = udev_monitor_new_from_netlink(udev, "udev");
	udev_monitor_filter_add_match_subsystem_devtype(mon, "block", NULL);
	udev_monitor_enable_receiving(mon);

	notificationref = malloc(256 * sizeof(NotifyNotification));
	for(i = 0; i < 256; i++)
		notificationref[i] = NULL;
	maxminor = malloc(256 * sizeof(short int));
	for(i = 0; i < 256; i++)
		maxminor[i] = -1;

	while (1) {
                FD_ZERO(&readfds);
                if (mon != NULL)
                        FD_SET(udev_monitor_get_fd(mon), &readfds);

                select(udev_monitor_get_fd(mon) + 1, &readfds, NULL, NULL, NULL);

                if ((mon != NULL) && FD_ISSET(udev_monitor_get_fd(mon), &readfds)) {
			dev = udev_monitor_receive_device(mon);
			if(dev) {
				device = (char *) udev_device_get_sysname(dev);
				devnum = udev_device_get_devnum(dev);
				major = devnum / 256;
				minor = devnum - (major * 256);

				action = udev_device_get_action(dev)[0];
				switch(action) {
					case 'a':
						// a: add
						notifystr = malloc(strlen(TEXT_ADD) + strlen(device) + 5);
						sprintf(notifystr, TEXT_ADD, device, major, minor);
						icon = ICON_ADD;
						break;
					case 'r':
						// r: remove
						notifystr = malloc(strlen(TEXT_REMOVE) + strlen(device) + 5);
						sprintf(notifystr, TEXT_REMOVE, device, major, minor);
						icon = ICON_REMOVE;
						break;
					case 'm':
						// m: move
						notifystr = malloc(strlen(TEXT_MOVE) + strlen(device) + 5);
						sprintf(notifystr, TEXT_MOVE, device, major, minor);
						icon = ICON_MOVE;
						break;
					case 'c':
						// c: change
						notifystr = malloc(strlen(TEXT_CHANGE) + strlen(device) + 5);
						sprintf(notifystr, TEXT_CHANGE, device, major, minor);
						icon = ICON_CHANGE;
						break;
					default:
						// we should never get here I think...
						notifystr = malloc(strlen(TEXT_DEFAULT) + strlen(device) + 5);
						sprintf(notifystr, TEXT_CHANGE, device, major, minor);
						icon = ICON_DEFAULT;
				}

				if (action != 'r') {
					if ((value = udev_device_get_property_value(dev, "ID_FS_LABEL")) != NULL) {
						notifystr = realloc(notifystr, strlen(TEXT_TAG) + strlen(notifystr) + 5 + strlen(value));
						sprintf(notifystr + strlen(notifystr), TEXT_TAG, "Label", value);
					}
					if ((value = udev_device_get_property_value(dev, "ID_FS_TYPE")) != NULL) {
						notifystr = realloc(notifystr, strlen(TEXT_TAG) + strlen(notifystr) + 4 + strlen(value));
						sprintf(notifystr + strlen(notifystr), TEXT_TAG, "Type", value);
					}
					if ((value = udev_device_get_property_value(dev, "ID_FS_USAGE")) != NULL) {
						notifystr = realloc(notifystr, strlen(TEXT_TAG) + strlen(notifystr) + 5 + strlen(value));
						sprintf(notifystr + strlen(notifystr), TEXT_TAG, "Usage", value);
					}
					if ((value = udev_device_get_property_value(dev, "ID_FS_UUID")) != NULL) {
						notifystr = realloc(notifystr, strlen(TEXT_TAG) + strlen(notifystr) + 4 + strlen(value));
						sprintf(notifystr + strlen(notifystr), TEXT_TAG, "UUID", value);
					}
					if ((value = udev_device_get_property_value(dev, "ID_PART_TABLE_TYPE")) != NULL) {
						notifystr = realloc(notifystr, strlen(TEXT_TAG) + strlen(notifystr) + 14 + strlen(value));
						sprintf(notifystr + strlen(notifystr), TEXT_TAG, "Partition Type", value);
					}
					if ((value = udev_device_get_property_value(dev, "ID_PART_TABLE_NAME")) != NULL) {
						notifystr = realloc(notifystr, strlen(TEXT_TAG) + strlen(notifystr) + 14 + strlen(value));
						sprintf(notifystr + strlen(notifystr), TEXT_TAG, "Partition Name", value);
					}
					if ((value = udev_device_get_property_value(dev, "ID_PART_ENTRY_TYPE")) != NULL) {
						notifystr = realloc(notifystr, strlen(TEXT_TAG) + strlen(notifystr) + 14 + strlen(value));
						sprintf(notifystr + strlen(notifystr), TEXT_TAG, "Partition UUID", value);
					}
				}

#if DEBUG
				printf("%s: %s\n", argv[0], notifystr);
#endif

				if (maxminor[major] < minor) {
					notificationref[major] = realloc(notificationref[major], (minor + 1) * sizeof(size_t));
			                while(maxminor[major] < minor)
			                        notificationref[major][++maxminor[major]] = NULL;
			        }
					
				if (notificationref[major][minor] == NULL) {
					notification = notify_notification_new(TEXT_TOPIC, notifystr, icon);
					notificationref[major][minor] = notification;
				} else {
					notification = notificationref[major][minor];
					notify_notification_update(notification, TEXT_TOPIC, notifystr, icon);
				}

			        notify_notification_set_timeout(notification, NOTIFICATION_TIMEOUT);
				notify_notification_set_category(notification, PROGNAME);
				notify_notification_set_urgency (notification, NOTIFY_URGENCY_NORMAL);
	
				while(!notify_notification_show(notification, &error)) {
					if (errcount > 1) {
						fprintf(stderr, "%s: Looks like we can not reconnect to notification daemon... Exiting.\n", argv[0]);
						exit(EXIT_FAILURE);
					} else {
						g_printerr("%s: Error \"%s\" while trying to show notification. Trying to reconnect.\n", argv[0], error->message);
						errcount++;

						g_error_free(error);
						error = NULL;
	
						notify_uninit();
						
						usleep(500 * 1000);

						if(!notify_init("Udev-Block-Notification")) {
							fprintf(stderr, "%s: Can't create notify.\n", argv[0]);
							exit(EXIT_FAILURE);
						}
					}
				}
				errcount = 0;
	
				free(notifystr);
				udev_device_unref(dev);
			}
	
			// This is not really needed... But we want to make shure not to eat 100% CPU if anything breaks. ;)
			usleep(50 * 1000);
		}
	}

	udev_unref(udev);
	return EXIT_SUCCESS;
}