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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
|
/* update-notifier.c
* Copyright (C) 2004 Lukas Lipka <lukas@pmad.net>
* (C) 2004 Michael Vogt <mvo@debian.org>
* (C) 2004 Michiel Sikkes <michiel@eyesopened.nl>
* (C) 2004-2006 Canonical
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <signal.h>
#include <gnome.h>
#include <grp.h>
#include <pwd.h>
#include <limits.h>
#include <gtk/gtk.h>
#include <glade/glade.h>
#include <libgnomevfs/gnome-vfs.h>
#include <libgnomevfs/gnome-vfs-utils.h>
#include <libgnomevfs/gnome-vfs-monitor.h>
#include "update-notifier.h"
#include "eggtrayicon.h"
#include "hal.h"
#include "update.h"
#include "hooks.h"
/* some prototypes */
extern gboolean up_get_clipboard (void);
gboolean update_timer_finished(gpointer data);
static void tray_destroyed_cb(GtkWidget *widget, void *data);
#define UPDATE_ICON_FILE PACKAGE_DATA_DIR"/pixmaps/update-icon.png"
#define HOOK_ICON_FILE PACKAGE_DATA_DIR"/pixmaps/hook-notifier.png"
#define REBOOT_ICON_FILE PACKAGE_DATA_DIR"/pixmaps/reboot-icon.png"
// the time when we check for fam events
#define TIMEOUT_FAM 1000*5 /* 5 sec */
// the timeout (in msec) for apt-get update (changes in
// /var/lib/apt/lists{/partial})
#define TIMEOUT_APT_GET_UPDATE 1000*30 /* 30 sec */
// the timeout (in sec) for a notification after apt/dpkg have finished
#define TIMEOUT_APT_RUN 60 /* 60 sec */
void
invoke_with_gksu(gchar *cmd, gchar *descr)
{
gchar *full_descr = NULL;
if(descr != NULL)
full_descr = g_strdup_printf(_("Please enter your password to run:\n %s"), descr);
//g_print("invoke_update_manager ()\n");
gchar *argv[5];
argv[0] = "/usr/bin/gksudo";
argv[1] = "--message";
argv[2] = full_descr;
argv[3] = cmd;
argv[4] = NULL;
g_spawn_async (NULL, argv, NULL, 0, NULL, NULL, NULL, NULL);
g_free(full_descr);
}
gboolean
trayapplet_create (TrayApplet *un, char *name, char *iconfile)
{
//g_print("trayicon_create()\n");
GdkPixbuf *pixbuf;
/* setup widgets */
un->tooltip = gtk_tooltips_new ();
un->tray_icon = egg_tray_icon_new (name);
gtk_widget_show(un->tray_icon);
un->eventbox = gtk_event_box_new ();
gtk_widget_show(un->eventbox);
un->icon = gtk_image_new ();
gtk_widget_show(un->icon);
/* load icon */
pixbuf = gtk_image_get_pixbuf (GTK_IMAGE(un->icon));
if (pixbuf)
g_object_unref (G_OBJECT (pixbuf));
pixbuf = gdk_pixbuf_new_from_file (iconfile, NULL);
gtk_image_set_from_pixbuf (GTK_IMAGE (un->icon), pixbuf);
/* build container */
gtk_container_add (GTK_CONTAINER (un->eventbox), un->icon);
gtk_container_add (GTK_CONTAINER (un->tray_icon), un->eventbox);
gtk_widget_hide(un->tray_icon);
return TRUE;
}
/*
the following files change:
on "install":
- /var/lib/dpkg/lock
- /var/lib/dpkg/ *
- /var/lib/update-notifier/dpkg-run-stamp
on "update":
- /var/lib/apt/lists/lock
- /var/lib/apt/lists/ *
- /var/lib/dpkg/lock
*/
void
monitor_cb(GnomeVFSMonitorHandle *handle,
const gchar *monitor_uri,
const gchar *info_uri,
GnomeVFSMonitorEventType event_type,
gpointer user_data)
{
gboolean dpkg_was_run = FALSE;
gboolean apt_get_update_runing = FALSE;
gboolean new_update_hook = FALSE;
gboolean reboot_required = FALSE;
static gboolean hook_pending = FALSE;
static time_t last_apt_action = 0;
static guint hook_timer = 0;
int i;
UpgradeNotifier *un = (UpgradeNotifier*)user_data;
#if 0
g_print("monitor_uri: %s\n", monitor_uri);
g_print("info_uri: %s\n", info_uri);
g_print("event_type: %i\n",event_type);
#endif
// we ignore lock file events because we can only get
// when a lock was taken, but not when it was removed
if(g_str_has_suffix(info_uri, "lock"))
return;
// look for apt-get install/update
if(g_str_has_prefix(info_uri,"file:///var/lib/apt/")
|| g_str_has_prefix(info_uri,"file:///var/cache/apt/")
|| strcmp(info_uri,"file:///var/lib/dpkg/status") == 0) {
// a timer is already runing, stop it and start a new one
if(un->update_finished_timer > 0) {
//g_print("removing old timer: %i\n", un->update_finished_timer);
g_source_remove(un->update_finished_timer);
un->update_finished_timer = 0;
}
apt_get_update_runing=TRUE;
}
if(strstr(info_uri, "/var/lib/update-notifier/dpkg-run-stamp")) {
dpkg_was_run = TRUE;
}
if(strstr(info_uri, REBOOT_FILE)) {
reboot_required = TRUE;
}
if(strstr(info_uri, HOOKS_DIR)) {
g_print("new hook!\n");
new_update_hook = TRUE;
}
#if 0
while(FAMPending(&un->fc)) {
if(FAMNextEvent(&un->fc, &fe) < 0) {
g_critical("FAMNextEvent() failed");
break;
}
// g_print("FAMPending() %i %s (reqnum %i)\n",
// fe.code,fe.filename, fe.fr.reqnum);
if(!(fe.code == FAMChanged || fe.code == FAMDeleted
|| fe.code == FAMCreated))
continue;
// we ignore lock file events because we can only get
// when a lock was taken, but not when it was removed
if(g_str_has_suffix(fe.filename, "lock"))
continue;
/* apt-get update */
for(i=0;un->frq_apt[i].reqnum != -1; i++) {
if(fe.fr.reqnum == un->frq_apt[i].reqnum) {
// a timer is already runing, stop it and start a new one
if(un->update_finished_timer > 0) {
//g_print("removing old timer: %i\n", un->update_finished_timer);
g_source_remove(un->update_finished_timer);
un->update_finished_timer = 0;
}
apt_get_update_runing=TRUE;
break;
}
}
/* apt-get install/remove/upgrade run */
if(strstr(fe.filename, "/var/lib/update-notifier/dpkg-run-stamp"))
dpkg_was_run = TRUE;
/* a update hook was registered */
if(fe.fr.reqnum == un->frq_hooks.reqnum)
new_update_hook = TRUE;
}
// FIXME: this is a stupid workaround because stupid gamin/inotify
// is oh so broken
if(!dpkg_was_run) {
static struct stat last_buf,buf;
stat("/var/lib/update-notifier/dpkg-run-stamp", &buf);
if(memcmp(&buf,&last_buf,sizeof(struct stat)) != 0) {
dpkg_was_run=TRUE;
last_buf = buf;
}
}
#endif
if(dpkg_was_run) {
update_check(un->update);
// the update must be finished, otherwise apt-get install
// wouldn't run
if(un->update_finished_timer > 0) {
update_apt_is_running(un->update, FALSE);
g_source_remove(un->update_finished_timer);
}
if(hook_pending) {
g_print("checking hooks now\n");
check_update_hooks(un->hook);
hook_pending = FALSE;
last_apt_action = 0;
}
}
if(apt_get_update_runing) {
update_apt_is_running(un->update, TRUE);
un->update_finished_timer = g_timeout_add(TIMEOUT_APT_GET_UPDATE,
update_timer_finished, un);
}
// show the hooks only after apt/dpkg are finished
time_t now = time(NULL);
if(new_update_hook) {
hook_pending = TRUE;
last_apt_action = now;
}
// FIXME: PORT THIS BITS TO GNOME-VFS
// the problem is that this code used to be called periodically,
// no it is only called when a gnome-vfs montior evetns happens
// we want to not display the notification immediately, but right
// after apt is finished. so we need to show them then and figure
// some way to do that
// if there is a hook pending, check that no apt/dpkg/etc is
// runing within the last N-sec
if(hook_pending) {
//g_print("hook_pending: now=%i last_action=%i (%i) \n", now, last_apt_action,now-last_apt_action);
if(apt_get_update_runing || dpkg_was_run) {
last_apt_action = now;
}
if( (now - last_apt_action) > TIMEOUT_APT_RUN) {
//g_print("checking hooks now\n");
check_update_hooks(un->hook);
hook_pending = FALSE;
last_apt_action = 0;
}
}
if (reboot_required)
reboot_check (un->reboot);
}
gboolean
file_monitor_periodic_check(gpointer data)
{
// FIXME: HACK -> this is run periodically to make sure that
// pending hooks are displayed, needs to be FIXED
// in a clean way (see comment in monitor_cb as well)
monitor_cb(NULL, "", "", 0, data);
return TRUE;
}
/* u_abort seems like an internal error notification.
* End user might not understand the message at all */
void u_abort(gchar *msg)
{
msg = g_strdup_printf("<span weight=\"bold\" size=\"larger\">%s</span>\n\n%s\n", _("Internal error"), msg);
gtk_dialog_run(gtk_message_dialog_new_with_markup(NULL,0,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE,
msg));
g_free(msg);
exit(1);
}
// FIXME: get the apt directories with apt-config or something
gboolean monitor_init(UpgradeNotifier *un)
{
int i;
GnomeVFSResult res;
GnomeVFSMonitorHandle *monitor_handle;
static const char *monitor_dirs[] = {
"/var/lib/apt/lists/", "/var/lib/apt/list/partial",
"/var/cache/apt/archives/", "/var/cache/apt/archives/partial",
HOOKS_DIR,
NULL};
for(i=0;monitor_dirs[i] != NULL;i++) {
res = gnome_vfs_monitor_add(&monitor_handle,
monitor_dirs[i],
GNOME_VFS_MONITOR_DIRECTORY,
monitor_cb, un);
}
res = gnome_vfs_monitor_add(&monitor_handle,
"/var/lib/dpkg/status",
GNOME_VFS_MONITOR_FILE,
monitor_cb,
un);
res = gnome_vfs_monitor_add(&monitor_handle,
"/var/lib/update-notifier/dpkg-run-stamp",
GNOME_VFS_MONITOR_FILE,
monitor_cb,
un);
res = gnome_vfs_monitor_add(&monitor_handle,
"/var/run/reboot-required",
GNOME_VFS_MONITOR_FILE,
monitor_cb,
un);
g_timeout_add (TIMEOUT_FAM, (GSourceFunc)file_monitor_periodic_check, un);
#if 0
FAMRequest frq;
// FIXME: if the number of directories is increased that are monitored
// for apt, make sure that they alaway fir in UpgradeNotifier.frq_apt
int aptindx=0;
/* setup FAM/GAMIN, monitor the importend directories */
if(FAMOpen(&un->fc) >= 0) {
// monitor for apt getting updates
if(FAMMonitorDirectory(&un->fc, "/var/lib/apt/lists/",
&un->frq_apt[aptindx++], NULL))
u_abort("FAMMonitorFile() failed, make sure that fam/gamin "
"is installed");
if(FAMMonitorDirectory(&un->fc, "/var/lib/apt/lists/partial/",
&un->frq_apt[aptindx++], NULL))
u_abort("FAMMonitorFile() failed, , make sure that fam/gamin "
"is installed");
// monitor for apt geting packages
if(FAMMonitorDirectory(&un->fc, "/var/cache/apt/archives/",
&un->frq_apt[aptindx++], NULL))
u_abort("FAMMonitorFile() failed, , make sure that fam/gamin "
"is installed");
if(FAMMonitorDirectory(&un->fc, "/var/cache/apt/archives/partial/",
&un->frq_apt[aptindx++], NULL))
u_abort("FAMMonitorFile() failed, , make sure that fam/gamin "
"is installed");
// dpkg database
if(FAMMonitorFile(&un->fc,
"/var/lib/dpkg/status",
&un->frq_apt[aptindx++], NULL))
u_abort("FAMMonitorFile() failed, make sure that fam/gamin "
"is installed");
// monitor for hooks
if(FAMMonitorDirectory(&un->fc, HOOKS_DIR,
&un->frq_hooks, NULL))
u_abort("FAMMonitorFile() failed, make sure that fam/gamin "
"is installed");
// monitor for installs
if(FAMMonitorFile(&un->fc,
"/var/lib/update-notifier/dpkg-run-stamp",
&frq, NULL))
u_abort("FAMMonitorFile() failed, make sure that fam/gamin "
"is installed");
} else {
u_abort("FAMOpen() failed, make sure that fam/gamin "
"is installed");
}
un->frq_apt[aptindx].reqnum = -1;
#endif
return TRUE;
}
static gboolean
tray_icons_init(UpgradeNotifier *un)
{
/* new upates tray icon */
un->update = g_new0 (TrayApplet, 1);
trayapplet_create(un->update, "update-notifier", UPDATE_ICON_FILE);
update_tray_icon_init(un->update);
/* a destroy signal handler (e.g. when the gnome-panel is restarted */
g_signal_connect(G_OBJECT(un->update->tray_icon), "destroy",
G_CALLBACK(tray_destroyed_cb), un);
/* update hook icon*/
un->hook = g_new0 (TrayApplet, 1);
trayapplet_create(un->hook, "hook-notifier", HOOK_ICON_FILE);
hook_tray_icon_init(un->hook);
/* reboot required icon */
un->reboot = g_new0 (TrayApplet, 1);
trayapplet_create(un->reboot, "reboot-notifier", REBOOT_ICON_FILE);
reboot_tray_icon_init(un->reboot);
return FALSE; // for the tray_destroyed_cb
}
static void
tray_destroyed_cb(GtkWidget *widget, void *data)
{
g_critical("tray icon destroyed\n");
// FIXME: handle this situation
g_timeout_add(1000, (GSourceFunc*)(tray_icons_init), data);
}
// this function checks if the user is in the admin group
// if there is no admin group, we return true becuase there
// is no way to figure if the user is a admin or not
static gboolean
in_admin_group()
{
int i, ng = 0;
gid_t *groups = NULL;
struct passwd *pw = getpwuid(getuid());
if (pw == NULL)
return TRUE;
struct group *grp = getgrnam("admin");
if(grp == NULL)
return TRUE;
if (getgrouplist(pw->pw_name, pw->pw_gid, NULL, &ng) < 0) {
groups = (gid_t *) malloc(ng * sizeof (gid_t));
getgrouplist(pw->pw_name, pw->pw_gid, groups, &ng);
}
for(i=0;i<ng;i++) {
if(groups[i] == grp->gr_gid) {
return TRUE;
}
}
if(groups != NULL)
free(groups);
return FALSE;
}
int
main (int argc, char *argv[])
{
GnomeClient *client;
UpgradeNotifier *un;
gnome_program_init (PACKAGE, PACKAGE_VERSION,
LIBGNOMEUI_MODULE,
argc, argv,
GNOME_PARAM_NONE);
notify_init("update-notifier");
bindtextdomain(PACKAGE, PACKAGE_LOCALE_DIR);
bind_textdomain_codeset(PACKAGE, "UTF-8");
textdomain(PACKAGE);
//g_print("starting update-notifier\n");
// check if we should run at all (see HideAdminTools spec
// on https://wiki.ubuntu.com/HideAdminToolsToUsers)
if(!in_admin_group()) {
g_warning("not starting because user is not in admin group\n");
exit(0);
}
client = gnome_master_client ();
if (up_get_clipboard ())
gnome_client_set_restart_style (client, GNOME_RESTART_ANYWAY);
else {
gnome_client_set_restart_style (client, GNOME_RESTART_NEVER);
g_warning ("already running?\n");
return 1;
}
/* Make sure we die when the session dies */
g_signal_connect (G_OBJECT (client), "die",
G_CALLBACK (gtk_main_quit), NULL);
/* Create the UpgradeNotifier object */
un = g_new0 (UpgradeNotifier, 1);
// check for .update-notifier dir and create if needed
gchar *dirname = g_strdup_printf("%s/.update-notifier",
g_get_home_dir());
if(!g_file_test(dirname, G_FILE_TEST_IS_DIR))
g_mkdir(dirname, 0700);
g_free(dirname);
// create the icons
tray_icons_init(un);
// init hal (needs to be done _after_ the icons are created)
/* setup hal so that inserted cdroms can be checked */
LibHalContext *hal_ctx = up_do_hal_init();
if (!hal_ctx) {
u_abort("failed to initialize HAL!\n");
return 1;
}
libhal_ctx_set_user_data(hal_ctx, un);
// init fam
monitor_init(un);
/* Set a time-out for the fam check */
//g_timeout_add (TIMEOUT_FAM, (GSourceFunc)file_monitor_check, un);
/* Start the main gtk loop */
gtk_main ();
return 0;
}
|