~ubuntu-core-dev/update-notifier/ubuntu

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
/* 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-2009 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 <stdlib.h>
#include <signal.h>
#include <grp.h>
#include <pwd.h>
#include <limits.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <gtk/gtk.h>
#include <gio/gio.h>

#include "update-notifier.h"
#include "update.h"
#include "hooks.h"
#include "gdu.h"
#include "reboot.h"
#include "crash.h"
#include "avahi.h"

/* some prototypes */
extern gboolean up_get_clipboard (void);
gboolean update_timer_finished(gpointer data);

// 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) when a further activity from dpkg/apt
// causes the applet to "ungray"
#define TIMEOUT_APT_RUN 120 /* 120 sec */

// force statup even if the user is not in the admin group
static gboolean FORCE_START=FALSE;

// force gksu for all menu actions
static gboolean FORCE_GKSU=FALSE;

// delay on startup (to make session startup faster)
static int STARTUP_DELAY=5;

// global debug options
int HOOK_DEBUG = 0;
int UPDATE_DEBUG = 0;
int INOTIFY_DEBUG = 0;

// logging stuff
static void debug_log_handler (const gchar   *log_domain,
                              GLogLevelFlags log_level,
                              const gchar   *message,
                              gpointer       user_data)
{
   if (HOOK_DEBUG > 0 && strcmp(log_domain, "hooks") == 0)
       g_log_default_handler (log_domain, log_level, message, user_data);
   if (UPDATE_DEBUG > 0 && strcmp(log_domain, "update") == 0)
       g_log_default_handler (log_domain, log_level, message, user_data);
   if (INOTIFY_DEBUG > 0 && strcmp(log_domain, "inotify") == 0)
       g_log_default_handler (log_domain, log_level, message, user_data);

}

static inline void
g_debug_inotify(const char *msg, ...)
{
   va_list va;
   va_start(va, msg);
   g_logv("inotify",G_LOG_LEVEL_DEBUG, msg, va);
   va_end(va);
}

void invoke(const gchar *cmd, const gchar *desktop, gboolean with_gksu)
{
   if(with_gksu || FORCE_GKSU)
      invoke_with_gksu(cmd, desktop, FALSE);
   else
      g_spawn_command_line_async(cmd, NULL);
}

void
invoke_with_gksu(const gchar *cmd, const gchar *descr, gboolean whole_message)
{
        //g_print("invoke_update_manager ()\n");
        gchar *argv[5];
	argv[0] = "/usr/bin/gksu";
	argv[1] = whole_message ? "--message" : "--desktop";
	argv[2] = (gchar*)descr;
	argv[3] = (gchar*)cmd;
	argv[4] = NULL;

	g_spawn_async (NULL, argv, NULL, 0, NULL, NULL, NULL, NULL);
}



