UPS: apcupsd clean sources
[tomato.git] / release / src / router / apcupsd / src / lib / apcfile.c
blobe38caecaf59ddbf0fa01e06a394dd21b2b985c0f
1 /*
2 * apcfile.c
4 * Files management.
5 */
7 /*
8 * Copyright (C) 1996-99 Andre M. Hedrick <andre@suse.com>
9 * Copyright (C) 1999-2000 Riccardo Facchetti <riccardo@master.oasi.gpa.it>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of version 2 of the GNU General
13 * Public License as published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the Free
22 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23 * MA 02111-1307, USA.
26 #include "apc.h"
28 /*
29 * If this is a Windows machine, we do NOT create a pid file.
30 * We prevent multiple copies of apcupsd from running by
31 * ensuring that there is only one system tray entry for
32 * apcupsd.
36 * Create a file in `path'. Used for nologin and powerfail
37 * files.
39 int make_file(UPSINFO *ups, const char *path)
41 int makefd;
43 if ((makefd = open(path, O_CREAT | O_WRONLY, 0644)) >= 0) {
44 close(makefd);
45 } else {
46 log_event(ups, LOG_ERR, "Unable to create %s: ERR=%s\n",
47 path, strerror(errno));
50 return makefd;
53 /* Create the pid lock file. */
54 const char *pidfile = APCPID;
55 void make_pid_file(void)
57 #if !defined(HAVE_WIN32)
58 pid_t pid = getpid();
59 int pfd, len;
60 char buf[100];
62 unlink(pidfile);
63 if ((pfd = open(pidfile, O_CREAT | O_TRUNC | O_WRONLY, 0644)) >= 0) {
64 len = asnprintf(buf, sizeof(buf), "%ld\n", (long)pid);
65 write(pfd, buf, len);
66 close(pfd);
68 #endif