~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to app/plug-in/gimppluginshm.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2007-05-02 16:33:03 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070502163303-bvzhjzbpw8qglc4y
Tags: 2.3.16-1ubuntu1
* Resynchronized with Debian, remaining Ubuntu changes:
  - debian/rules: i18n magic.
* debian/control.in:
  - Maintainer: Ubuntu Core Developers <ubuntu-devel@lists.ubuntu.com>
* debian/patches/02_help-message.patch,
  debian/patches/03_gimp.desktop.in.in.patch,
  debian/patches/10_dont_show_wizard.patch: updated.
* debian/patches/04_composite-signedness.patch,
  debian/patches/05_add-letter-spacing.patch: dropped, used upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GIMP - The GNU Image Manipulation Program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
4
 * gimppluginhsm.c
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
19
 */
 
20
 
 
21
#include "config.h"
 
22
 
 
23
#include <sys/types.h>
 
24
 
 
25
#include <errno.h>
 
26
 
 
27
#if defined(USE_SYSV_SHM)
 
28
 
 
29
#ifdef HAVE_IPC_H
 
30
#include <sys/ipc.h>
 
31
#endif
 
32
 
 
33
#ifdef HAVE_SHM_H
 
34
#include <sys/shm.h>
 
35
#endif
 
36
 
 
37
#elif defined(USE_POSIX_SHM)
 
38
 
 
39
#ifdef HAVE_UNISTD_H
 
40
#include <unistd.h>
 
41
#endif
 
42
 
 
43
#include <fcntl.h>
 
44
#include <sys/mman.h>
 
45
 
 
46
#endif /* USE_POSIX_SHM */
 
47
 
 
48
#include <glib-object.h>
 
49
 
 
50
#if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
 
51
 
 
52
#define STRICT
 
53
#include <windows.h>
 
54
#include <process.h>
 
55
 
 
56
#ifdef G_OS_WIN32
 
57
#include <fcntl.h>
 
58
#include <io.h>
 
59
#endif
 
60
 
 
61
#define USE_WIN32_SHM 1
 
62
 
 
63
#endif /* G_OS_WIN32 || G_WITH_CYGWIN */
 
64
 
 
65
#include "plug-in-types.h"
 
66
 
 
67
#include "base/tile.h"
 
68
 
 
69
#include "gimppluginshm.h"
 
70
 
 
71
 
 
72
#define TILE_MAP_SIZE (TILE_WIDTH * TILE_HEIGHT * 4)
 
73
 
 
74
#define ERRMSG_SHM_DISABLE "Disabling shared memory tile transport"
 
75
 
 
76
 
 
77
struct _GimpPlugInShm
 
78
{
 
79
  gint    shm_ID;
 
80
  guchar *shm_addr;
 
81
 
 
82
#if defined(USE_WIN32_SHM)
 
83
  HANDLE  shm_handle;
 
84
#endif
 
85
};
 
86
 
 
87
 
 
88
GimpPlugInShm *
 
89
gimp_plug_in_shm_new (void)
 
