aboutsummaryrefslogtreecommitdiffstats
path: root/udev-block-notify.c
blob: 207044f057b61a77fbc82eb0f2bfea8277daca51 (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
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
/*
 * (C) 2011-2024 by Christian Hesse <mail@eworm.de>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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, see <https://www.gnu.org/licenses/>.
 *
 */

#include "udev-block-notify.h"
#include "config.h"
#include "version.h"

const static char optstring[] = "ht:vV";
const static struct option options_long[] = {
	/* name		has_arg			flag	val */
	{ "help",	no_argument,		NULL,	'h' },
	{ "timeout",	required_argument,	NULL,	't' },
	{ "verbose",	no_argument,		NULL,	'v' },
	{ "version",	no_argument,		NULL,	'V' },
	{ 0, 0, 0, 0 }
};

char *program;
uint8_t verbose = 0;
uint8_t doexit = 0;

/*** get_notification ***/
NotifyNotification * get_notification(struct notifications *notifications, dev_t devnum) {
	/* work with notifications->next here, we need it later to not miss
	 * the pointer in main struct */
	while (notifications->next != NULL) {
		if (notifications->next->devnum == devnum)
			return notifications->next->notification;
		notifications = notifications->next;
	}

	/* did not find the notification, creating a new struct element */
	notifications->next = malloc(sizeof(struct notifications));
	notifications = notifications->next;
	notifications->devnum = devnum;
	notifications->notification =
#		if NOTIFY_CHECK_VERSION(0, 7, 0)
		notify_notification_new(NULL, NULL, NULL);
#		else
		notify_notification_new(NULL, NULL, NULL, NULL);
#		endif
	notifications->next = NULL;

	notify_notification_set_category(notifications->notification, PROGNAME);
	notify_notification_set_urgency (notifications->notification, NOTIFY_URGENCY_NORMAL);

	return notifications->notification;
}

/*** newstr ***/
char * newstr(const char *text, const char *device, unsigned short int major, unsigned short int minor) {
	char *notifystr;

	notifystr = malloc(strlen(text) + strlen(device) + 10 /* max string length 2* unsigned short int */);
	sprintf(notifystr, text, device, major, minor);

	return notifystr;
}

/*** appendstr ***/
char * appendstr(const char *text, char *notifystr, const char *property, const char *value) {
	notifystr = realloc(notifystr, strlen(text) + strlen(notifystr) + strlen(property) + strlen(value));
	sprintf(notifystr + strlen(notifystr), text, property, value);

	return notifystr;
}

/*** received_signal ***/
void received_signal(int signal) {
	if (verbose > 0)
		printf("%s: Received signal: %s\n", program, strsignal(signal));

	doexit++;
}

/*** main ***/
int main (int argc, char ** argv) {
	const char *action = NULL;
	const char *device = NULL, *value = NULL;
	char *icon = NULL, *notifystr = NULL;
	fd_set readfds;
	GError *error = NULL;
	NotifyNotification *notification = NULL;
	struct notifications *notifications = NULL;
	unsigned int notification_timeout = NOTIFICATION_TIMEOUT;

	int i;
	dev_t devnum = 0;
	unsigned int major = 0, minor = 0;
	struct udev_device *dev = NULL;
	struct udev_monitor *mon = NULL;
	struct udev *udev = NULL;

	unsigned int version = 0, help = 0;

	program = argv[0];

	/* get the verbose status */
	while ((i = getopt_long(argc, argv, optstring, options_long, NULL)) != -1) {
		switch (i) {
			case 'h':
				help++;
				break;
			case 't':
				notification_timeout = atof(optarg) * 1000;
				break;
			case 'v':
				verbose++;
				break;
			case 'V':
				verbose++;
				version++;
				break;
		}
	}

	if (verbose > 0)
		printf("%s: %s v%s"
#ifdef HAVE_SYSTEMD
			" +systemd"
#endif
			" (compiled: " __DATE__ ", " __TIME__ ")\n", program, PROGNAME, VERSION);

	if (help > 0)
		printf("usage: %s [-h] [-t TIMEOUT] [-v] [-V]\n", program);

	if (version > 0 || help > 0)
		return EXIT_SUCCESS;

	if(notify_init("Udev-Block-Notification") == FALSE) {
		fprintf(stderr, "%s: Can't create notify.\n", program);
		exit(EXIT_FAILURE);
	}

	if ((udev = udev_new()) == NULL) {
		fprintf(stderr, "%s: Can't create udev.\n", program);
		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);

	/* allocate first struct element as dummy */
	notifications = malloc(sizeof(struct notifications));
	notifications->devnum = 0;
	notifications->notification = NULL;
	notifications->next = NULL;

	signal(SIGINT, received_signal);
	signal(SIGTERM, received_signal);

#ifdef HAVE_SYSTEMD
	sd_notify(0, "READY=1\nSTATUS=Waiting for udev block events...");
#endif

	while (doexit == 0) {
		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 = udev_device_get_sysname(dev);

				/* ignore temporary device mapper devices
				 * is there a better way to do this? */
				if (strncmp(device, "dm", 2) == 0) {
					const char * property;

					if (udev_device_get_property_value(dev, "DM_NAME") == NULL) {
						if (verbose > 0)
							printf("%s: Skipping temporary DM device %s\n", program, device);
						continue;
					}
					if ((property = udev_device_get_property_value(dev, "DM_LV_LAYER")) != NULL) {
						if (strcmp(property, "cow") == 0 || strcmp(property, "real") == 0) {
							if (verbose > 0)
								printf("%s: Skipping DM %s device %s\n", program, property, device);
							continue;
						}
					}
				}

				devnum = udev_device_get_devnum(dev);
				major = major(devnum);
				minor = minor(devnum);

				if (verbose > 0)
					printf("%s: Processing device %d:%d\n", program, major, minor);

				action = udev_device_get_action(dev);
				if (strcmp(action, "add") == 0)
					notifystr = newstr(TEXT_ADD, device, major, minor);
				else if (strcmp(action, "remove") == 0)
					notifystr = newstr(TEXT_REMOVE, device, major, minor);
				else if (strcmp(action, "move") == 0)
					notifystr = newstr(TEXT_MOVE, device, major, minor);
				else if (strcmp(action, "change") == 0)
					notifystr = newstr(TEXT_CHANGE, device, major, minor);
				else
					/* we should never get here I think... */
					notifystr = newstr(TEXT_DEFAULT, device, major, minor);

				/* Get possible values with:
				 * $ udevadm info --query=all --name=/path/to/dev
				 * Values available differs from device type and content */

				/* file system */
				if ((value = udev_device_get_property_value(dev, "ID_FS_LABEL")) != NULL && *value != 0)
					notifystr = appendstr(TEXT_TAG, notifystr, "Label", value);
				if ((value = udev_device_get_property_value(dev, "ID_FS_TYPE")) != NULL && *value != 0)
					notifystr = appendstr(TEXT_TAG, notifystr, "Type", value);
				if ((value = udev_device_get_property_value(dev, "ID_FS_USAGE")) != NULL && *value != 0)
					notifystr = appendstr(TEXT_TAG, notifystr, "Usage", value);
				if ((value = udev_device_get_property_value(dev, "ID_FS_UUID")) != NULL && *value != 0)
					notifystr = appendstr(TEXT_TAG, notifystr, "UUID", value);

				/* partition */
				if ((value = udev_device_get_property_value(dev, "ID_PART_TABLE_TYPE")) != NULL && *value != 0)
					notifystr = appendstr(TEXT_TAG, notifystr, "Partition Table Type", value);
				if ((value = udev_device_get_property_value(dev, "ID_PART_TABLE_NAME")) != NULL && *value != 0)
					notifystr = appendstr(TEXT_TAG, notifystr, "Partition Name", value);
				if ((value = udev_device_get_property_value(dev, "ID_PART_ENTRY_TYPE")) != NULL && *value != 0)
					notifystr = appendstr(TEXT_TAG, notifystr, "Partition Type", value);

				/* device mapper */
				if ((value = udev_device_get_property_value(dev, "DM_NAME")) != NULL && *value != 0)
					notifystr = appendstr(TEXT_TAG, notifystr, "Device mapper name", value);

				/* multi disk */
				if ((value = udev_device_get_property_value(dev, "MD_LEVEL")) != NULL && *value != 0)
					notifystr = appendstr(TEXT_TAG, notifystr, "Multi disk level", value);

				if (verbose > 0)
					printf("%s: %s\n", program, notifystr);

				/* get a notification */
				notification = get_notification(notifications, devnum);

				/* this is a fallback and should be replaced below
				 * if it is not... what drive is it? */
				icon = ICON_UNKNOWN;

				/* decide about what icon to use */
				value = udev_device_get_property_value(dev, "ID_BUS");
				if (udev_device_get_property_value(dev, "ID_CDROM") != NULL) { /* optical drive */
					if (udev_device_get_property_value(dev, "ID_CDROM_MEDIA_TRACK_COUNT_AUDIO") != NULL) {
						icon = ICON_MEDIA_OPTICAL_CD_AUDIO;
					} else {
						icon = ICON_DRIVE_OPTICAL;
					}
				} else if (udev_device_get_property_value(dev, "ID_DRIVE_FLOPPY") != NULL) { /* floppy drive */
					icon = ICON_MEDIA_FLOPPY;
				} else if (udev_device_get_property_value(dev, "ID_DRIVE_THUMB") != NULL) { /* thumb drive, e.g. USB flash */
					icon = ICON_MEDIA_REMOVABLE;
				} else if (udev_device_get_property_value(dev, "ID_DRIVE_FLASH_CF") != NULL ||
						udev_device_get_property_value(dev, "ID_DRIVE_FLASH_MS") != NULL ||
						udev_device_get_property_value(dev, "ID_DRIVE_FLASH_SD") != NULL ||
						udev_device_get_property_value(dev, "ID_DRIVE_FLASH_SM") != NULL) { /* flash card reader */
							/* note that usb card reader are recognized as USB hard disk */
					icon = ICON_MEDIA_FLASH;
				} else if (udev_device_get_property_value(dev, "ID_DRIVE_FLOPPY_ZIP") != NULL) { /* ZIP drive */
					icon = ICON_MEDIA_ZIP;
				} else if (udev_device_get_property_value(dev, "ID_MEDIA_PLAYER") != NULL) { /* media player */
					icon = ICON_MULTIMEDIA_PLAYER;
				} else if (udev_device_get_property_value(dev, "DM_NAME") != NULL) { /* device mapper */
					icon = ICON_DEVICE_MAPPER;
				} else if (udev_device_get_property_value(dev, "MD_NAME") != NULL) { /* multi disk */
					icon = ICON_DRIVE_MULTIDISK;
				} else if (strncmp(device, "loop", 4) == 0 ||
						strncmp(device, "ram", 3) == 0) { /* loop & RAM */
					icon = ICON_LOOP;
				} else if (strncmp(device, "nbd", 3) == 0) { /* network block device */
					icon = ICON_NETWORK_SERVER;
				} else if (value != NULL) {
					if (strcmp(value, "ata") == 0 ||
							strcmp(value, "scsi") == 0) { /* internal (s)ata/scsi hard disk */
						icon = ICON_DRIVE_HARDDISK;
					} else if (strcmp(value, "usb") == 0) { /* USB hard disk */
						icon = ICON_DRIVE_HARDDISK_USB;
					} else if (strcmp(value, "ieee1394") == 0) { /* firewire hard disk */
						icon = ICON_DRIVE_HARDDISK_IEEE1394;
					}
				}

				notify_notification_update(notification, TEXT_TOPIC, notifystr, icon);
				notify_notification_set_timeout(notification, notification_timeout);

				if (notify_notification_show(notification, &error) == FALSE) {
					g_printerr("%s: Error showing notification: %s\n", program, error->message);
					g_error_free(error);

					exit(EXIT_FAILURE);
				}

				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);
		}
	}

	/* report stopping to systemd */
#ifdef HAVE_SYSTEMD
	sd_notify(0, "STOPPING=1\nSTATUS=Stopping...");
#endif

	udev_unref(udev);

#ifdef HAVE_SYSTEMD
	sd_notify(0, "STATUS=Stopped. Bye!");
#endif

	return EXIT_SUCCESS;
}