File: shmctl.c

package info (click to toggle)
glibc 2.28-10
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, buster-proposed-updates, experimental, sid
  • size: 272,168 kB
  • sloc: ansic: 1,008,602; asm: 259,607; makefile: 11,271; sh: 10,477; python: 6,910; cpp: 4,992; perl: 2,258; awk: 2,005; yacc: 290; pascal: 182; sed: 73
file content (132 lines) | stat: -rw-r--r-- 3,495 bytes parent folder | download | duplicates (21)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* Copyright (C) 2005 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   02111-1307 USA.  */

#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "sysvshm.h"

/* Provide operations to control over shared memory segments.  */
int
__shmctl (int id, int cmd, struct shmid_ds *buf)
{
  error_t err = 0;
  int fd;
  int res;
  char filename[sizeof (SHM_DIR) - 1 + SHM_NAMEMAX];
  struct stat statbuf;

  sprintf (filename, SHM_DIR SHM_NAMEPRI, id);
  /* SysV requires read access for IPC_STAT.  */
  fd = __open (filename, O_NORW);
  if (fd < 0)
    {
      if (errno == ENOENT)
	errno = EINVAL;
      return -1;
    }

  res = __fstat (fd, &statbuf);
  if (res < 0)
    {
      err = errno;
      __close (fd);
      errno = err;
      return -1;
    }

  switch (cmd)
    {
    case IPC_STAT:

      buf->shm_perm.__key = id;
      buf->shm_perm.uid = statbuf.st_uid;
      buf->shm_perm.gid = statbuf.st_gid;

      /* We do not support the creator.  */
      buf->shm_perm.cuid = statbuf.st_uid;
      buf->shm_perm.cgid = statbuf.st_gid;

      /* We just want the protection bits.  */
      buf->shm_perm.mode = statbuf.st_mode & 0777;
      /* Hopeless.  We do not support a sequence number.  */
      buf->shm_perm.__seq = statbuf.st_ino;
      buf->shm_segsz = statbuf.st_size;

      /* Hopeless.  We do not support any of these.  */
      buf->shm_atime = statbuf.st_atime;
      buf->shm_dtime = statbuf.st_mtime;
      /* Well, this comes at least close.  */
      buf->shm_ctime = statbuf.st_ctime;

      /* We do not support the PID.  */
      buf->shm_cpid = 0;
      buf->shm_lpid = 0;

      if (statbuf.st_mode & S_IMMAP0)
        buf->shm_nattch = 0;
      else
        /* 42 is the answer.  Of course this is bogus, but for most
	   applications, this should be fine.  */
        buf->shm_nattch = 42;

      break;

    case IPC_SET:
      if (statbuf.st_uid != buf->shm_perm.uid
	  || statbuf.st_gid != buf->shm_perm.gid)
	{
	  res = __fchown (fd,
			  (statbuf.st_uid != buf->shm_perm.uid)
			  ? buf->shm_perm.uid : -1,
			  (statbuf.st_gid != buf->shm_perm.gid)
			  ? buf->shm_perm.gid : -1);
	  if (res < 0)
	    err = errno;
	}

      if (!err && statbuf.st_mode & 0777 != buf->shm_perm.mode & 0777)
	{
	  res = __fchmod (fd, (statbuf.st_mode & ~0777)
			  | (buf->shm_perm.mode & 0777));
	  if (res < 0)
	    err = errno;
	}
      break;

    case IPC_RMID:
      res = __unlink (filename);
      /* FIXME: Check error (mapping ENOENT to EINVAL).  */
      break;

    default:
      err = EINVAL;
    }

  __close (fd);
  errno = err;
  return err ? -1 : 0;
}

weak_alias(__shmctl, shmctl)