Gammu internals  1.38.0
devfunc.c File Reference
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include "devfunc.h"
#include "../gsmstate.h"
Include dependency graph for devfunc.c:

Go to the source code of this file.

Macros

#define max_buf_len   128
 
#define lock_path   "/var/lock/LCK.."
 

Functions

GSM_Error lock_device (GSM_StateMachine *s, const char *port, char **lock_name)
 
gboolean unlock_device (GSM_StateMachine *s, char **lock_file)
 
int FindSerialSpeed (const char *buffer)
 

Macro Definition Documentation

§ lock_path

#define lock_path   "/var/lock/LCK.."

Definition at line 206 of file devfunc.c.

Referenced by lock_device().

§ max_buf_len

#define max_buf_len   128

Definition at line 205 of file devfunc.c.

Referenced by lock_device().

Function Documentation

§ FindSerialSpeed()

int FindSerialSpeed ( const char *  buffer)

Definition at line 367 of file devfunc.c.

Referenced by GSM_RegisterAllConnections().

368 {
369  switch (atoi(buffer)) {
370  case 50 : return 50;
371  case 75 : return 75;
372  case 110 : return 110;
373  case 134 : return 134;
374  case 150 : return 150;
375  case 200 : return 200;
376  case 300 : return 300;
377  case 600 : return 600;
378  case 1200 : return 1200;
379  case 1800 : return 1800;
380  case 2400 : return 2400;
381  case 3600 : return 3600;
382  case 4800 : return 4800;
383  case 7200 : return 7200;
384  case 9600 : return 9600;
385  case 14400 : return 14400;
386  case 19200 : return 19200;
387  case 28800 : return 28800;
388  case 38400 : return 38400;
389  case 57600 : return 57600;
390  case 115200 : return 115200;
391  case 230400 : return 230400;
392  case 460800 : return 460800;
393  case 614400 : return 614400;
394  case 921600 : return 921600;
395  case 1228800 : return 1228800;
396  case 2457600 : return 2457600;
397  case 3000000 : return 3000000;
398  case 6000000 : return 6000000;
399  default : return 0;
400  }
401 }

§ lock_device()

GSM_Error lock_device ( GSM_StateMachine s,
const char *  port,
char **  lock_name 
)

Definition at line 212 of file devfunc.c.

References ERR_DEVICELOCKED, ERR_DEVICENOPERMISSION, ERR_DEVICEOPENERROR, ERR_MOREMEMORY, ERR_NONE, ERR_UNKNOWN, ERR_WRITING_FILE, lock_device(), lock_path, max_buf_len, and smprintf().

Referenced by GSM_OpenConnection(), and lock_device().

