Initial commit for Tizen
[profile/extras/shadow-utils.git] / lib / nscd.c
1 /* Author: Peter Vrabec <pvrabec@redhat.com> */
2
3 #include <config.h>
4 #ifdef USE_NSCD
5
6 /* because of TEMP_FAILURE_RETRY */
7 #ifndef _GNU_SOURCE
8 #define _GNU_SOURCE
9 #endif
10
11 #include <features.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <spawn.h>
16 #include <errno.h>
17 #include <sys/wait.h>
18 #include <sys/types.h>
19 #include "defines.h"
20 #include "nscd.h"
21
22 #define MSG_NSCD_FLUSH_CACHE_FAILED "Failed to flush the nscd cache.\n"
23
24 /*
25  * nscd_flush_cache - flush specified service buffer in nscd cache
26  */
27 int nscd_flush_cache (const char *service)
28 {
29         pid_t pid, termpid;
30         int err, status;
31         char *spawnedArgs[] = {"/usr/sbin/nscd", "nscd", "-i", service, NULL};
32         char *spawnedEnv[] = {NULL};
33
34         /* spawn process */
35         err = posix_spawn (&pid, spawnedArgs[0], NULL, NULL,
36                            spawnedArgs, spawnedEnv);
37         if(0 != err)
38         {
39                 (void) fputs (_(MSG_NSCD_FLUSH_CACHE_FAILED), stderr);
40                 (void) fprintf (stderr, "posix_spawn() error=%d\n", err);
41                 return -1;
42         }
43
44         /* Wait for the spawned process to exit */
45         termpid = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
46         if (-1 == termpid)
47         {
48                 (void) fputs (_(MSG_NSCD_FLUSH_CACHE_FAILED), stderr);
49                 perror("waitpid");
50                 return -1;
51         }
52         else if (termpid != pid)
53         {
54                 (void) fputs (_(MSG_NSCD_FLUSH_CACHE_FAILED), stderr);
55                 (void) fprintf (stderr, "waitpid returned %ld != %ld\n",
56                                (long int) termpid, (long int) pid);
57                 return -1;
58         }
59
60         return 0;
61 }
62 #else                           /* USE_NSCD */
63 extern int errno;               /* warning: ANSI C forbids an empty source file */
64 #endif                          /* USE_NSCD */
65