90
{
 
91
  /* allocate a piece of shared memory for use in transporting tiles
 
92
   *  to plug-ins. if we can't allocate a piece of shared memory then
 
93
   *  we'll fall back on sending the data over the pipe.
 
94
   */
 
95
 
 
96
  GimpPlugInShm *shm = g_new0 (GimpPlugInShm, 1);
 
97
 
 
98
  shm->shm_ID = -1;
 
99
 
 
100
#if defined(USE_SYSV_SHM)
 
101
 
 
102
  /* Use SysV shared memory mechanisms for transferring tile data. */
 
103
  {
 
104
    shm->shm_ID = shmget (IPC_PRIVATE, TILE_MAP_SIZE, IPC_CREAT | 0600);
 
105
 
 
106
    if (shm->shm_ID != -1)
 
107
      {
 
108
        shm->shm_addr = (guchar *) shmat (shm->shm_ID, NULL, 0);
 
109
 
 
110
        if (shm->shm_addr == (guchar *) -1)
 
111
          {
 
112
            g_printerr ("shmat() failed: %s\n" ERRMSG_SHM_DISABLE,
 
113
                        g_strerror (errno));
 
114
            shmctl (shm->shm_ID, IPC_RMID, NULL);
 
115
            shm->shm_ID = -1;
 
116
          }
 
117
 
 
118
#ifdef IPC_RMID_DEFERRED_RELEASE
 
119
        if (shm->shm_addr != (guchar *) -1)
 
120
          shmctl (shm->shm_ID, IPC_RMID, NULL);
 
121
#endif
 
122
      }
 
123
    else
 
124
      {
 
125
        g_printerr ("shmget() failed: %s\n" ERRMSG_SHM_DISABLE,
 
126
                    g_strerror (errno));
 
127
      }
 
128
  }
 
129
 
 
130
#elif defined(USE_WIN32_SHM)
 
131
 
 
132
  /* Use Win32 shared memory mechanisms for transferring tile data. */
 
133
  {
 
134
    gint  pid;
 
135
    gchar fileMapName[MAX_PATH];
 
136
 
 
137
    /* Our shared memory id will be our process ID */
 
138
    pid = GetCurrentProcessId ();
 
139
 
 
140
    /* From the id, derive the file map name */
 
141
    g_snprintf (fileMapName, sizeof (fileMapName), "GIMP%d.SHM", pid);
 
142
 
 
143
    /* Create the file mapping into paging space */
 
144
    shm->shm_handle = CreateFileMapping ((HANDLE) 0xFFFFFFFF, NULL,
 
145
                                         PAGE_READWRITE, 0,
 
146
                                         TILE_MAP_SIZE,
 
147
                                         fileMapName);
 
148
 
 
149
    if (shm->shm_handle)
 
150
      {
 
151
        /* Map the shared memory into our address space for use */
 
152
        shm->shm_addr = (guchar *) MapViewOfFile (shm->shm_handle,
 
153
                                                  FILE_MAP_ALL_ACCESS,
 
154
                                                  0, 0, TILE_MAP_SIZE);
 
155
 
 
156
        /* Verify that we mapped our view */
 
157
        if (shm->shm_addr)
 
158
          {
 
159
            shm->shm_ID = pid;
 
160
          }
 
161
        else
 
162
          {
 
163
            g_printerr ("MapViewOfFile error: %d... " ERRMSG_SHM_DISABLE,
 
164
                        GetLastError ());
 
165
          }
 
166
      }
 
167
    else
 
168
      {
 
169
        g_printerr ("CreateFileMapping error: %d... " ERRMSG_SHM_DISABLE,
 
170
                    GetLastError ());
 
171
      }
 
172
  }
 
173
 
 
174
#elif defined(USE_POSIX_SHM)
 
175
 
 
176
  /* Use POSIX shared memory mechanisms for transferring tile data. */
 
177
  {
 
178
    gint  pid;
 
179
    gchar shm_handle[32];
 
180
    gint  shm_fd;
 
181
 
 
182
    /* Our shared memory id will be our process ID */
 
183
    pid = getpid ();
 
184
 
 
185
    /* From the id, derive the file map name */
 
186
    g_snprintf (shm_handle, sizeof (shm_handle), "/gimp-shm-%d", pid);
 
187
 
 
188
    /* Create the file mapping into paging space */
 
189
    shm_fd = shm_open (shm_handle, O_RDWR | O_CREAT, 0600);
 
190
 
 
191
    if (shm_fd != -1)
 
192
      {
 
193
        if (ftruncate (shm_fd, TILE_MAP_SIZE) != -1)
 
194
          {
 
195
            /* Map the shared memory into our address space for use */
 
196
            shm->shm_addr = (guchar *) mmap (NULL, TILE_MAP_SIZE,
 
197
                                             PROT_READ | PROT_WRITE, MAP_SHARED,
 
198
                                             shm_fd, 0);
 
199
 
 
200
            /* Verify that we mapped our view */
 
201
            if (shm->shm_addr != MAP_FAILED)
 
202
              {
 
203
                shm->shm_ID = pid;
 
204
              }
 
205
            else
 
206
              {
 
207
                g_printerr ("mmap() failed: %s\n" ERRMSG_SHM_DISABLE,
 
208
                            g_strerror (errno));
 
209
 
 
210
                shm_unlink (shm_handle);
 
211
              }
 
212
          }
 
213
        else
 
214
          {
 
215
            g_printerr ("ftruncate() failed: %s\n" ERRMSG_SHM_DISABLE,
 
216
                        g_strerror (errno));
 
217
 
 
218
            shm_unlink (shm_handle);
 
219
          }
 
220
 
 
221
        close (shm_fd);
 
222
      }
 
223
    else
 
224
      {
 
225
        g_printerr ("shm_open() failed: %s\n" ERRMSG_SHM_DISABLE,
 
226
                    g_strerror (errno));
 
227
      }
 
228
  }
 
229
 
 
230
#endif
 
231
 
 
232
  if (shm->shm_ID == -1)
 
233
    {
 
234
      g_free (shm);
 
235
      shm = NULL;
 
236
    }
 
237
 
 
238
  return shm;
 
239
}
 
240
 
 
241
void
 
242
gimp_plug_in_shm_free (GimpPlugInShm *shm)
 
243
{
 
244
  g_return_if_fail (shm != NULL);
 
245
 
 
246
  if (shm->shm_ID != -1)
 
247
    {
 
248
 
 
249
#if defined (USE_SYSV_SHM)
 
250
 
 
251
#ifndef IPC_RMID_DEFERRED_RELEASE
 
252
      shmdt (shm->shm_addr);
 
253
      shmctl (shm->shm_ID, IPC_RMID, NULL);
 
254
#else
 
255
      shmdt (shm->shm_addr);
 
256
#endif /* IPC_RMID_DEFERRED_RELEASE */
 
257
 
 
258
#elif defined(USE_WIN32_SHM)
 
259
 
 
260
      if (shm->shm_handle)
 
261
        CloseHandle (shm->shm_handle);
 
262
 
 
263
#elif defined(USE_POSIX_SHM)
 
264
 
 
265
      gchar shm_handle[32];
 
266
 
 
267
      munmap (shm->shm_addr, TILE_MAP_SIZE);
 
268
 
 
269
      g_snprintf (shm_handle, sizeof (shm_handle), "/gimp-shm-%d",
 
270
                  shm->shm_ID);
 
271
 
 
272
      shm_unlink (shm_handle);
 
273
 
 
274
#endif
 
275
 
 
276
    }
 
277
 
 
278
  g_free (shm);
 
279
}
 
280
 
 
281
gint
 
282
gimp_plug_in_shm_get_ID (GimpPlugInShm *shm)
 
283
{
 
284
  g_return_val_if_fail (shm != NULL, -1);
 
285
 
 
286
  return shm->shm_ID;
 
287
}
 
288
 
 
289
guchar *
 
290
gimp_plug_in_shm_get_addr (GimpPlugInShm *shm)
 
291
{
 
292
  g_return_val_if_fail (shm != NULL, NULL);
 
293
 
 
294
  return shm->shm_addr;
 
295
}