213 {
214  char *lock_file = NULL;
215  char buffer[max_buf_len];
216  const char *aux;
217  int fd = -1, len;
218  GSM_Error error = ERR_NONE;
219  size_t wrotebytes;
220  char buf[max_buf_len];
221  int pid, n = 0;
222 
223 
224  smprintf(s, "Locking device\n");
225 
226  aux = strrchr(port, '/');
227  /* Remove leading '/' */
228  if (aux) {
229  aux++;
230  } else {
231  /* No / in port */
232  aux = port;
233  }
234  len = strlen(aux) + strlen(lock_path);
235 
236  memset(buffer, 0, sizeof(buffer));
237  lock_file = calloc(len + 1, 1);
238  if (!lock_file) {
239  smprintf(s, "Out of memory error while locking device\n");
240  return ERR_MOREMEMORY;
241  }
242  /* I think we don't need to use strncpy, as we should have enough
243  * buffer due to strlen results
244  */
245  strcpy(lock_file, lock_path);
246  strcat(lock_file, aux);
247 
248  /* Check for the stale lockfile.
249  * The code taken from minicom by Miquel van Smoorenburg */
250  if ((fd = open(lock_file, O_RDONLY)) >= 0) {
251  n = read(fd, buf, sizeof(buf) - 1);
252  if (n <= 0) {
253  goto failread;
254  }
255  if (n == 4 && 4 == sizeof(int) &&
256  ! (
257  isdigit((int)buf[0]) &&
258  isdigit((int)buf[1]) &&
259  isdigit((int)buf[2]) &&
260  isdigit((int)buf[3])
261  )) {
262  /* Rewind */
263  lseek(fd, 0, SEEK_SET);
264  /* Read PID */
265  /* We could make it from buf, but we would have to care about endians. */
266  n = read(fd, &pid, sizeof(int));
267  if (n != 4) {
268  smprintf(s, "Reading lock for second time failed\n");
269  goto failread;
270 
271  }
272  } else {
273  /* Ascii lockfile. */
274  buf[n] = 0;
275  sscanf(buf, "%d", &pid);
276  }
277  close(fd);
278  fd = -1;
279 
280 
281  if (pid > 0 && kill((pid_t)pid, 0) < 0 && errno == ESRCH) {
282  smprintf(s, "Lockfile %s is stale. Overriding it..\n", lock_file);
283  if (unlink(lock_file) != 0) {
284  smprintf(s, "Overriding failed, please check the permissions\n");
285  smprintf(s, "Cannot lock device\n");
286  error = ERR_DEVICENOPERMISSION;
287  goto failed;
288  }
289  } else {
290  smprintf(s, "Device already locked by PID %d.\n", pid);
291  error = ERR_DEVICELOCKED;
292  goto failed;
293  }
294  }
295 
296  /* Try to create a new file, with 0644 mode */
297  fd = open(lock_file, O_CREAT | O_EXCL | O_WRONLY, 0644);
298  if (fd == -1) {
299  if (errno == EEXIST) {
300  smprintf(s, "Device seems to be locked by unknown process\n");
301  error = ERR_DEVICEOPENERROR;
302  } else if (errno == EACCES) {
303  smprintf(s, "Please check permission on lock directory\n");
304  error = ERR_DEVICENOPERMISSION;
305  } else if (errno == ENOENT) {
306  smprintf(s, "Cannot create lockfile %s. Please check for existence of path\n", lock_file);
307  error = ERR_UNKNOWN;
308  } else {
309  smprintf(s, "Unknown error with creating lockfile %s\n", lock_file);
310  error = ERR_UNKNOWN;
311  }
312  goto failed;
313  }
314  sprintf(buffer, "%10ld gammu\n", (long)getpid());
315  wrotebytes = write(fd, buffer, strlen(buffer));
316  close(fd);
317  fd = -1;
318  if (wrotebytes != strlen(buffer)) {
319  error = ERR_WRITING_FILE;
320  goto failed;
321  }
322  *lock_name = lock_file;
323  return ERR_NONE;
324 failread:
325  smprintf(s, "Unable to read lockfile %s.\n", lock_file);
326  smprintf(s, "Please check for reason and remove the lockfile by hand.\n");
327  smprintf(s, "Cannot lock device\n");
328  error = ERR_UNKNOWN;
329 failed:
330  if (fd != -1) {
331  close(fd);
332  }
333  free(lock_file);
334  *lock_name = NULL;
335  return error;
336 }
GSM_Error
Definition: gammu-error.h:23
#define lock_path
Definition: devfunc.c:206
#define max_buf_len
Definition: devfunc.c:205
int smprintf(GSM_StateMachine *s, const char *format,...)
Definition: debug.c:261

§ unlock_device()

gboolean unlock_device ( GSM_StateMachine s,
char **  lock_file 
)

Definition at line 347 of file devfunc.c.

References FALSE, smprintf(), TRUE, and unlock_device().

Referenced by GSM_CloseConnection(), GSM_OpenConnection(), and unlock_device().

348 {
349  int err;
350 
351  if (lock_file == NULL || *lock_file == NULL) {
352  smprintf(s, "Cannot unlock device\n");
353  return FALSE;
354  }
355  err = unlink(*lock_file);
356  free(*lock_file);
357  *lock_file = NULL;
358  return (err + 1);
359 }
#define FALSE
Definition: gammu-types.h:25
int smprintf(GSM_StateMachine *s, const char *format,...)
Definition: debug.c:261