XBPS Library API  0.19
The X Binary Package System
package_script.c
1 /*-
2  * Copyright (c) 2012 Juan Romero Pardines.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <stdio.h>
27 #include <stdbool.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <errno.h>
31 
32 #include "xbps_api_impl.h"
33 
34 int
36  const void *blob,
37  const size_t blobsiz,
38  const char *pkgname,
39  const char *version,
40  const char *action,
41  bool update)
42 {
43  ssize_t ret;
44  const char *tmpdir;
45  char *fpath;
46  int fd, rv;
47 
48  assert(blob);
49  assert(pkgname);
50  assert(version);
51  assert(action);
52 
53  if (strcmp(xhp->rootdir, "/") == 0) {
54  tmpdir = getenv("TMPDIR");
55  if (tmpdir == NULL)
56  tmpdir = P_tmpdir;
57 
58  fpath = xbps_xasprintf("%s/.xbps-script-XXXXXX", tmpdir);
59  } else {
60  fpath = strdup(".xbps-script-XXXXXX");
61  }
62 
63  /* change cwd to rootdir to exec the script */
64  if (chdir(xhp->rootdir) == -1) {
65  rv = errno;
66  goto out;
67  }
68 
69  /* Create temp file to run script */
70  if ((fd = mkstemp(fpath)) == -1) {
71  xbps_dbg_printf(xhp, "%s: mkstemp %s\n",
72  __func__, strerror(errno));
73  free(fpath);
74  return errno;
75  }
76  /* write blob to our temp fd */
77  ret = write(fd, blob, blobsiz);
78  if (ret == -1) {
79  xbps_dbg_printf(xhp, "%s: write %s\n",
80  __func__, strerror(errno));
81  close(fd);
82  rv = errno;
83  goto out;
84  }
85  fchmod(fd, 0750);
86  fdatasync(fd);
87  close(fd);
88 
89  /* exec script */
90  rv = xbps_file_exec(xhp, fpath, action, pkgname, version,
91  update ? "yes" : "no",
92  xhp->conffile, NULL);
93 
94 out:
95  remove(fpath);
96  free(fpath);
97  return rv;
98 }
99 
100 int
102  prop_dictionary_t d,
103  const char *script,
104  const char *action,
105  bool update)
106 {
107  prop_data_t data;
108  const char *pkgname, *version;
109 
110  assert(xhp);
111  assert(d);
112  assert(script);
113  assert(action);
114 
115  data = prop_dictionary_get(d, script);
116  if (data == NULL)
117  return 0;
118 
119  prop_dictionary_get_cstring_nocopy(d, "pkgname", &pkgname);
120  prop_dictionary_get_cstring_nocopy(d, "version", &version);
121 
122  return xbps_pkg_exec_buffer(xhp, prop_data_data(data),
123  prop_data_size(data), pkgname, version,
124  action, update);
125 }