Skip to contentSkip to conversation

wrong pid in siginfo_t

PATCH Ulrich Drepper 1 participant 1 message Jul 4, 2003

lore
Share & tools
lore
#0 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

If a signal is sent via kill() or tkill() the kernel fills in the wrong PID value in the siginfo_t structure (obviously only if the handler has SA_SIGINFO set). POSIX specifies the the si_pid field is filled with the process ID. The kernel currently fills in the thread ID. The thread IDs are not useful at all at userlevel. They are not exposed anywhere.

One might argue that sending up the ID of the thread as well is a good thing but I don't agree. This is what sigqueue is for: one can attach arbitrary data to the signal and get it transmitted (the sigval_t parameter).

I've attached a patch for the current 2.5 kernel and a test case. Whoever collects kernel tests might want to add the program.

- --
- --------------. ,-. 444 Castro Street
Ulrich Drepper \ ,-----------------' \ Mountain View, CA 94041 USA
Red Hat --' drepper at redhat.com ---------------------------
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQE/BewL2ijCOnn/RHQRAtnYAKCmG6vX3YFo9ko8kjwzvNObxrklfACfVga3 43za5G4ljJwJD1SZEqrr+eU=
=lfde
-----END PGP SIGNATURE-----

signal.c renamed from signal.c-save +2 -2
@@ -2081,7 +2081,7 @@ sys_kill(int pid, int sig)
 	info.si_signo = sig;
 	info.si_errno = 0;
 	info.si_code = SI_USER;
-	info.si_pid = current->pid;
+	info.si_pid = current->tgid;
 	info.si_uid = current->uid;
 
 	return kill_something_info(sig, &info, pid);
@@ -2104,7 +2104,7 @@ sys_tkill(int pid, int sig)
 	info.si_signo = sig;
 	info.si_errno = 0;
 	info.si_code = SI_TKILL;
-	info.si_pid = current->pid;
+	info.si_pid = current->tgid;
 	info.si_uid = current->uid;
 
 	read_lock(&tasklist_lock);

#include <semaphore.h>
#include <signal.h>
#include <unistd.h>

static int failed;
static pid_t pid;
static uid_t uid;
static sem_t sem;
static int is_kill;

#define THESIG SIGRTMIN

static void
sh (int sig, siginfo_t *si, void *ctx)
{
if (sig != si->si_signo)

    {
      printf ("sig != si->si_signo: %d vs %d\n", sig, (int) si->si_signo);
      failed = 1;
    }
  if (si->si_code != is_kill ? SI_USER : SI_QUEUE)
    {
      printf ("si->si_code wrong: %d\n", si->si_code);
      failed = 1;
    }
  if (si->si_pid != pid)
    {
      printf ("si->si_pid wrong: %d vs %d\n", (int) si->si_pid, (int) pid);
      failed = 1;
    }
  if (si->si_uid != uid)
    {
      printf ("si->si_uid wrong: %d vs %d\n", (int) si->si_uid, (int) uid);
      failed = 1;
    }

sem_post (&sem);
}

static void *
tf (void *arg)
{
puts ("trying kill");
is_kill = 1;
kill (pid, THESIG);
sem_wait (&sem);

puts ("trying sigqueue");
sigval_t sv;
sv.sival_int = 42;
is_kill = 0;
sigqueue (pid, THESIG, sv);
sem_wait (&sem);
}

int
main (void)
{
pid = getpid ();
uid = getuid ();

  struct sigaction sa;
  sa.sa_sigaction = sh;
  sa.sa_flags = SA_SIGINFO;
  sigemptyset (&sa.sa_mask);
  sigaction (THESIG, &sa, NULL);

  sem_init (&sem, 0, 0);

  pthread_t th;
  puts ("starting 1st thread");
  pthread_create (&th, NULL, tf, NULL);
  pthread_join (th, NULL);
  puts ("starting 2nd thread");
  pthread_create (&th, NULL, tf, NULL);
  pthread_join (th, NULL);

return failed;
}