Home | History | Annotate | Download | only in sigqueue
      1 /*
      2  * Copyright (c) 2002-2003, Intel Corporation. All rights reserved.
      3  * Created by:  salwan.searty REMOVE-THIS AT intel DOT com
      4  * This file is licensed under the GPL license.  For the full content
      5  * of this license, see the COPYING file at the top level of this
      6  * source tree.
      7 
      8  *  Test that when the process does not have the appropriate privilege
      9     to send the signal to the receiving process, then sigqueue()
     10     returns -1 and errno is set to [EPERM]
     11 
     12     The real or effective user ID of the sending process shall match
     13     the real or saved set-user-ID of the receiving process.
     14  */
     15 
     16 #define _XOPEN_SOURCE 600
     17 
     18 #include <signal.h>
     19 #include <stdio.h>
     20 #include <stdlib.h>
     21 #include <unistd.h>
     22 #include <errno.h>
     23 #include <sys/types.h>
     24 #include <pwd.h>
     25 #include <string.h>
     26 #include "posixtest.h"
     27 
     28 /** Set the euid of this process to a non-root uid */
     29 int set_nonroot()
     30 {
     31 	struct passwd *pw;
     32 	setpwent();
     33 	/* search for the first user which is non root */
     34 	while ((pw = getpwent()) != NULL)
     35 		if (strcmp(pw->pw_name, "root"))
     36 			break;
     37 	endpwent();
     38 	if (pw == NULL) {
     39 		printf("There is no other user than current and root.\n");
     40 		return 1;
     41 	}
     42 	/* setuid will change uid, euid */
     43 	if (setuid(pw->pw_uid) != 0) {
     44 		if (errno == EPERM) {
     45 			printf
     46 			    ("You don't have permission to change your UID.\n");
     47 			return 1;
     48 		}
     49 		perror("An error occurs when calling seteuid()");
     50 		return 1;
     51 	}
     52 
     53 	printf("Testing with user '%s' (euid: %d)(uid: %d)\n",
     54 	       pw->pw_name, (int)geteuid(), (int)getuid());
     55 	return 0;
     56 }
     57 
     58 int main(void)
     59 {
     60 	int failure = 0;
     61 	union sigval value;
     62 	value.sival_int = 0;	/* 0 is just an arbitrary value */
     63 
     64 	/* We assume process Number 1 is created by root */
     65 	/* and can only be accessed by root */
     66 	/* This test should be run under standard user permissions */
     67 	if (getuid() == 0) {
     68 		if (set_nonroot() != 0) {
     69 			printf("Cannot run this test as non-root user\n");
     70 			return PTS_UNTESTED;
     71 		}
     72 	}
     73 
     74 	if (-1 == sigqueue(1, 0, value)) {
     75 		if (EPERM == errno) {
     76 			printf("EPERM error received\n");
     77 		} else {
     78 			printf
     79 			    ("sigqueue() failed but errno not set correctly\n");
     80 			failure = 1;
     81 		}
     82 	} else {
     83 		printf("sigqueue() did not return -1\n");
     84 		failure = 1;
     85 	}
     86 
     87 	if (failure) {
     88 		return PTS_FAIL;
     89 	}
     90 	printf("Test PASSED\n");
     91 	return PTS_PASS;
     92 }
     93