Permalink
Browse files

Migrate to using libtinynotify.

  • Loading branch information...
mgorny committed Aug 19, 2011
1 parent 1f2035a commit 79978ccdac57d145184be90fc10709fcc7225246
Showing with 49 additions and 28 deletions.
  1. +2 −0 Makefile.am
  2. +2 −1 README
  3. +3 −1 configure.ac
  4. +42 −26 sw-notify-send.c
View
@@ -1,3 +1,5 @@
bin_PROGRAMS = sw-notify-send
sw_notify_send_SOURCES = sw-notify-send.c
+sw_notify_send_CPPFLAGS = $(LIBTINYNOTIFY_CFLAGS)
+sw_notify_send_LDADD = $(LIBTINYNOTIFY_LIBS)
View
3 README
@@ -20,8 +20,9 @@ System requirements
-------------------
* The `proc` library (which can be found in the procps package),
-* the `notify-send` program (which is a part of libnotify).
+* [libtinynotify][1] with `--enable-cli`.
+[1]:https://github.com/mgorny/libtinynotify/
How to use
----------
View
@@ -1,5 +1,5 @@
AC_PREREQ([2.60])
-AC_INIT([sw-notify-send], [0.1.1])
+AC_INIT([sw-notify-send], [0.2])
AC_CONFIG_AUX_DIR([build-aux])
AM_INIT_AUTOMAKE([1.6 foreign dist-bzip2 no-dependencies])
@@ -11,6 +11,8 @@ AC_CHECK_HEADERS([sysexits.h])
AC_CHECK_LIB([proc], [readproc],,
[AC_MSG_ERROR([Error: required library proc not found! Please install procps.])])
+PKG_CHECK_MODULES([LIBTINYNOTIFY], [libtinynotify libtinynotify-cli])
+
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
View
@@ -1,7 +1,6 @@
/* System-wide notify-send wrapper
* (c) 2010 Michał Górny
* Released under the terms of 3-clause BSD license
- * (link with -lproc)
*/
#ifdef HAVE_CONFIG_H
@@ -20,6 +19,9 @@
#include <proc/readproc.h>
+#include <tinynotify.h>
+#include <tinynotify-cli.h>
+
#ifdef HAVE_SYSEXITS_H
# include <sysexits.h>
#else
@@ -90,45 +92,56 @@ const char* getroot(int pid) {
/* Fork and call notify-send for particular dbus session. */
int send_notify(char* const display, char* const xauth,
- uid_t uid, const char* const root, char* const argv[]) {
+ uid_t uid, const char* const root, NotifySession s, Notification n) {
+ int ret = 0;
+ uid_t old_uid = geteuid();
- switch (fork()) {
- case 0:
#ifdef HAVE_CHROOT
- if (root)
- CANFAIL(chroot(root));
+ if (root)
+ CANFAIL(chroot(root));
#endif
- CANFAIL(setuid(uid));
- CANTFAIL(putenv(display));
- CANTFAIL(putenv(xauth));
-
- execvp("notify-send", argv);
- perror("execvp() returned");
- exit(1);
- return EX_OSERR;
- break;
- case -1:
- perror("fork() failed (aborting)");
- return EX_OSERR;
- break;
- default:
- wait(NULL);
- return EX_OK;
- }
+ CANFAIL(seteuid(uid));
+
+ CANTFAIL(putenv(display));
+ CANTFAIL(putenv(xauth));
+
+ notify_session_disconnect(s); /* ensure to get new connection */
+ if (!notification_send(n, s))
+ ret = 1;
+
+ CANTFAIL(seteuid(old_uid));
+#ifdef HAVE_CHROOT
+ if (root)
+ CANTFAIL(chroot(".")); /* escape the chroot */
+#endif
+
+ return ret;
}
-int main(int argc, char* const argv[]) {
+int main(int argc, char* argv[]) {
/* We need the command line and environment. Username would be nice
* too but readproc() shortens it and we need to getpwuid() anyway. */
- PROCTAB *proc = openproc(PROC_FILLCOM | PROC_FILLENV);
+ PROCTAB *proc;
proc_t *p = NULL;
int ret = EX_UNAVAILABLE;
+ NotifySession s;
+ Notification n;
+
+ CANTFAIL(chdir("/"));
+
+ n = notification_new_from_cmdline(argc, argv, PACKAGE " " VERSION);
+ if (!n)
+ return EX_USAGE;
+
+ proc = openproc(PROC_FILLCOM | PROC_FILLENV);
if (!proc) {
fputs("FATAL: openproc() failed", stderr);
+ notification_free(n);
return EX_OSERR;
}
+ s = notify_session_new(PACKAGE, NULL);
while (((p = readproc(proc, p)))) {
if (validateproc(p)) {
char* const display = FINDENV(p, "DISPLAY");
@@ -150,13 +163,16 @@ int main(int argc, char* const argv[]) {
xauth = xauthbuf;
}
- ret = send_notify(display, xauth, p->euid, getroot(p->tgid), argv);
+ if (send_notify(display, xauth, p->euid, getroot(p->tgid), s, n))
+ ret = EX_OK;
if (xauthbuf)
free(xauthbuf);
}
}
+ notify_session_free(s);
closeproc(proc);
+ notification_free(n);
return ret;
}

0 comments on commit 79978cc

Please sign in to comment.