[go: up one dir, main page]

Menu

[9c7f01]: / src / shmdumper.c  Maximize  Restore  History

Download this file

176 lines (154 with data), 4.8 kB

  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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* shmcat: Dump System V Shared Memory Objects
* (C) 2012 by Stefan Gast (sgdev@arcor.de)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* shmdumper.c: Implementation of the shared memory dumper
*/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#include "shmdumper.h"
#include "translate.h"
static ShmcatStatus dumpShm(int id, int byKey, const char *param, const char *programName)
{
struct shmid_ds infoBuffer;
const char *shm;
const char *shmpos;
const char *shmend;
int bytesWritten;
/* Put the segment into our address space. We do this even before
* we query its size, to make sure the segment does not disappear
* (i.e. get deleted) between getting the size and attaching it. */
shm = shmat(id, NULL, SHM_RDONLY);
if(shm == (const char *)-1)
{
if(byKey)
fprintf(stderr, _("%s: Cannot attach the shared memory segment with the key \"%s\": %s\n"),
programName, param, strerror(errno));
else
fprintf(stderr, _("%s: Cannot attach the shared memory segment with the id \"%s\": %s\n"),
programName, param, strerror(errno));
return SHMCAT_ERROR;
}
/* Now we need to know how big our segment is. */
if(shmctl(id, IPC_STAT, &infoBuffer) == -1)
{
shmdt(shm);
if(byKey)
fprintf(stderr, _("%s: Cannot determine the size of the shared memory segment with the key \"%s\": %s\n"),
programName, param, strerror(errno));
else
fprintf(stderr, _("%s: Cannot determine the size of the shared memory segment with the id \"%s\": %s\n"),
programName, param, strerror(errno));
return SHMCAT_ERROR;
}
/* Set position and end pointer. */
shmpos = shm;
shmend = shm + infoBuffer.shm_segsz;
/* If we have a huge shared memory segment, we have to be
* careful with the size we pass to write. Otherwise the ssize_t
* might overflow. So we do the huge chunks first with
* this special loop. */
while((uintptr_t)shmend-(uintptr_t)shmpos > (uintptr_t)SSIZE_MAX)
{
bytesWritten = write(STDOUT_FILENO, shmpos, SSIZE_MAX);
if(bytesWritten <= 0)
{
shmdt(shm);
fprintf(stderr, _("%s: Error writing to standard output: %s\n"),
programName, strerror(errno));
return SHMCAT_PANIC;
}
else
shmpos += bytesWritten;
}
/* Now dump the rest. */
while(shmpos != shmend)
{
bytesWritten = write(STDOUT_FILENO, shmpos, shmend-shmpos);
if(bytesWritten <= 0)
{
shmdt(shm);
fprintf(stderr, _("%s: Error writing to standard output: %s\n"),
programName, strerror(errno));
return SHMCAT_PANIC;
}
else
shmpos += bytesWritten;
}
shmdt(shm);
return SHMCAT_OK;
}
ShmcatStatus dumpShmKey(const char *keystr, const char *programName)
{
char *numEnd;
unsigned long long key;
int id;
key = strtoull(keystr, &numEnd, 0);
if(*numEnd != '\0')
{
fprintf(stderr, _("%s: Invalid shared memory key: %s\n"),
programName, keystr);
return SHMCAT_ERROR;
}
if(key == (unsigned long long)IPC_PRIVATE)
{
fprintf(stderr, _("%s: \"%s\" is the key for private shared memory segments! Try passing the segment by ID instead.\n"),
programName, keystr);
return SHMCAT_ERROR;
}
if((unsigned long long)(key_t)key != key) /* simple overflow detection */
{
fprintf(stderr, _("%s: Invalid shared memory key: %s\n"),
programName, keystr);
return SHMCAT_ERROR;
}
id = shmget((key_t)key, 0, 0);
if(id == -1)
{
fprintf(stderr, _("%s: Cannot open a shared memory segment with the key \"%s\": %s\n"),
programName, keystr, strerror(errno));
return SHMCAT_ERROR;
}
return dumpShm(id, 1, keystr, programName);
}
ShmcatStatus dumpShmId(const char *idstr, const char *programName)
{
char *numEnd;
unsigned long id;
id = strtoul(idstr, &numEnd, 0);
if(*numEnd != '\0' || id > (unsigned long)(INT_MAX))
{
fprintf(stderr, _("%s: Invalid shared memory id: %s\n"),
programName, idstr);
return SHMCAT_ERROR;
}
return dumpShm((int)id, 0, idstr, programName);
}