📄 dhcp-files.c
字号:
/* $Header: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-files.c,v 1.9 2003/06/11 03:08:48 actmodern Exp $ * * Copyright 2002 Thamer Alharbash <tmh@whitefang.com> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. The names of the authors may not be used to endorse or promote * products derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. * * Handles our variable files. * */#define MODULE_NAME "dhcp-files"#include "dhcp-local.h"#include "dhcp-libutil.h"/* * * * * * * * * * * * utility routines * * * * * * * * * * * *//* grab our pid file. <name>.pid */static char *get_pid_file_name(char *name){ stringbuffer_t *sb; char *fname; sb = stringbuffer_create(); stringbuffer_append(sb, name); stringbuffer_append(sb, ".pid"); fname = xstrdup(stringbuffer_getstring(sb)); stringbuffer_destroy(sb); return fname;}static FILE *file_open_proc(const char *filename, char *cmode, int flags, mode_t mode){ FILE *fp; int fd; /* open file and create if it needed. */ if((fd = open(filename, flags, mode)) < 0) { ERROR_MESSAGE("cannot open or create file: %s : %s", filename, strerror(errno)); return NULL; } if((fp = fdopen(fd, cmode)) == NULL) { close(fd); ERROR_MESSAGE("cannot fdopen file: %s : %s", filename, strerror(errno)); return NULL; } return fp; /* fd is closed when fp is closed since they share the descriptor. */}/* * * * * * * * * interface * * * * * * * * *//* delete a file. */int file_delete(char *fname){ return (unlink(fname));}/* FIXME: we really should be checking return values here. *//* move a file. */void file_move(char *fname, char *dest){ /* use rename() if available. */#ifdef HAVE_RENAME rename(fname, dest);#else /* HAVE_RENAME */ unlink(dest); link(fname, dest); unlink(fname);#endif /* HAVE_RENAME */ return;}/* check if a file exists. -- achtung: watch out for race * conditions */int file_exists(const char *fname){ struct stat stat_buf; if(stat(fname, &stat_buf)) { return 0; } return 1;}/* check permissions. */int file_permissions_are(const char *fname, mode_t mode){ struct stat st; if(stat(fname, &st) < 0) { ERROR_MESSAGE("could not stat file %s", fname); return -1; } if((st.st_mode&(S_IRWXU|S_IRWXG|S_IRWXO)) == mode) return 1; /* mode matches. */ else return 0; /* mode does not match. */}/* change mode on a file so its publicaly readable. */void file_make_public_read(char *fname){ chmod(fname, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);}/* File creation routines. These assume we're opening a directory * only writable by us. It is unsafe to use them elsewhere. *//* create file or open it if it exists. */FILE *file_open_or_create_safe(const char *filename, char *mode){ return (file_open_proc(filename, mode, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));}/* create file or open if it exists, and always truncate. */FILE *file_create_and_truncate_safe(const char *filename, char *mode){ return (file_open_proc(filename, mode, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR));}/* XXX - check return val. *//* create a pid file <name>.pid */int file_create_pid(char *name){ FILE *fp; char *fname; fname = get_pid_file_name(name); fp = file_create_and_truncate_safe(fname, "a"); if(fp == NULL) { ERROR_MESSAGE("cannot open or create file: %s : %s", fname, strerror(errno)); xfree(fname); return -1; } fprintf(fp, "%lu", (unsigned long)getpid()); fclose(fp); xfree(fname); return 0;}/* delete a pid file <name>.pid */int file_delete_pid(char *name){ int retval; char *fname = get_pid_file_name(name); retval = file_delete(fname); /* ignore any errors. */ xfree(fname); return retval;}/* get pid from file. */int file_get_pid(char *name, pid_t *pid){ FILE *fp; unsigned long pid_value; char *fname = get_pid_file_name(name); if(!file_exists(fname)) { xfree(fname); return -1; } fp = file_open_or_create_safe(fname, "r"); if(fp == NULL) { ERROR_MESSAGE("could not open pid file"); xfree(fname); return 1; } /* probably a safe cast since we're most likely promoting. */ fscanf(fp, "%ld", &pid_value); fclose(fp); xfree(fname); *pid = pid_value; return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -