mercredi 13 février 2013

Running a wxWidgets/GTK+ setgid application

When trying to run a wxWidgets/GTK+ setgid application, you'll get the following error message:

(process:XXXX): Gtk-WARNING **: This process is currently running
setuid or setgid.
This is not a supported use of GTK+. You must create a helper
program instead. For further details, see:

http://www.gtk.org/setuid.html

Refusing to initialize GTK+.

While I agree that a GUI application should not run as setuid, I do believe there are good reasons for running a setgid GUI application.

To work-around this issue, it is possible to set the real group id to the effective group id before gtk is initialized.
wxWidgets sadly does not provide any mean to add code before the gtk initialization.

It is still possible to use the GCC constructor attribute:

void gtk_init_hack(void) __attribute__((constructor));
void gtk_init_hack(void) { setregid(getegid(), -1); } // This will always run before main()

The only drawback is, if the application creates files or folder, they will inherit the same group as the setgid application. This can be fixed using a chown() function call afterwards.

lundi 10 décembre 2012

tv_grab_fr_telerama: supprimer les programmes cryptés de Canal+ de la grille des programmes

MAJ 2012-12-22 : le patch initial a été intégré dans tv_grab_fr_telerama. Ce post a été mis à jour en conséquence.

Pour utilisateurs avertis !

Faire une sauvegarde de la base de données au préalable:
$ mysqldump -uuser -ppassword --add-drop-table -B mythconverg | gzip - > mysqlbackup.sql.gz

En cas de problème, la restoration se fait comme suit:
$ gunzip mysqlbackup.sql.gz
$ mysql -uuser -ppassword < mysqlbackup.sql

Récupérer tv_grab_fr_telerama :
$ sudo wget http://www.number6.ch/tools/tv_grab_fr_telerama -O /usr/bin/tv_grab_fr_telerama
$ sudo chmod +x /usr/bin/tv_grab_fr_telerama
Lancer mythtv-setup, sélectionner le grabber, et ajouter -- "--no_cryptedcplus --no_cryptedpprem" comme argument de mythfilldatabase.
NE PAS EXECUTER mythfilldatabase EN QUITTANT mythtv-setup.

Mettre à jour la grille des programmes :
(on supprime d'abord les programmes déjà présents)
$ mysql -uuser -ppassword mythconverg
mysql> select chanid from channel where name='Canal+';
+--------+
| chanid |
+--------+
| 1004 |
+--------+
1 row in set (0.01 sec)
mysql> delete from program where chanid=1004;
Query OK, 201 rows affected (0.11 sec)
mysql> quit
$ sudo -H -u mythtv mythfilldatabase -- "--no_cryptedcplus --no_cryptedpprem"

vendredi 6 juillet 2012

DVB-T w_scan

#only if there is no free firmware for the DVB-T card
sudo apt-get install linux-firmware-nonfree
#install w_scan
sudo apt-get install w-scan
#scan frequencies; FR is the country code
w_scan -x -c FR > Rennes
#find channels
scan Rennes > ~/.vlc/channels.conf

jeudi 22 décembre 2011

Recursive command-line replace

$ grep -rl string1 somedir/ | xargs sed -i 's/string1/string2/g'
The above line will also modify files in hidden folders, such as files in .svn directories!
To avoid modifying files in .svn subdirs:
 $ grep -rl string1 somedir/ | grep -v .svn | xargs sed -i 's/string1/string2/g'

jeudi 24 février 2011

Get the username and home directory in a C program

Don't use getlogin or cuserid functions.
Don't try to guess the home directory from the username (/home/username is not working all the time).

Use getpwuid instead:

#include <sys/types.h>
#include <pwd.h>
[...]
char* username = getpwuid(getuid())->pw_name;
char* homedir = getpwuid(getuid())->pw_dir;

This works all the time, in particular if the process is not controlled by a terminal.

Stdio buffering

Nice article about stdio buffering:

http://www.pixelbeat.org/programming/stdio_buffering/

I recently had the following problem: I launch p1 | p2 (piped processes, p2 is the receiver), and p2 isn't receiving on its stdin the data (a few bytes) p1 is writing to its stdout. After a while I discovered that the data is buffered (4096 bytes if the process output is not redirected to a terminal, else it is line buffered).

To make it line-buffered all the time, use setlinebuf:

#include <stdio.h>
[...]
setlinebuf(stdout);

Since there is no setlinebuf in windows, a portable manner to do this is:

#include <stdio.h>
[...]
printf(line);
fflush(stdout); //to do after each printed line