[<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 Решения
Вывод различных атрибутов процесса, в соответствии с указанными
опциями

proc_att.c:

 1 #include <sys/types.h>
 2 #include <sys/resource.h>
 3 #include <sys/time.h>
 4 #include <unistd.h>
 5 #include <ulimit.h>
 6 #include <stdlib.h>
 7 #include <stdio.h>
 8 #define GET_FSLIM 1
 9 #define SET_FSLIM 2
10 extern char **environ;
11
12 main(int argc, char *argv[])
13 {
14     int c;
15     char options[ ] = "ispuU:cC:dvV:";  /* valid options */
16     struct rlimit rlp;
17     char **p;
18
19     if(argc < 2) {
20     fprintf(stderr,"Usage: %s options\n", argv[0]);
21     exit(0);
22      }
23
24     while ((c = getopt(argc, argv, options)) != EOF)
25     switch (c) {
26     case 'i':
27         printf("real userid = %ld\n", getuid());
28         printf("effective userid = %ld\n", geteuid());
29         printf("real groupid = %ld\n", getgid());
30         printf("effective groupid = %ld\n", getegid());
31         break;
32     case 's':
33         (void) setpgid(0,0);
34         break;
35     case 'p':
36         printf("process number = %ld\n", getpid());
37         printf("parent process number = %ld\n", getppid());
38         printf("group process number = %ld\n", getpgid(0));
39         break;
40     case 'U':
41         if( ulimit(SET_FSLIM, atol(optarg) ) == -1)
42         fprintf(stderr,"Must be super-user to increase ulimit\n");
43         break;
44     case 'u':
45         printf("ulimit = %ld\n", ulimit(GET_FSLIM, 0) );
46         break;
47     case 'c':
48         getrlimit(RLIMIT_CORE, &rlp);
49         printf("core size = %ld\n", rlp.rlim_cur);
50         break;

51     case 'C':
52         getrlimit(RLIMIT_CORE, &rlp);
53         rlp.rlim_cur = atol(optarg);
54         if (setrlimit(RLIMIT_CORE, &rlp) == -1)
55           fprintf(stderr, "Must be super-user to increase core\n");
56         break;
57     case 'd':
58         printf("current working directory is: %s\n",
getcwd(NULL,100));
59         break;
60     case 'v':
61         printf("environment variables are:\n");
62         for (p = environ; *p; p++)
63           printf("%s\n", *p);
64         break;
65     case 'V':
66         putenv(optarg);
67         break;
68     }
69 }


ВЫЗОВ:
$ proc_att -p -s -p -u -U1024 -u -c -C5 -c -d -Veditor=vi -v
process number = 16400
parent process number = 12339
group process number = 12339
process number = 16400
parent process number = 12339
group process number = 16400
ulimit = 2048
ulimit = 1024
core size = 1048576
core size = 5
current working directory is: /home/jrs/unit02/solutions
environment variables are:
HOME=/home/jrs
LOGNAME=jrs
MAIL=/var/mail/jrs
PATH=/home/jrs/bin:/usr/bin:.
TZ=EST5EDT
editor=vi
Время в Калифорнии
ca_time.c:

  1 #include <stdlib.h>
  2 #include <sys/types.h>
  3 #include <time.h>
  4 #include <stdio.h>
  5 extern char *tzname[];
  6
  7 main()
  8 {
  9
 10     time_t now;
 11     struct tm *sp;
 12
 13     (void) time( &now );
 14
 15     putenv("TZ=PST8PDT");
 16
 17     printf("%s", ctime( &now ) );
 18
 19     sp = localtime(&now);
 20     printf("%d/%d/%02d %d:%02d %s\n",
 21         sp->tm_mon + 1, sp->tm_mday, sp->tm_year,
 22         sp->tm_hour, sp->tm_min,
 23         tzname[sp->tm_isdst]);
 24 }

ВЫЗОВ:
$ ca_time
Wed Jun 14 07:04:31 1989
6/14/89 7:04 PDT
Установка идентификатора пользователя для доступа к файлу
suid_ex.c

  1 #include <sys/types.h>
  2 #include <unistd.h>
  3 #include <stdio.h>
  4
  5 main(int argc, char *argv[])
  6 {
  7     FILE *fp;
  8     uid_t uid;
  9
 10     if(argc <2){
 11         fprintf(stderr,"Usage: %s file_name\n", argv[0]);
 12         exit(1);
 13     }
 14     printf("initially uid=%ld and euid=%ld\n", getuid(), geteuid()
);
 15     if ((fp = fopen(argv[1], "r")) == NULL) {
 16         perror(argv[0]);
 17         exit(2);
 18     }
 19     else {
 20         printf("first open successful\n");
 21         fclose(fp);
 22     }
 23
 24     setuid( uid=getuid() );
 25
 26     printf("after setuid(%ld):\n   uid=%ld and euid=%ld\n",
 27         uid, getuid(), geteuid() );
 28     if ((fp = fopen(argv[1], "r")) == NULL) {
 29         perror(argv[0]);
 30         exit(3);
 31     }
 32     else {
 33         printf("second open successful\n");
 34         fclose(fp);
 35     }
 36 }
             Две сеанса работы за различными терминалами:

$ ls -l suid_ex file
-rw-------   1 jeg      unixc      11 Nov 26 13:08 file
-rwxr-x---   1 jeg      unixc   15559 Nov 26 12:51 suid_ex
$ id                               $ id
uid=503(jeg) gid=21(unixc)         uid=5000(cs0) gid=21(unixc)
$ suid_ex file                     $ suid_ex file
initially uid=503 and euid=503     initially uid=5000 and euid=5000
first open successful              suid_ex: Permission denied
after setuid(503):
uid=503 and euid=503
second open successful
$ chmod u+s suid_ex # done by owner

$ ls -l suid_ex file
-rw-------   1 jeg      unixc      11 Nov 26 13:08 file
-rws--x--x   1 jeg      unixc   15559 Nov 26 12:51 suid_ex
$ suid_ex file                     $ suid_ex file
initially uid=503 and euid=503     initially uid=5000 and euid=503
first open successful              first open successful
after setuid(503):                 after setuid(5000):
uid=503 and euid=503               uid=5000 and euid=5000
second open successful             suid_ex: Permission denied
$                                  $