public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Ulrich Drepper <drepper@redhat.com>
To: Linus Torvalds <torvalds@osdl.org>, Andrew Morton <akpm@osdl.org>,
	linux-kernel@vger.kernel.org
Subject: More ->pid to ->tgid changes
Date: Thu, 28 Aug 2003 10:23:44 -0700	[thread overview]
Message-ID: <3F4E3AA0.9030808@redhat.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 2395 bytes --]

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

One more overlooked area where the thread group ID has to be used.
Compile and run the following code.  The program should always exit with
value zero, but currently it doesn't if a parameter is passed (i.e., if
the semaphore is modified in a thread other than the main one).  If
somebody wants to add the code to a kernel test suite, feel free.

The attached patch is against the current 2.5 tree.

#define _GNU_SOURCE
#include <errno.h>
#include <error.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/sem.h>
#include <sys/wait.h>


union semun
{
  int val;
};

static int sem;

static void
eh (void)
{
  semctl (sem, 0, IPC_RMID);
}

static void
second_process (void)
{
  int val = semctl (sem, 0, GETPID);
  printf ("parent PID: %ld\nsem PID: %d\n", (long int) getppid (), val);
  exit (getppid () != val);
}

static void
setval (void)
{
  union semun su = { .val = 1 };
  if (semctl (sem, 0, SETVAL, su) == -1)
    error (EXIT_FAILURE, errno, "semctl failed");
}

static void *
tf (void *arg)
{
  setval ();
  return NULL;
}

int
main (int argc, char *argv[])
{
  sem = semget (IPC_PRIVATE, 1, IPC_CREAT | IPC_EXCL | 0666);
  if (sem == -1)
    error (EXIT_FAILURE, errno, "semget failed");

  atexit (eh);

  if (argc == 1)
    setval ();
  else
    {
      pthread_t th;
      int r = pthread_create (&th, NULL, tf, NULL);
      if (r != 0)
        error (EXIT_FAILURE, r, "pthread_create failed");
      r = pthread_join (th, NULL);
      if (r != 0)
        error (EXIT_FAILURE, r, "pthread_join failed");
    }

  pid_t pid = fork ();
  if (pid == -1)
    error (EXIT_FAILURE, errno, "fork failed");
  if (pid == 0)
    second_process ();

  int status;
  if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
    error (EXIT_FAILURE, errno, "waitpid failed");

  if (!WIFEXITED (status))
    error (EXIT_FAILURE, 0, "child process didn't exit normally");

  return WEXITSTATUS (status);
}


- --
- --------------.                        ,-.            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/Tjqg2ijCOnn/RHQRAs4KAJ9HlFJo3pfh/nChsLvldaDpk02KKwCeLXMz
kODgRkSVcOfNXs693xo1iK8=
=krhV
-----END PGP SIGNATURE-----

[-- Attachment #2: d-ipc-pid --]
[-- Type: text/plain, Size: 2669 bytes --]

--- linux-2.5/ipc/msg.c-save	2003-05-14 00:36:26.000000000 -0700
+++ linux-2.5/ipc/msg.c	2003-08-28 10:07:51.000000000 -0700
@@ -707,7 +707,7 @@ retry:
 		goto retry;
 	}
 
-	msq->q_lspid = current->pid;
+	msq->q_lspid = current->tgid;
 	msq->q_stime = get_seconds();
 
 	if(!pipelined_send(msq,msg)) {
@@ -801,7 +801,7 @@ retry:
 		list_del(&msg->m_list);
 		msq->q_qnum--;
 		msq->q_rtime = get_seconds();
-		msq->q_lrpid = current->pid;
+		msq->q_lrpid = current->tgid;
 		msq->q_cbytes -= msg->m_ts;
 		atomic_sub(msg->m_ts,&msg_bytes);
 		atomic_dec(&msg_hdrs);
--- linux-2.5/ipc/sem.c-save	2003-08-14 01:27:01.000000000 -0700
+++ linux-2.5/ipc/sem.c	2003-08-28 10:08:09.000000000 -0700
@@ -664,7 +664,7 @@ static int semctl_main(int semid, int se
 		for (un = sma->undo; un; un = un->id_next)
 			un->semadj[semnum] = 0;
 		curr->semval = val;
-		curr->sempid = current->pid;
+		curr->sempid = current->tgid;
 		sma->sem_ctime = get_seconds();
 		/* maybe some queued-up processes were waiting for this */
 		update_queue(sma);
@@ -1052,7 +1052,7 @@ retry_undos:
 	if (error)
 		goto out_unlock_free;
 
-	error = try_atomic_semop (sma, sops, nsops, un, current->pid);
+	error = try_atomic_semop (sma, sops, nsops, un, current->tgid);
 	if (error <= 0)
 		goto update;
 
@@ -1064,7 +1064,7 @@ retry_undos:
 	queue.sops = sops;
 	queue.nsops = nsops;
 	queue.undo = un;
-	queue.pid = current->pid;
+	queue.pid = current->tgid;
 	queue.id = semid;
 	if (alter)
 		append_to_queue(sma ,&queue);
@@ -1206,7 +1206,7 @@ found:
 				sem->semval += u->semadj[i];
 				if (sem->semval < 0)
 					sem->semval = 0; /* shouldn't happen */
-				sem->sempid = current->pid;
+				sem->sempid = current->tgid;
 			}
 		}
 		sma->sem_otime = get_seconds();
--- linux-2.5/ipc/shm.c-save	2003-07-11 20:32:12.000000000 -0700
+++ linux-2.5/ipc/shm.c	2003-08-28 10:08:22.000000000 -0700
@@ -89,7 +89,7 @@ static inline void shm_inc (int id) {
 	if(!(shp = shm_lock(id)))
 		BUG();
 	shp->shm_atim = get_seconds();
-	shp->shm_lprid = current->pid;
+	shp->shm_lprid = current->tgid;
 	shp->shm_nattch++;
 	shm_unlock(shp);
 }
@@ -136,7 +136,7 @@ static void shm_close (struct vm_area_st
 	/* remove from the list of attaches of the shm segment */
 	if(!(shp = shm_lock(id)))
 		BUG();
-	shp->shm_lprid = current->pid;
+	shp->shm_lprid = current->tgid;
 	shp->shm_dtim = get_seconds();
 	shp->shm_nattch--;
 	if(shp->shm_nattch == 0 &&
@@ -209,7 +209,7 @@ static int newseg (key_t key, int shmflg
 	if(id == -1) 
 		goto no_id;
 
-	shp->shm_cprid = current->pid;
+	shp->shm_cprid = current->tgid;
 	shp->shm_lprid = 0;
 	shp->shm_atim = shp->shm_dtim = 0;
 	shp->shm_ctim = get_seconds();

                 reply	other threads:[~2003-08-28 17:24 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3F4E3AA0.9030808@redhat.com \
    --to=drepper@redhat.com \
    --cc=akpm@osdl.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=torvalds@osdl.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox