Skip to contentSkip to conversation

More ->pid to ->tgid changes

PATCH Ulrich Drepper 1 participant 1 message Aug 28, 2003

lore
Share & tools
lore
#0 lore raw

-----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-----

ipc/msg.c renamed from ipc/msg.c-save +2 -2
@@ -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);
ipc/sem.c renamed from ipc/sem.c-save +4 -4
@@ -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();
ipc/shm.c renamed from ipc/shm.c-save +3 -3
@@ -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();