gboolean
trayapplet_create (TrayApplet *un, char *name)
{
        //g_print("trayicon_create()\n");

	/* setup widgets */
	un->tray_icon = gtk_status_icon_new_from_icon_name (name);
	un->name = name;
	gtk_status_icon_set_visible (un->tray_icon, FALSE);

	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
  - /var/lib/apt/periodic/update-success-stamp
*/
void
monitor_cb(GFileMonitor *handle,
	   GFile *monitor_f,
	   GFile *other_monitor_f,
	   GFileMonitorEvent event_type,
	   gpointer user_data)
{
   UpgradeNotifier *un = (UpgradeNotifier*)user_data;

   gchar *info_uri = g_file_get_path(monitor_f);
#if 0
   g_debug_inotify("info_uri: %s\n", info_uri);
   g_debug_inotify("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,"/var/lib/apt/") 
      || g_str_has_prefix(info_uri,"/var/cache/apt/")
      || strcmp(info_uri,"/var/lib/dpkg/status") == 0) {
      g_debug_inotify("apt_get_running=TRUE");
      un->apt_get_runing=TRUE;
   } 
   if(strstr(info_uri, "/var/lib/update-notifier/dpkg-run-stamp") ||
      strstr(info_uri, "/var/lib/apt/periodic/update-success-stamp")) {
      g_debug_inotify("dpkg_was_run=TRUE");
      un->dpkg_was_run = TRUE;
   } 
   if(strstr(info_uri, REBOOT_FILE)) {
      g_debug_inotify("reboot required\n");
      un->reboot_pending = TRUE;
   }
   if(strstr(info_uri, HOOKS_DIR)) {
      g_debug_inotify("new hook!\n");
      un->hook_pending = TRUE;
   }
   if(strstr(info_uri, CRASHREPORT_DIR)) {
      g_debug_inotify("crashreport found\n");
      un->crashreport_pending = TRUE;
   }
   if(strstr(info_uri, UNICAST_LOCAL_AVAHI_FILE)) {
      g_debug_inotify("avahi disabled due to unicast .local domain\n");
      un->unicast_local_avahi_pending = TRUE;
   }
}

/*
 * We periodically check here what actions happend in this "time-slice". 
 * This can be:
 * - dpkg_was_run=TRUE: set when apt wrote the "dpkg-run-stamp" file
 * - apt_get_runing: set when apt/dpkg activity is detected (in the 
 *                   lists-dir, archive-cache, or /var/lib/dpkg/status
 * - hook_pending: we have new upgrade hoook information
 * - reboot_pending: we need to reboot
 * - crashreport_pending: we have a new crashreport
 * - unicast_local_avahi_pending: avahi got disabled due to a unicast .local domain
 *
 */
gboolean file_monitor_periodic_check(gpointer data)

{
   UpgradeNotifier *un = (UpgradeNotifier *)data;

   // we are not ready yet, wait for the next timeslice
   if((un->reboot == NULL) || (un->crashreport == NULL))
      return TRUE;

   // DPkg::Post-Invoke has written a stamp file, that means a install/remove
   // operation finished, we can show hooks/reboot notifications then
   if(un->dpkg_was_run) {

      // check updates
      update_check(un->update);

      // any apt-get update  must be finished, otherwise 
      // apt-get install wouldn't be finished
      update_apt_is_running(un->update, FALSE);
      if(un->update_finished_timer > 0) 
	 g_source_remove(un->update_finished_timer);
      
      // show pending hooks/reboots
      if(un->hook_pending) {
	 //g_print("checking hooks now\n");
	 check_update_hooks(un->hook);
	 un->hook_pending = FALSE;
      }
      if(un->reboot_pending) {
	 //g_print("checking reboot now\n");
	 reboot_check (un->reboot);
	 un->reboot_pending = FALSE;
      }

      // apt must be finished when a new stamp file was writen, so we
      // reset the apt_get_runing time-slice field because its not 
      // important anymore (it finished runing)
      //
      // This may leave a 5s race condition when a apt-get install finished
      // and something new (apt-get) was started
      un->apt_get_runing = FALSE;
      un->last_apt_action = 0;
   }

   // apt-get update/install or dpkg is runing (and updates files in 
   // it's list/cache dir) or in /var/lib/dpkg/status
   if(un->apt_get_runing) 
      update_apt_is_running(un->update, TRUE);

   // update time information for apt/dpkg
   time_t now = time(NULL);
   if(un->apt_get_runing) 
      un->last_apt_action = now;

   // no apt operation for a long time
   if(un->last_apt_action > 0 &&
      (now - un->last_apt_action) >  TIMEOUT_APT_RUN) {
      update_apt_is_running(un->update, FALSE);
      update_check(un->update);
      un->last_apt_action = 0;
   }

   if(un->crashreport_pending) {
      g_print("checking for valid crashreport now\n");
      crashreport_check (un->crashreport);
      un->crashreport_pending = FALSE;
   }

   if(un->unicast_local_avahi_pending) {
      g_print("checking for disabled avahi due to unicast .local domain now\n");
      avahi_disabled_check ();
      un->unicast_local_avahi_pending = FALSE;
   }

   // reset the bitfields (for the next "time-slice")
   un->dpkg_was_run = FALSE;
   un->apt_get_runing = FALSE;

   return TRUE;
}




/* u_abort seems like an internal error notification.
 * End user might not understand the message at all */
void u_abort(gchar *msg)
{
   const char *fmt = "<span weight=\"bold\" size=\"larger\">%s</span>\n\n%s\n";
   gtk_dialog_run(GTK_DIALOG(gtk_message_dialog_new_with_markup(NULL, 0,
						     GTK_MESSAGE_ERROR,
						     GTK_BUTTONS_CLOSE,
						     fmt,
						     _("Internal error"),
						     msg)));
   g_free(msg);
   exit(1);
}

// FIXME: get the apt directories with apt-config or something
gboolean 
monitor_init(UpgradeNotifier *un)
{
   int i;
   GFileMonitor *monitor_handle;

   // monitor thise dirs
   static const char *monitor_dirs[] = { 
      "/var/lib/apt/lists/", "/var/lib/apt/lists/partial/", 
      "/var/cache/apt/archives/", "/var/cache/apt/archives/partial/", 
      HOOKS_DIR, 
      CRASHREPORT_DIR,
      NULL};
   for(i=0;monitor_dirs[i] != NULL;i++) {
      GError *error = NULL;
      GFile *gf = g_file_new_for_path(monitor_dirs[i]);
      monitor_handle = g_file_monitor_directory(gf, 0, NULL, &error);
      if(monitor_handle)
	 g_signal_connect(monitor_handle, "changed", (GCallback)monitor_cb, un);
      else
	 g_warning("can not add '%s'\n", monitor_dirs[i]);
   }

   // and those files
   static const char *monitor_files[] = { 
      "/var/lib/dpkg/status", 
      "/var/lib/update-notifier/dpkg-run-stamp", 
      "/var/lib/apt/periodic/update-success-stamp",
      REBOOT_FILE,
      UNICAST_LOCAL_AVAHI_FILE,
      NULL};
   for(i=0;monitor_files[i] != NULL;i++) {
      GError *error = NULL;
      GFile *gf = g_file_new_for_path(monitor_files[i]);
      monitor_handle = g_file_monitor_file(gf, 0, NULL, &error);
      if(monitor_handle)
	 g_signal_connect(monitor_handle, "changed", (GCallback)monitor_cb, un);
      else
	 g_warning("can not add '%s'\n", monitor_dirs[i]);
   }

   g_timeout_add (TIMEOUT_FAM, (GSourceFunc)file_monitor_periodic_check, un);


   return TRUE;
}




static gboolean
tray_icons_init(UpgradeNotifier *un)
{
   //g_debug_inotify("tray_icons_init");

   /* new upates tray icon */
   un->update = g_new0 (TrayApplet, 1);

   // check if the updates icon should be displayed
   if (in_admin_group() || FORCE_START) {
      trayapplet_create(un->update, "software-update-available");
      update_tray_icon_init(un->update);
   } else 
      un->update = NULL;

   /* update hook icon*/
   un->hook = g_new0 (TrayApplet, 1);
   trayapplet_create(un->hook, "hook-notifier");
   hook_tray_icon_init(un->hook);

   /* reboot required icon */
   un->reboot = g_new0 (TrayApplet, 1);
   trayapplet_create(un->reboot, "reboot-notifier");
   reboot_tray_icon_init(un->reboot);

   /* crashreport detected icon */
   un->crashreport = g_new0 (TrayApplet, 1);
   trayapplet_create(un->crashreport, "apport");
   crashreport_tray_icon_init(un->crashreport);

   return FALSE; // for the tray_destroyed_cb
}

gboolean
system_user()
{
   /* do not start for system users, e.g. the guest session 
    * (see /usr/share/gdm/guest-session/guest-session-setup.sh)
    */
   int end_system_uid = 500;
   GConfClient *gconf = gconf_client_get_default();
   if (gconf) {
      int i = gconf_client_get_int(gconf, GCONF_KEY_END_SYSTEM_UIDS, NULL);
      if (i>0)
	 end_system_uid = i;
   }

   uid_t uid = getuid();
   if (uid < end_system_uid)
      return TRUE;
   return FALSE;
}

// 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
gboolean
in_admin_group()
{
   int i, ng = 0;
   gid_t *groups = NULL;

   struct group *grp = getgrnam("admin");
   if(grp == NULL) 
      return TRUE;

   ng = getgroups (0, NULL);
   groups = (gid_t *) malloc (ng * sizeof (gid_t));

   i = getgroups (ng, groups);
   if (i != ng) {
     free (groups);
     return TRUE;
   }
   
   for(i=0;i<ng;i++) {
      if(groups[i] == grp->gr_gid) {
        free(groups);
        return TRUE;
      }
   }
   
   if(groups != NULL)
      free(groups);

   return FALSE;
}

static GOptionEntry entries[] = 
{
   { "debug-hooks", 0, 0, G_OPTION_ARG_NONE, &HOOK_DEBUG, "Enable hooks debugging"},
   { "debug-updates", 0, 0, G_OPTION_ARG_NONE, &UPDATE_DEBUG, "Enable updates/autolaunch debugging"},
   { "debug-inotify", 0, 0, G_OPTION_ARG_NONE, &INOTIFY_DEBUG, "Enable inotify debugging"},
   { "force", 0, 0, G_OPTION_ARG_NONE, &FORCE_START, "Force start even if the user is not in the admin group"},
   { "force-use-gksu", 0, 0, G_OPTION_ARG_NONE, &FORCE_GKSU, "Force running all commands (update-manager, synaptic) with gksu" },
   { "startup-delay", 0, 0, G_OPTION_ARG_INT, &STARTUP_DELAY, "Delay startup by given amount of seconds" },
   { NULL }
};

int 
main (int argc, char **argv)
{
	UpgradeNotifier *un;

	// init
	gtk_init_with_args (&argc, &argv, 
			    _("- inform about updates"), entries, 
			    "update-notifier", NULL);

	notify_init("update-notifier");
        bindtextdomain(PACKAGE, PACKAGE_LOCALE_DIR);
        bind_textdomain_codeset(PACKAGE, "UTF-8");
        textdomain(PACKAGE);

	// setup a custom debug log handler
	g_log_set_handler ("inotify", G_LOG_LEVEL_DEBUG,
			   debug_log_handler, NULL);
	g_log_set_handler ("hooks", G_LOG_LEVEL_DEBUG,
			   debug_log_handler, NULL);
	g_log_set_handler ("update", G_LOG_LEVEL_DEBUG,
			   debug_log_handler, NULL);

	g_set_application_name (_("update-notifier"));
	gtk_window_set_default_icon_name ("update-notifier");

	//g_print("starting update-notifier\n");

	// do not run as system user (e.g. guest user)
	if (system_user() && !FORCE_START) {
	   g_warning("not starting for system user\n");
	   exit(0);
	}

	// check if it is running already
	if (!up_get_clipboard ()) {
	   g_warning ("already running?\n");
	   return 1;
	}

	/* 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);

	// delay icon creation for 30s so that the desktop 
	// login is not slowed down by update-notifier
	g_timeout_add_seconds(STARTUP_DELAY, 
			      (GSourceFunc)(tray_icons_init), un);

        // initial check for avahi
        avahi_disabled_check();
	
	/* setup hal so that inserted cdroms can be checked */
	if(!up_do_hal_init(un)) {
	   u_abort("failed to initialize hal!\n");
	   return 1;
	}
	
	// init gio file monitoring
	monitor_init (un);
	
	/* Start the main gtk loop */
	gtk_main ();

	return 0;
}